Skip to content

Commit

Permalink
Feat: RefreshToken API 호출 시 DB 업데이트
Browse files Browse the repository at this point in the history
  • Loading branch information
YooJisu826 committed Sep 2, 2024
1 parent 15719cb commit 3e9dc2a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion config
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ public class MemberService {
private final GoogleSocialLoginService googleSocialLoginService;
private final NaverSocialLoginService naverSocialLoginService;

@Transactional
public void save(Member member) {
memberRepository.save(member);
}

@Transactional
public Member register(RegisterMember registerMember) {
Member member = new Member(
registerMember.getEmail(),
Expand All @@ -72,6 +74,7 @@ public Member register(RegisterMember registerMember) {
return member;
}

@Transactional
public TokenInfo registerMember(RegisterMember registerMember) {
checkRegister(registerMember);
Member member = register(registerMember);
Expand All @@ -82,6 +85,7 @@ public TokenInfo registerMember(RegisterMember registerMember) {
return tokenInfo;
}

@Transactional
public TokenInfo socialLogin(SocialLoginType socialLoginType,
SocialLoginToken socialLoginToken) throws BaseException, IOException {
String idToken = socialLoginToken.getIdToken();
Expand Down Expand Up @@ -144,6 +148,7 @@ public List<TeacherInfo> findHotTeacherForHome() {
return teacherInfos;
}

@Transactional
public String modifyProfile(Member member, ModifyProfile modifyProfile) {
// 대표 뱃지 변경
String badgeName = badgeService.changeRepresentativeBadge(member, modifyProfile.getBadgeId());
Expand Down Expand Up @@ -171,9 +176,12 @@ public MemberProfileInfo getProfile(Long memberId) {
}

public TokenInfo refreshAccessToken(Member member) {
String refreshToken = jwtTokenProvider.generateRefreshToken(member.getId());
member.changeRefreshToken(refreshToken);
save(member);
return TokenInfo.builder()
.accessToken(jwtTokenProvider.generateAccessToken(member.getId()))
.refreshToken(member.getRefreshToken())
.refreshToken(refreshToken)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,42 @@ protected void init() {
}

public String generateAccessToken(Long memberId) {
Claims claims = Jwts.claims();
claims.put("memberId", memberId);

Date now = new Date();
Date accessTokenExpirationTime = new Date(now.getTime() + TOKEN_VALID_TIME);

return Jwts.builder()
.setClaims(claims)
.setClaims(setClaims(memberId))
.setIssuedAt(now) // 토큰 발행 시간 정보
.setExpiration(accessTokenExpirationTime)
.signWith(SignatureAlgorithm.HS256, jwtSecretKey)
.compact();
}

public MemberResponseDto.TokenInfo generateToken(Long memberId) {
Claims claims = Jwts.claims();
claims.put("memberId", memberId);

public String generateRefreshToken(Long memberId) {
Date now = new Date();
Date refreshTokenExpirationTime = new Date(now.getTime() + REF_TOKEN_VALID_TIME);

String accessToken = generateAccessToken(memberId);
String refreshToken = Jwts.builder()
.setClaims(claims)
.setIssuedAt(now) // 토큰 발행 시간 정보
.setExpiration(refreshTokenExpirationTime)
.signWith(SignatureAlgorithm.HS256, refreshSecretKey)
.compact();
return Jwts.builder()
.setClaims(setClaims(memberId))
.setIssuedAt(now) // 토큰 발행 시간 정보
.setExpiration(refreshTokenExpirationTime)
.signWith(SignatureAlgorithm.HS256, refreshSecretKey)
.compact();
}

public MemberResponseDto.TokenInfo generateToken(Long memberId) {
String accessToken = generateAccessToken(memberId);
String refreshToken = generateRefreshToken(memberId);
return new MemberResponseDto.TokenInfo(accessToken, refreshToken);
}

public Claims setClaims(Long memberId) {
Claims claims = Jwts.claims();
claims.put("memberId", memberId);

return claims;
}

public Authentication getAuthentication(String token) {
try {
PrincipalDetails principalDetails = principalDetailsService.loadUserByUsername(
Expand Down

0 comments on commit 3e9dc2a

Please sign in to comment.