From 7e6cdb58308db529578c9b2c3cded5d0f94ad0d0 Mon Sep 17 00:00:00 2001 From: hojeong2747 Date: Wed, 12 Jul 2023 21:50:00 +0900 Subject: [PATCH] =?UTF-8?q?feat(#3)=20:=20=EA=B5=AC=EA=B8=80=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=20=ED=86=A0=ED=81=B0=20=EA=B0=92=20DB=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=20->=20=EC=84=B1=EA=B3=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/auth/OAuthController.java | 6 ++- .../trothcam/service/auth/OAuthService.java | 37 ++++++++++++++++++- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/main/java/trothly/trothcam/controller/auth/OAuthController.java b/src/main/java/trothly/trothcam/controller/auth/OAuthController.java index 89af8e5..7385a49 100644 --- a/src/main/java/trothly/trothcam/controller/auth/OAuthController.java +++ b/src/main/java/trothly/trothcam/controller/auth/OAuthController.java @@ -58,9 +58,11 @@ public void socialLoginType(@PathVariable(name="socialLoginType") String socialL // code -> accessToken 받아오기 // accessToken -> 사용자 정보 받아오기 @GetMapping(value="/auth/{socialLoginType}/callback") - public void callback( + public BaseResponse callback( @PathVariable(name="socialLoginType") String socialLoginType, @RequestParam(name="code") String code) throws JsonProcessingException { - oauthService.oauthLogin(socialLoginType, code); + + LoginResDto result = oauthService.oauthLogin(socialLoginType, code); + return BaseResponse.onSuccess(result); } } diff --git a/src/main/java/trothly/trothcam/service/auth/OAuthService.java b/src/main/java/trothly/trothcam/service/auth/OAuthService.java index 0563c6b..692d23d 100644 --- a/src/main/java/trothly/trothcam/service/auth/OAuthService.java +++ b/src/main/java/trothly/trothcam/service/auth/OAuthService.java @@ -115,7 +115,8 @@ public void request(String socialLoginType) throws IOException { // code -> accessToken 받아오기 // accessToken -> 사용자 정보 받아오기 - public void oauthLogin(String socialLoginType, String code) throws JsonProcessingException { + @Transactional + public LoginResDto oauthLogin(String socialLoginType, String code) throws JsonProcessingException { // 구글로 일회성 코드를 보내 액세스 토큰이 담긴 응답객체를 받아옴 ResponseEntity accessTokenResponse = googleOauth.requestAccessToken(code); @@ -127,5 +128,39 @@ public void oauthLogin(String socialLoginType, String code) throws JsonProcessin // 다시 JSON 형식의 응답 객체를 deserialization 해서 자바 객체에 담음 GoogleUser googleUser = googleOauth.getUserInfo(userInfoResponse); + String email = googleUser.getEmail(); + log.info(googleUser.getEmail()); + + if(email == null) + throw new BaseException(ErrorCode.NOT_AGREE_EMAIL); + + Optional getMember = memberRepository.findByEmail(email); + Member member; + if(getMember.isPresent()){ // 이미 회원가입한 회원인 경우 + member = getMember.get(); + if(!member.getProvider().equals(Provider.GOOGLE)) // 이미 회원가입했지만 GOOGLE이 아닌 다른 소셜 로그인 사용 + throw new InvalidProviderException("APPLE로 회원가입한 회원입니다."); + } else { // 아직 회원가입 하지 않은 회원인 경우 + member = memberRepository.save( + Member.builder() + .email(email) + .provider(Provider.GOOGLE) + .build() + ); + } + + // accessToken, refreshToken 발급 + String newAccessToken = jwtService.encodeJwtToken(new TokenDto(member.getId())); + String newRefreshToken = jwtService.encodeJwtRefreshToken(member.getId()); + + // redis에 refreshToken 저장 +// redisTemplate.opsForValue().set(member.getId().toString(), newRefreshToken, 14L, TimeUnit.SECONDS); +// log.info("redis에 저장된 refreshToken : " + newRefreshToken + "\nmember.getId : " + member.getId().toString()); + + // DB에 refreshToken 저장 + member.updateRefreshToken(newRefreshToken); + memberRepository.save(member); + return new LoginResDto(newAccessToken, newRefreshToken); + } }