Skip to content

Commit

Permalink
#8 refactor: 회원탈퇴 api 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
sojungpp committed Mar 4, 2023
1 parent 4dc78a3 commit 0684307
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public enum BaseResponseStatus {
NULL_EMAIL(false, 2001, "이메일을 입력해주세요."),
NULL_PROVIDER(false, 2002, "소셜 이름을 입력해주세요."),
INVALID_PROVIDER(false, 2003, "잘못된 소셜 이름입니다."),
ALREADY_WITHDRAW_USER(false, 2004, "이미 삭제된 회원입니다."),
ALREADY_WITHDRAW_USER(false, 2004, "이미 탈퇴한 회원입니다."),
INVALID_TOKEN(false, 2005, "유효하지 않은 토큰 값입니다."),

// stores(2100~2199)
Expand Down
33 changes: 13 additions & 20 deletions src/main/java/com/codepatissier/keki/user/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,14 @@ public String validateRefreshToken(Long userIdx, String refreshTokenReq) throws
public void logout(Long userIdx) throws BaseException {
deleteToken(userIdx);
String token = getToken();
Long expiration = getExpiration(token);
registerBlackList(token, expiration);
registerBlackList(token, Constant.LOGOUT_STATUS);
}

// 회원 탈퇴
public void signout(Long userIdx) throws BaseException {
deleteToken(userIdx);
String token = getToken();
registerBlackList(token, Constant.INACTIVE_STATUS);
}

// refreshToken 삭제
Expand All @@ -132,27 +138,14 @@ public void deleteToken(Long userIdx) {
if(redisTemplate.opsForValue().get(key)!=null) redisTemplate.delete(key);
}

// 토큰 유효시간 구하기
public Long getExpiration(String token) {
// 유효한 토큰 blacklist로 등록
private void registerBlackList(String token, String status) {
token = token.replaceAll(TOKEN_REGEX, TOKEN_REPLACEMENT);
Date expiration = Jwts.parser().setSigningKey(key).parseClaimsJws(token).getBody().getExpiration();
Date AccessTokenExpiration = Jwts.parser().setSigningKey(key).parseClaimsJws(token).getBody().getExpiration();
long now = (new Date()).getTime();
return (expiration.getTime() - now);
}

// blacklist로 등록
private void registerBlackList(String token, Long expiration) {
redisTemplate.opsForValue().set(token, Constant.LOGOUT_STATUS, Duration.ofMillis(expiration));
Long expiration = AccessTokenExpiration.getTime() - now;
redisTemplate.opsForValue().set(token, status, Duration.ofMillis(expiration));
}

private void registerSignout(String token, Long expiration) {
redisTemplate.opsForValue().set(token, Constant.INACTIVE_STATUS, Duration.ofMillis(expiration));
}

public void signout(Long userIdx) throws BaseException {
deleteToken(userIdx);
String token = getToken();
Long expiration = getExpiration(token);
registerBlackList(token, expiration);
}
}

0 comments on commit 0684307

Please sign in to comment.