Skip to content

Commit

Permalink
Feat : 네이버 oauth 기능, 카카오 oauth 기능 추가 (#213)
Browse files Browse the repository at this point in the history
* Fix : 카카오 회원탈퇴 오류 수정 , 카카오 추가정보 있을때 회원가입 (#210)

* Feat : 카카오 추가정보 있을때 회원가입

* Fix : 카카오 회원탈퇴 오류 수정

성별이 필드가 비어있을때 조회 사용하는 로직 수정

* Feat : 네이버 oauth (#211)

회원가입, 로그인, 로그아웃, 회원탈퇴

---------

Co-authored-by: hyeonjaez <jhjh0022@naver.com>
Co-authored-by: Fiat_lux <50399586+hyeonjaez@users.noreply.github.com>
Co-authored-by: SK\ssssk <ssssksss@naver.com>
Co-authored-by: 노현진 <nhj9831@naver.com>
  • Loading branch information
5 people authored Sep 26, 2024
1 parent 9eb7c75 commit 605a329
Show file tree
Hide file tree
Showing 13 changed files with 488 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -18,12 +18,16 @@
import solitour_backend.solitour.auth.entity.Token;
import solitour_backend.solitour.auth.entity.TokenRepository;
import solitour_backend.solitour.auth.exception.TokenNotExistsException;
import solitour_backend.solitour.auth.exception.UnsupportedLoginTypeException;
import solitour_backend.solitour.auth.exception.UserRevokeErrorException;
import solitour_backend.solitour.auth.service.OauthService;
import solitour_backend.solitour.auth.service.dto.response.AccessTokenResponse;
import solitour_backend.solitour.auth.service.dto.response.LoginResponse;
import solitour_backend.solitour.auth.service.dto.response.OauthLinkResponse;
import solitour_backend.solitour.auth.support.google.GoogleConnector;
import solitour_backend.solitour.auth.support.kakao.KakaoConnector;
import solitour_backend.solitour.auth.support.kakao.dto.request.CreateUserInfoRequest;
import solitour_backend.solitour.auth.support.naver.NaverConnector;
import solitour_backend.solitour.user.user_status.UserStatus;


Expand All @@ -35,6 +39,7 @@ public class OauthController {
private final OauthService oauthService;
private final KakaoConnector kakaoConnector;
private final GoogleConnector googleConnector;
private final NaverConnector naverConnector;
private final TokenRepository tokenRepository;

@GetMapping(value = "/login", params = {"type", "redirectUrl"})
Expand All @@ -43,6 +48,22 @@ public ResponseEntity<OauthLinkResponse> access(@RequestParam String type, @Requ
return ResponseEntity.ok(response);
}

@PostMapping(value = "/login/kakao", params = {"code", "redirectUrl"})
public ResponseEntity<UserStatus> kakaoLogin(HttpServletResponse response,
@RequestParam String code, @RequestParam String redirectUrl,
@RequestBody CreateUserInfoRequest createUserInfoRequest) {
LoginResponse loginResponse = oauthService.requestkakaoAccessToken(code, redirectUrl, createUserInfoRequest);

String accessCookieHeader = setCookieHeader(loginResponse.getAccessToken());
String refreshCookieHeader = setCookieHeader(loginResponse.getRefreshToken());

response.addHeader("Set-Cookie", accessCookieHeader);
response.addHeader("Set-Cookie", refreshCookieHeader);

return ResponseEntity.ok(loginResponse.getLoginStatus());
}


@GetMapping(value = "/login", params = {"type", "code", "redirectUrl"})
public ResponseEntity<UserStatus> login(HttpServletResponse response, @RequestParam String type,
@RequestParam String code, @RequestParam String redirectUrl) {
Expand Down Expand Up @@ -78,8 +99,8 @@ public ResponseEntity<Void> reissueAccessToken(HttpServletResponse response,

@Authenticated
@DeleteMapping()
public ResponseEntity<String> deleteUser(HttpServletResponse response, @AuthenticationPrincipal Long id,
@RequestParam String type) {
public ResponseEntity<Void> deleteUser(HttpServletResponse response, @AuthenticationPrincipal Long id,
@RequestParam String type) {
Token token = tokenRepository.findByUserId(id)
.orElseThrow(() -> new TokenNotExistsException("토큰이 존재하지 않습니다"));
String oauthRefreshToken = getOauthAccessToken(type, token.getOauthToken());
Expand All @@ -89,11 +110,11 @@ public ResponseEntity<String> deleteUser(HttpServletResponse response, @Authenti

oauthService.logout(response, id);
oauthService.deleteUser(id);

return ResponseEntity.ok("User deleted successfully");
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred");
throw new UserRevokeErrorException("회원 탈퇴 중 오류가 발생했습니다");
}

return ResponseEntity.noContent().build();
}

private String setCookieHeader(Cookie cookie) {
Expand All @@ -107,10 +128,13 @@ private String getOauthAccessToken(String type, String refreshToken) {
case "kakao" -> {
token = kakaoConnector.refreshToken(refreshToken);
}
case "google" -> {
token = googleConnector.refreshToken(refreshToken);
case "naver" -> {
token = naverConnector.refreshToken(refreshToken);
}
default -> throw new RuntimeException("Unsupported oauth type");
// case "google" -> {
// token = googleConnector.refreshToken(refreshToken);
// }
default -> throw new UnsupportedLoginTypeException("지원하지 않는 로그인 타입입니다");
}
return token;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package solitour_backend.solitour.auth.exception;

public class RevokeFailException extends RuntimeException {
public RevokeFailException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package solitour_backend.solitour.auth.exception;

public class UnsupportedLoginTypeException extends RuntimeException {
public UnsupportedLoginTypeException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package solitour_backend.solitour.auth.exception;

public class UserRevokeErrorException extends RuntimeException {
public UserRevokeErrorException(String message) {
super(message);
}
}
Loading

0 comments on commit 605a329

Please sign in to comment.