Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[♻️ refactor/#114]: signIn 메서드 리팩토링 및 코드 개선 #119

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,10 @@ public class AuthServiceImpl implements AuthService {
@Transactional
public SignInResponseDto signIn(String authAccessToken, SignInRequestDto request) {
String authId = getAuthId(request.authType(), authAccessToken);
Optional<User> userOptional = userRepository.findByAuthIdAndAuthType(authId, request.authType());

if (userOptional.isPresent()) {
User user = userOptional.get();
Token token = getToken(user);
user.updateRefreshToken(token.getRefreshToken());
return SignInResponseDto.of(
token,
authId,
request.authType(),
user.getId()
);
}
else {
return SignInResponseDto.of(null, authId, request.authType(), null);
}

return findUserByAuthIdAndType(authId, request.authType())
.map(user -> createSignInResponseForExistingUser(user, authId, request.authType()))
.orElseGet(() -> createSignInResponseForNonExistingUser(authId, request.authType()));
}

@Transactional
Expand Down Expand Up @@ -113,6 +101,25 @@ public void connectFilterToUser(long userId, long filterId) {
userRepository.save(user);
}

private Optional<User> findUserByAuthIdAndType(String authId, AuthType authType) {
return userRepository.findByAuthIdAndAuthType(authId, authType);
}

private SignInResponseDto createSignInResponseForExistingUser(User user, String authId, AuthType authType) {
Token token = getToken(user);
user.updateRefreshToken(token.getRefreshToken());
return SignInResponseDto.of(
token,
authId,
authType,
user.getId()
);
}

private SignInResponseDto createSignInResponseForNonExistingUser(String authId, AuthType authType) {
return SignInResponseDto.of(null, authId, authType, null);
}

private String getAuthId(AuthType authType, String authAccessToken) {
return switch (authType) {
case APPLE -> appleService.getAppleData(authAccessToken);
Expand Down
Loading