Skip to content

Commit

Permalink
fix(#5) : DB에 refreshToken 저장
Browse files Browse the repository at this point in the history
  • Loading branch information
aeeazip committed Jul 12, 2023
1 parent 6ca3742 commit c554db8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public BaseResponse<LoginResDto> appleLogin(@RequestBody @Validated LoginReqDto
}

// refreshToken으로 accessToken 재발급
// @PostMapping("/refresh")
// public BaseResponse<LoginResDto> regenerateAccessToken(@RequestBody @Validated RefreshTokenReqDto refreshTokenReqDto) throws BaseException {
// LoginResDto result = oauthService.regenerateAccessToken(refreshTokenReqDto);
// return BaseResponse.onSuccess(result);
// }
@PostMapping("/refresh")
public BaseResponse<LoginResDto> regenerateAccessToken(@RequestBody @Validated RefreshTokenReqDto refreshTokenReqDto) throws BaseException {
LoginResDto result = oauthService.regenerateAccessToken(refreshTokenReqDto);
return BaseResponse.onSuccess(result);
}

// 구글 로그인
@GetMapping(value="/auth/{socialLoginType}")
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/trothly/trothcam/domain/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import javax.persistence.*;
import javax.validation.constraints.Email;
import java.time.LocalDateTime;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -33,9 +34,22 @@ public class Member extends BaseTimeEntity {
@Enumerated(EnumType.STRING)
private Provider provider;

@Column(name = "refresh_token", nullable = false)
private String refreshToken;

@Column(name = "refresh_token_expires_at", nullable = false)
private LocalDateTime refreshTokenExpiresAt;

@Builder
private Member(String email, Provider provider) {
this.email = email;
this.provider = provider;
this.refreshToken = "";
this.refreshTokenExpiresAt = LocalDateTime.now();
}

public void updateRefreshToken(String refreshToken) {
this.refreshToken = refreshToken;
this.refreshTokenExpiresAt = LocalDateTime.now();
}
}
42 changes: 23 additions & 19 deletions src/main/java/trothly/trothcam/service/auth/OAuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
import trothly.trothcam.dto.auth.apple.LoginReqDto;
import trothly.trothcam.dto.auth.apple.LoginResDto;
//import trothly.trothcam.dto.auth.apple.RefreshTokenReqDto;
import trothly.trothcam.dto.auth.apple.RefreshTokenReqDto;
import trothly.trothcam.dto.auth.google.GoogleOauthToken;
import trothly.trothcam.dto.auth.google.GoogleUser;
import trothly.trothcam.exception.base.*;
import trothly.trothcam.exception.custom.InvalidProviderException;
import trothly.trothcam.auth.apple.AppleOAuthUserProvider;
import trothly.trothcam.domain.member.*;
//import trothly.trothcam.exception.custom.InvalidTokenException;
import trothly.trothcam.exception.custom.InvalidTokenException;
import trothly.trothcam.service.JwtService;

import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -76,29 +78,31 @@ public LoginResDto appleLogin(LoginReqDto loginReqDto) throws BaseException {
// 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);
}

// refreshToken으로 accessToken 발급하기
// @Transactional
// public LoginResDto regenerateAccessToken(RefreshTokenReqDto refreshTokenReqDto) throws BaseException {
// String getRefreshToken = refreshTokenReqDto.getRefreshToken();
// Long memberId = jwtService.getMemberIdFromJwtToken(getRefreshToken);
//
// String redisRefreshToken = redisTemplate.opsForValue().get(memberId.toString());
// log.info("getRefreshToken : " + getRefreshToken);
// log.info("redisRefreshToken : " + redisRefreshToken); // 요 부분이 값이 있었다가 null로 떴다가 그래
//
// if(!getRefreshToken.equals(redisRefreshToken))
// throw new InvalidTokenException("유효하지 않은 Refresh Token입니다.");
//
// String newAccessToken = jwtService.encodeJwtToken(new TokenDto(memberId));
// String newRefreshToken = jwtService.encodeJwtRefreshToken(memberId);
//
// redisTemplate.opsForValue().set(memberId.toString(), newRefreshToken, 14L, TimeUnit.SECONDS);
//
// return new LoginResDto(newAccessToken, newRefreshToken);
// }
@Transactional
public LoginResDto regenerateAccessToken(RefreshTokenReqDto refreshTokenReqDto) throws BaseException {
Long memberId = jwtService.getMemberIdFromJwtToken(refreshTokenReqDto.getRefreshToken());
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new IllegalArgumentException("해당되는 member_id를 찾을 수 없습니다."));

String refreshToken = refreshTokenReqDto.getRefreshToken();
if(refreshToken.equals(member.getRefreshToken()))
throw new InvalidTokenException("유효하지 않은 Refresh Token입니다.");

String newRefreshToken = jwtService.encodeJwtRefreshToken(memberId);
String newAcessToken = jwtService.encodeJwtToken(new TokenDto(memberId));

member.updateRefreshToken(newRefreshToken);
memberRepository.save(member);

return new LoginResDto(newAcessToken, newRefreshToken);
}

// 구글 로그인
//1. request
Expand Down

0 comments on commit c554db8

Please sign in to comment.