Skip to content

Commit

Permalink
Merge pull request #38 from Troth-Cam/feat/google-login
Browse files Browse the repository at this point in the history
feat(#3) : 구글 로그인 토큰 값 DB 저장 -> 성공
  • Loading branch information
hojeong2747 authored Jul 12, 2023
2 parents 2ab58e1 + 7e6cdb5 commit 3460f55
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<LoginResDto> 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);
}
}
37 changes: 36 additions & 1 deletion src/main/java/trothly/trothcam/service/auth/OAuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> accessTokenResponse = googleOauth.requestAccessToken(code);

Expand All @@ -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<Member> 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);

}
}

0 comments on commit 3460f55

Please sign in to comment.