Skip to content

Commit

Permalink
[refactor][#53] dto 변경 관련 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
ProtoSeo committed Jul 25, 2022
1 parent 0a3d751 commit b70a36c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetailsService;
Expand Down Expand Up @@ -47,41 +48,38 @@ public Long signup(SignupRequest request) {
if (isDuplicateNickname(request.getNickname())) {
throw new UserException(DUPLICATE_NICKNAME);
}

User user = userRepository.save(User.builder()
.email(request.getEmail())
.nickname(request.getNickname())
.password(new BCryptPasswordEncoder().encode(request.getPassword()))
.provider(AuthProvider.LOCAL)
.build());

User user = userRepository.save(request.toEntity());
return user.getId();
}

@Transactional
public LoginResponse login(LoginRequest loginRequest) {
SecurityContext context = SecurityContextHolder.createEmptyContext();
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(loginRequest.getEmail(), loginRequest.getPassword()));
context.setAuthentication(authentication);
SecurityContextHolder.setContext(context);
Authentication authentication = getAuthentication(loginRequest);

UserPrincipal userPrincipal = (UserPrincipal)authentication.getPrincipal();
Long userId = userPrincipal.getId();
String email = userPrincipal.getEmail();
String nickname = userPrincipal.getNickname();
String accessToken = jwtUtil.generateJwtToken(userPrincipal);
String uuid = UUID.randomUUID().toString();

redisUtil.setDataExpire(uuid, String.valueOf(userId), (int)JwtUtil.REFRESH_EXPIRATION_SECONDS);
redisUtil.setDataExpire(uuid, "true", (int)JwtUtil.REFRESH_EXPIRATION_SECONDS);

return LoginResponse.of(userPrincipal, accessToken, uuid);
}

return LoginResponse.builder()
.userId(userId)
.email(email)
.accessToken(accessToken)
.nickname(nickname)
.uuid(uuid)
.build();
private Authentication getAuthentication(LoginRequest loginRequest) {
SecurityContext context = SecurityContextHolder.createEmptyContext();
Authentication authentication = authenticate(loginRequest);
context.setAuthentication(authentication);
SecurityContextHolder.setContext(context);
return authentication;
}

private Authentication authenticate(LoginRequest loginRequest) {
try {
return authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(loginRequest.getEmail(), loginRequest.getPassword()));
} catch (AuthenticationException e) {
throw new UserException(INVALID_PASSWORD);
}
}

@Transactional
Expand All @@ -98,7 +96,7 @@ public AccessTokenResponse refreshToken(RefreshRequest request) {
String newAccessToken = jwtUtil.generateJwtToken(userPrincipal);

redisUtil.setDataExpire(oldAccessToken, "true", (int)JwtUtil.TOKEN_EXPIRATION_SECONDS);
return new AccessTokenResponse(userPrincipal.getId(), newAccessToken);
return AccessTokenResponse.of(userPrincipal.getId(), newAccessToken);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class UserService {
@Transactional(readOnly = true)
public UserInfoResponse getUserInformation(Long id) {
User user = userRepository.findById(id).orElseThrow(() -> new UserException(ExceptionType.USER_NOT_FOUND));
return new UserInfoResponse(user.getId(), user.getEmail(), user.getNickname());
return UserInfoResponse.from(user);
}

@Transactional
Expand Down

0 comments on commit b70a36c

Please sign in to comment.