Skip to content

Commit

Permalink
bug: 모바일 인증 로직 수정 (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
qzzloz authored Dec 20, 2024
1 parent 530e306 commit f7f6d5a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,34 +107,54 @@ public MobileTokenResponseDTO verifyGoogleToken(String accessToken, String name)
Map<String, Object> tokenInfo = restTemplate.getForObject(url, Map.class);
log.info("===== Token info received ===== " + tokenInfo);
if (tokenInfo != null && tokenInfo.containsKey("sub")) {

MobileUserDTO mobileUserDTO = MobileUserDTO.builder()
.email((String) tokenInfo.get("email"))
.username((String) tokenInfo.get("sub"))
.name(name)
.role("ROLE_USER")
.isNewAccount(false)
.build();

if(!userRepository.findByUsernameContaining(mobileUserDTO.getUsername()).isPresent()){
mobileUserDTO.setNewAccount(true);
String email = (String) tokenInfo.get("email");
String username = (String) tokenInfo.get("sub");

// 사용자 정보 검색
Optional<User> existingUser = userRepository.findByUsernameContaining(username);
String role;
boolean isNewAccount;

if (existingUser.isPresent()) {
// 기존 사용자: Role 정보 가져오기
role = existingUser.get().getRole();
isNewAccount = false;
log.info("Existing user found: " + username);
} else {
// 새로운 사용자: Role 기본값 설정
role = "ROLE_USER";
isNewAccount = true;
log.info("New user created: " + username);

// 신규 사용자 정보 저장
MobileUserDTO newUser = MobileUserDTO.builder()
.email(email)
.username(username)
.name(name)
.role(role)
.isNewAccount(true)
.build();
userService.saveMobileUser(newUser);
}

userService.saveMobileUser(mobileUserDTO);
// JWT 토큰 생성
String newAccessToken = jwtUtil.createJwt("access", username, role, 1800000L); // Access token (30분)
String newRefreshToken = jwtUtil.createJwt("refresh", username, role, 86400000L); // Refresh token (24시간)

log.info("[*] USER>>>>> EMAIL[" + mobileUserDTO.getEmail(), "] NAME[" + mobileUserDTO.getUsername() + "] ROLE[" + mobileUserDTO.getRole() + "]");

String newAccessToken = jwtUtil.createJwt("access", mobileUserDTO.getUsername(), "ROLE_USER", 1800000000L); // Access token (30분)
String newRefreshToken = jwtUtil.createJwt("refresh", mobileUserDTO.getUsername(), "ROLE_USER", 86400000L); // Refresh token (24시간)
// log.info("[*] USER>>>>> EMAIL[" + mobileUserDTO.getEmail(), "] NAME[" + mobileUserDTO.getUsername() + "] ROLE[" + mobileUserDTO.getRole() + "]");
//
// String newAccessToken = jwtUtil.createJwt("access", mobileUserDTO.getUsername(), "ROLE_USER", 1800000000L); // Access token (30분)
// String newRefreshToken = jwtUtil.createJwt("refresh", mobileUserDTO.getUsername(), "ROLE_USER", 86400000L); // Refresh token (24시간)

// Refresh 토큰을 Redis 또는 DB에 저장 (선택적)
refreshTokenService.addRefreshEntity(mobileUserDTO.getUsername(), newRefreshToken, 86400000L); // 24 hrs
refreshTokenService.addRefreshEntity(username, newRefreshToken, 86400000L);

// access, refresh 토큰 반환
// Access, Refresh 토큰 반환
return MobileTokenResponseDTO.builder()
.refreshToken(newRefreshToken)
.accessToken(newAccessToken)
.isNewAccount(mobileUserDTO.isNewAccount())
.isNewAccount(isNewAccount)
.build();
} else {
log.error("===== Invalid token info ===== " + tokenInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void deleteUser(String username) {

@Transactional
public void saveMobileUser(MobileUserDTO dto) {
if(!userRepository.findByUsername(dto.getUsername()).isPresent()){
if(!userRepository.findByUsernameContaining(dto.getUsername()).isPresent()){
userRepository.save(dto.toEntity());
}
}
Expand Down

0 comments on commit f7f6d5a

Please sign in to comment.