Skip to content

Commit

Permalink
Merge pull request #30 from dnd-side-project/feat/#29
Browse files Browse the repository at this point in the history
[#29] refresh token 재발급 API 추가
  • Loading branch information
youngreal authored Sep 2, 2024
2 parents 4a4acad + dd5425b commit 3fc3289
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.dnd.dndtravel.auth.controller;

import com.dnd.dndtravel.auth.controller.request.ReIssueTokenRequest;
import com.dnd.dndtravel.auth.service.dto.response.AppleIdTokenPayload;
import com.dnd.dndtravel.auth.service.AppleOAuthService;
import com.dnd.dndtravel.auth.service.JwtTokenService;
import com.dnd.dndtravel.auth.controller.request.AppleLoginRequest;
import com.dnd.dndtravel.auth.service.dto.response.TokenResponse;
import com.dnd.dndtravel.auth.service.dto.response.ReissueTokenResponse;
import com.dnd.dndtravel.member.domain.Member;
import com.dnd.dndtravel.member.service.MemberService;

Expand Down Expand Up @@ -41,4 +43,9 @@ public ResponseEntity<TokenResponse> appleOAuthLogin(@RequestBody AppleLoginRequ

return ResponseEntity.ok(tokenResponse);
}

@PostMapping("/reissue/token")
public ReissueTokenResponse reissueToken(@RequestBody ReIssueTokenRequest reissueTokenRequest) {
return jwtTokenService.reIssue(reissueTokenRequest.refreshToken());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.dnd.dndtravel.auth.controller.request;

public record ReIssueTokenRequest(
String refreshToken
) {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.dnd.dndtravel.auth.repository;


import java.util.Optional;

import com.dnd.dndtravel.auth.domain.RefreshToken;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface RefreshTokenRepository extends JpaRepository<RefreshToken, Long> {
RefreshToken findByMemberId(Long memberId);

Optional<RefreshToken> findByRefreshToken(String refreshToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ public String accessToken(Long memberId) {
.compact();
}

public String refreshToken(Long memberId) {
public String refreshToken() {
return Jwts.builder()
.claim(CLAIM_CONTENT, memberId)
.issuedAt(new Date(System.currentTimeMillis()))
.expiration(new Date(System.currentTimeMillis() + this.refreshTokenExpiredTime))
.signWith(secretKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.dnd.dndtravel.auth.domain.RefreshToken;
import com.dnd.dndtravel.auth.repository.RefreshTokenRepository;
import com.dnd.dndtravel.auth.service.dto.response.TokenResponse;
import com.dnd.dndtravel.auth.service.dto.response.ReissueTokenResponse;

import lombok.RequiredArgsConstructor;

Expand All @@ -21,7 +22,7 @@ public TokenResponse generateTokens(Long memberId) {
RefreshToken refreshToken = refreshTokenRepository.findByMemberId(memberId);

if (refreshToken == null) {
String newRefreshToken = jwtProvider.refreshToken(memberId);
String newRefreshToken = jwtProvider.refreshToken();
refreshTokenRepository.save(RefreshToken.of(memberId, newRefreshToken)); // refreshToken은 DB에 저장
return new TokenResponse(jwtProvider.accessToken(memberId), newRefreshToken);
} else if (refreshToken.isExpire()) {
Expand All @@ -30,4 +31,16 @@ public TokenResponse generateTokens(Long memberId) {

return new TokenResponse(jwtProvider.accessToken(memberId), null);
}

@Transactional
public ReissueTokenResponse reIssue(String token) {
//validation
RefreshToken refreshToken = refreshTokenRepository.findByRefreshToken(token).orElseThrow(() -> new RuntimeException("유효하지 않은 토큰"));

//RTR
refreshTokenRepository.delete(refreshToken);
String newRefreshToken = jwtProvider.refreshToken();
refreshTokenRepository.save(RefreshToken.of(refreshToken.getMemberId(), newRefreshToken));
return new ReissueTokenResponse(jwtProvider.accessToken(refreshToken.getMemberId()), newRefreshToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.dnd.dndtravel.auth.service.dto.response;

public record ReissueTokenResponse(
String accessToken,
String refreshToken
) {
}

0 comments on commit 3fc3289

Please sign in to comment.