Skip to content

Commit

Permalink
refactor: 액세스토큰과 리프레쉬토큰을 Enum으로 구분
Browse files Browse the repository at this point in the history
- 기존의 RefreshToken, AccessToken 객체를 제거
  • Loading branch information
SJ70 committed Jul 25, 2024
1 parent c71cfd6 commit 1104b7e
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 40 deletions.
13 changes: 0 additions & 13 deletions src/main/java/com/j9/bestmoments/domain/AccessToken.java

This file was deleted.

13 changes: 0 additions & 13 deletions src/main/java/com/j9/bestmoments/domain/RefreshToken.java

This file was deleted.

20 changes: 16 additions & 4 deletions src/main/java/com/j9/bestmoments/domain/Token.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
package com.j9.bestmoments.domain;

import lombok.Builder;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import java.util.UUID;
import lombok.Getter;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.TimeToLive;

@Getter
@NoArgsConstructor
@RedisHash("Token")
public class Token {
@RedisHash(value = "Token")
public
class Token {

@Id
private String token;

private TokenType tokenType;

@TimeToLive
private Long expiration;

private UUID memberId;

public Token(Member member, String token) {
this.token = token;
@Builder
public Token(Member member, TokenType tokenType, String token) {
this.memberId = member.getId();
this.token = token;
this.tokenType = tokenType;
this.expiration = tokenType.getExpiration();
}

}
18 changes: 18 additions & 0 deletions src/main/java/com/j9/bestmoments/domain/TokenType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.j9.bestmoments.domain;

import com.j9.bestmoments.constants.TokenExpiration;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum TokenType {

ACCESS_TOKEN("accessToken", TokenExpiration.ACCESS_TOKEN),
REFRESH_TOKEN("refreshToken", TokenExpiration.REFRESH_TOKEN),
;

private final String name;
private final long expiration;

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
@Repository
public interface TokenRepository extends CrudRepository<Token, String> {

boolean existsByToken(String token);

}
24 changes: 16 additions & 8 deletions src/main/java/com/j9/bestmoments/service/TokenService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.j9.bestmoments.service;

import com.j9.bestmoments.domain.AccessToken;
import com.j9.bestmoments.domain.Member;
import com.j9.bestmoments.domain.RefreshToken;
import com.j9.bestmoments.domain.Token;
import com.j9.bestmoments.domain.TokenType;
import com.j9.bestmoments.dto.response.LoginDto;
import com.j9.bestmoments.jwt.JwtTokenProvider;
import com.j9.bestmoments.repository.TokenRepository;
Expand All @@ -27,21 +26,30 @@ public LoginDto create(Member member) {
String accessToken = createAccessToken(member);
String refreshToken = createRefreshToken(member);
log.info("토큰 발급됨\n - accessToken: {}\n - refreshToken: {}", accessToken, refreshToken);
this.findByToken(accessToken);
return new LoginDto(accessToken, refreshToken, member.getDeletedAt());
}

private String createAccessToken(Member member) {
String accessTokenValue = jwtTokenProvider.generateAccessToken(member);
AccessToken accessToken = new AccessToken(member, accessTokenValue);
String token = jwtTokenProvider.generateAccessToken(member);
Token accessToken = Token.builder()
.member(member)
.tokenType(TokenType.ACCESS_TOKEN)
.token(token)
.build();
tokenRepository.save(accessToken);
return accessTokenValue;
return token;
}

private String createRefreshToken(Member member) {
String refreshTokenValue = jwtTokenProvider.generateRefreshToken(member);
RefreshToken refreshToken = new RefreshToken(member, refreshTokenValue);
String token = jwtTokenProvider.generateRefreshToken(member);
Token refreshToken = Token.builder()
.member(member)
.tokenType(TokenType.REFRESH_TOKEN)
.token(token)
.build();
tokenRepository.save(refreshToken);
return refreshTokenValue;
return token;
}

@Transactional
Expand Down

0 comments on commit 1104b7e

Please sign in to comment.