Skip to content

Commit

Permalink
Merge pull request #102 from teamterning/refactor/#101
Browse files Browse the repository at this point in the history
[♻️ refactor] 소셜 로그인 > 회원가입 로직 개선
  • Loading branch information
jsoonworld authored Aug 27, 2024
2 parents 8298f0e + 3e18889 commit 4b5b4d8
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,16 @@
import org.springframework.web.bind.annotation.*;
import org.terning.terningserver.controller.swagger.AuthSwagger;
import org.terning.terningserver.domain.Filter;
import org.terning.terningserver.domain.User;
import org.terning.terningserver.dto.auth.request.SignInRequestDto;
import org.terning.terningserver.dto.auth.request.SignUpFilterRequestDto;
import org.terning.terningserver.dto.auth.request.SignUpRequestDto;
import org.terning.terningserver.dto.auth.response.AccessTokenGetResponseDto;
import org.terning.terningserver.dto.auth.response.SignInResponseDto;
import org.terning.terningserver.dto.auth.response.SignUpFilterResponseDto;
import org.terning.terningserver.dto.auth.response.SignUpResponseDto;
import org.terning.terningserver.dto.auth.response.TokenGetResponseDto;
import org.terning.terningserver.exception.dto.SuccessResponse;
import org.terning.terningserver.service.AuthService;
import org.terning.terningserver.service.SignUpFilterService;
import org.terning.terningserver.service.SignUpService;

import java.security.Principal;

import static org.terning.terningserver.exception.enums.SuccessMessage.*;

