Skip to content

Commit 515cc60

Browse files
authored
fix: 토큰 갱신 API 로직 수정 (#70)
* feat: 카카오로그인/로그아웃 구현 * feat: 유저 탈퇴 / 유저 세부정보 입력 API 구현 * feat: 유저 탈퇴 / 유저 세부정보 입력 API 구현 * feat: 유저 탈퇴 로직 수정 * chore: 코드 정리 * fix: 토큰 갱신 API 로직 수정
1 parent 00491f6 commit 515cc60

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

src/main/java/com/startingblock/domain/auth/application/AuthService.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public AuthRes refresh(final RefreshTokenReq tokenRefreshRequest) {
6262

6363
Token refreshToken = tokenRepository.findByRefreshToken(tokenRefreshRequest.getRefreshToken())
6464
.orElseThrow(() -> new DefaultAuthenticationException(ErrorCode.INVALID_AUTHENTICATION));
65-
Authentication authentication = customTokenProviderService.getAuthenticationByEmail(refreshToken.getProviderId());
65+
66+
Authentication authentication = customTokenProviderService.getAuthenticationByProviderId(refreshToken.getProviderId());
6667

6768
//4. refresh token 정보 값을 업데이트 한다.
6869
//시간 유효성 확인
@@ -102,8 +103,9 @@ private boolean valid(String refreshToken){
102103
DefaultAssert.isTrue(token.isPresent(), "탈퇴 처리된 회원입니다.");
103104

104105
//3. email 값을 통해 인증값을 불러온다
105-
Authentication authentication = customTokenProviderService.getAuthenticationByEmail(token.get().getProviderId());
106-
DefaultAssert.isTrue(token.get().getProviderId().equals(authentication.getName()), "사용자 인증에 실패하였습니다.");
106+
Authentication authentication = customTokenProviderService.getAuthenticationByProviderId(token.get().getProviderId());
107+
UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal();
108+
DefaultAssert.isTrue(token.get().getProviderId().equals(userPrincipal.getPassword()), "사용자 인증에 실패하였습니다.");
107109

108110
return true;
109111
}

src/main/java/com/startingblock/domain/auth/application/CustomTokenProviderService.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,25 @@ public Long getUserIdFromToken(String token) {
9090
return Long.parseLong(claims.getSubject());
9191
}
9292

93-
public UsernamePasswordAuthenticationToken getAuthenticationById(String token){
93+
public UsernamePasswordAuthenticationToken getAuthenticationById(final String token){
9494
Long userId = getUserIdFromToken(token);
9595
UserDetails userDetails = customUserDetailsService.loadUserById(userId);
9696
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
9797
return authentication;
9898
}
9999

100-
public UsernamePasswordAuthenticationToken getAuthenticationByEmail(String email){
100+
public UsernamePasswordAuthenticationToken getAuthenticationByEmail(final String email){
101101
UserDetails userDetails = customUserDetailsService.loadUserByUsername(email);
102102
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
103103
return authentication;
104104
}
105105

106+
public UsernamePasswordAuthenticationToken getAuthenticationByProviderId(final String providerId){
107+
UserDetails userDetails = customUserDetailsService.loadUserByProviderId(providerId);
108+
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
109+
return authentication;
110+
}
111+
106112
public Long getExpiration(String token) {
107113
// accessToken 남은 유효시간
108114
Date expiration = Jwts.parserBuilder().setSigningKey(authConfig.getAuth().getTokenSecret()).build().parseClaimsJws(token).getBody().getExpiration();

src/main/java/com/startingblock/domain/auth/application/CustomUserDetailsService.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818

1919
@RequiredArgsConstructor
2020
@Service
21-
public class CustomUserDetailsService implements UserDetailsService{
21+
public class CustomUserDetailsService implements UserDetailsService {
2222

2323
private final UserRepository userRepository;
2424

2525
@Override
2626
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
27-
27+
2828
User user = userRepository.findByEmailAndStatus(email, Status.ACTIVE)
2929
.orElseThrow(() ->
3030
new UsernameNotFoundException("유저 정보를 찾을 수 없습니다.")
31-
);
31+
);
3232

3333
return UserPrincipal.create(user);
3434
}
@@ -40,5 +40,13 @@ public UserDetails loadUserById(Long id) {
4040

4141
return UserPrincipal.create(user.get());
4242
}
43-
43+
44+
@Transactional
45+
public UserDetails loadUserByProviderId(final String providerId) {
46+
Optional<User> user = userRepository.findByProviderIdAndStatus(providerId, Status.ACTIVE);
47+
DefaultAssert.isOptionalPresent(user);
48+
49+
return UserPrincipal.create(user.get());
50+
}
51+
4452
}

0 commit comments

Comments
 (0)