Expand All @@ -31,7 +27,6 @@
public class AuthController implements AuthSwagger {

private final AuthService authService;
private final SignUpService signUpService;
private final SignUpFilterService signUpFilterService;

@PostMapping("/sign-in")
Expand All @@ -44,9 +39,8 @@ public ResponseEntity<SuccessResponse<SignInResponseDto>> signIn(
return ResponseEntity.ok(SuccessResponse.of(SUCCESS_SIGN_IN, signInResponse));
}

// TODO: 에러 메시지 위치
@PostMapping("/token-reissue")
public ResponseEntity<SuccessResponse<TokenGetResponseDto>> reissueToken(
public ResponseEntity<SuccessResponse<AccessTokenGetResponseDto>> reissueToken(
@RequestHeader("Authorization") String refreshToken
) {
val response = authService.reissueToken(refreshToken);
Expand All @@ -60,7 +54,7 @@ public ResponseEntity<SuccessResponse<SignUpResponseDto>> signUp(
@RequestBody SignUpRequestDto request
) {

SignUpResponseDto signUpResponseDto = signUpService.signUp(authId, request.name(), request.profileImage(), request.authType());
SignUpResponseDto signUpResponseDto = authService.signUp(authId, request.name(), request.profileImage(), request.authType());
return ResponseEntity.ok(SuccessResponse.of(SUCCESS_SIGN_UP, signUpResponseDto));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import org.terning.terningserver.dto.auth.request.SignInRequestDto;
import org.terning.terningserver.dto.auth.request.SignUpFilterRequestDto;
import org.terning.terningserver.dto.auth.request.SignUpRequestDto;
import org.terning.terningserver.dto.auth.response.AccessTokenGetResponseDto;
import org.terning.terningserver.dto.auth.response.SignInResponseDto;
import org.terning.terningserver.dto.auth.response.SignUpFilterResponseDto;
import org.terning.terningserver.dto.auth.response.SignUpResponseDto;
import org.terning.terningserver.dto.auth.response.TokenGetResponseDto;
import org.terning.terningserver.exception.dto.SuccessResponse;

import java.security.Principal;
Expand All @@ -27,7 +27,7 @@ ResponseEntity<SuccessResponse<SignInResponseDto>> signIn(
);

@Operation(summary = "토큰 재발급", description = "토큰 재발급 API")
ResponseEntity<SuccessResponse<TokenGetResponseDto>> reissueToken(
ResponseEntity<SuccessResponse<AccessTokenGetResponseDto>> reissueToken(
@RequestHeader("Authorization") String refreshToken
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.terning.terningserver.dto.auth.response;

import lombok.Builder;
import lombok.NonNull;
import org.terning.terningserver.domain.Token;

import static lombok.AccessLevel.*;

@Builder(access = PRIVATE)
public record AccessTokenGetResponseDto(
@NonNull String accessToken
) {

public static AccessTokenGetResponseDto of(Token accessToken) {
return AccessTokenGetResponseDto.builder()
.accessToken(accessToken.getAccessToken())
.build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package org.terning.terningserver.service;

import org.terning.terningserver.domain.User;
import org.terning.terningserver.domain.enums.AuthType;
import org.terning.terningserver.dto.auth.request.SignInRequestDto;
import org.terning.terningserver.dto.auth.response.AccessTokenGetResponseDto;
import org.terning.terningserver.dto.auth.response.SignInResponseDto;
import org.terning.terningserver.dto.auth.response.TokenGetResponseDto;
import org.terning.terningserver.dto.auth.response.SignUpResponseDto;

public interface AuthService {

SignInResponseDto signIn(String authAccessToken, SignInRequestDto request);

User saveUser(SignInRequestDto request);
SignUpResponseDto signUp(String authId, String name, Integer profileImage, AuthType authType);

void signOut(long userId);

void withdraw(long userId);

TokenGetResponseDto reissueToken(String refreshToken);
AccessTokenGetResponseDto reissueToken(String refreshToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
import org.terning.terningserver.domain.Token;
import org.terning.terningserver.domain.User;
import org.terning.terningserver.dto.auth.request.SignInRequestDto;
import org.terning.terningserver.dto.auth.response.AccessTokenGetResponseDto;
import org.terning.terningserver.dto.auth.response.SignInResponseDto;
import org.terning.terningserver.dto.auth.response.TokenGetResponseDto;
import org.terning.terningserver.domain.enums.AuthType;
import org.terning.terningserver.dto.auth.response.SignUpResponseDto;
import org.terning.terningserver.exception.CustomException;
import org.terning.terningserver.jwt.JwtTokenProvider;
import org.terning.terningserver.jwt.UserAuthentication;
Expand Down Expand Up @@ -57,11 +58,19 @@ public SignInResponseDto signIn(String authAccessToken, SignInRequestDto request
}

@Transactional
public User saveUser(SignInRequestDto request) {
User user = User.builder()
.authType(request.authType())
.build();
return userRepository.save(user);
public SignUpResponseDto signUp(String authId, String name, Integer profileImage, AuthType authType) {

User user = userRepository.save(User.builder()
.authId(authId)
.name(name)
.authType(authType)
.profileImage(profileImage)
.build());

Token token = getToken(user);
userRepository.save(user);

return SignUpResponseDto.of(token.getAccessToken(), token.getRefreshToken(), user.getId(), authType);
}

@Override
Expand All @@ -79,11 +88,10 @@ public void withdraw(long userId) {
}

@Override
public TokenGetResponseDto reissueToken(String refreshToken) {
public AccessTokenGetResponseDto reissueToken(String refreshToken) {
val user = findUser(refreshToken);
Token token = getToken(user);
user.updateRefreshToken(token.getRefreshToken());
return TokenGetResponseDto.of(token);
Token accessToken = getAccessToken(user);
return AccessTokenGetResponseDto.of(accessToken);
}

private String getAuthId(AuthType authType, String authAccessToken) {
Expand All @@ -99,13 +107,24 @@ public Token getToken(User user) {
return token;
}

public Token getAccessToken(User user) {
val accessToken = generateAccessToken(new UserAuthentication(user.getId(), null, null));
return accessToken;
}

private Token generateToken(Authentication authentication) {
return Token.builder()
.accessToken(jwtTokenProvider.generateToken(authentication, valueConfig.getAccessTokenExpired()))
.refreshToken(jwtTokenProvider.generateToken(authentication, valueConfig.getRefreshTokenExpired()))
.build();
}

private Token generateAccessToken(Authentication authentication) {
return Token.builder()
.accessToken(jwtTokenProvider.generateToken(authentication, valueConfig.getAccessTokenExpired()))
.build();
}

private User findUser(long id) {
return userRepository.findById(id).orElseThrow(() -> new CustomException(INVALID_USER));
}
Expand Down
38 changes: 0 additions & 38 deletions src/main/java/org/terning/terningserver/service/SignUpService.java

This file was deleted.

0 comments on commit 4b5b4d8

Please sign in to comment.