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

[fix] 로그인 api response 오류 해결 #16

Merged
merged 7 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
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 @@ -9,7 +9,7 @@
@Builder
@Getter
public class NaverAccessToken {
private static final String TOKEN_TYPE = "Bearer ";
private static final String TOKEN_TYPE = "";
private String accessToken;

public static NaverAccessToken of(String accessToken){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;

@FeignClient(name = "naver-feign-client", url = "https://openapi.naver.com")
@FeignClient(name = "naver-feign-client", url = "https://openapi.naver.com/v1/nid/me")
public interface NaverFeignClient {
@GetMapping
NaverAccessTokenInfo getNaverAccessTokenInfo(@RequestHeader("Authorization") String accessToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public class AuthController {
@PostMapping("/signIn")
public ResponseEntity<BaseResponse<?>> signIn(@RequestHeader("Authorization") final String token,
@RequestBody final UserAuthRequestDto userAuthRequestDto){

final UserAuthResponseDto responseDto = authService.signIn(token, userAuthRequestDto);
return ResponseEntity.status(HttpStatus.OK)
.body(BaseResponse.of(SuccessCode.OK, responseDto));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ public class User {
@Enumerated(value = EnumType.STRING)
private Platform platform;
private String platformId;
private String userName;

public static User createUser(Platform platform, String platformId){
public static User createUser(Platform platform, String platformId, String userName){
return User.builder()
.platform(platform)
.platformId(platformId)
.userName(userName)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
@Builder
@Getter
public class UserAuthResponseDto {
private Long userId;
private String userName;
private String accessToken;
private String refreshToken;
private Long userId;

public static UserAuthResponseDto of(Token token, User user) {
return UserAuthResponseDto.builder()
.accessToken(token.getAccessToken())
.refreshToken(token.getRefreshToken())
.userId(user.getId())
.userName(user.getUserName())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
import com.kusithm.hdmedi_server.domain.user.repository.UserRepository;
import com.kusithm.hdmedi_server.global.config.jwt.JwtProvider;
import com.kusithm.hdmedi_server.global.config.jwt.Token;
import com.kusithm.hdmedi_server.global.error.exception.EntityNotFoundException;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Optional;

import static com.kusithm.hdmedi_server.domain.user.domain.RefreshToken.createRefreshToken;
import static com.kusithm.hdmedi_server.global.error.exception.ErrorCode.USER_NOT_FOUND;

@RequiredArgsConstructor
@Transactional
Expand All @@ -26,7 +28,7 @@ public class AuthService {
private final UserRepository userRepository;
private final JwtProvider jwtProvider;

public UserAuthResponseDto signIn(String token, UserAuthRequestDto requestDto){
public UserAuthResponseDto signIn(String token, UserAuthRequestDto requestDto) {
Platform platform = Platform.getEnumPlatformFrom(requestDto.getPlatform());
String platformId = getPlatformId(token);
User findUser = getUser(platform, platformId);
Expand All @@ -35,26 +37,26 @@ public UserAuthResponseDto signIn(String token, UserAuthRequestDto requestDto){
return UserAuthResponseDto.of(issuedToken, findUser);
}

private User getUser(Platform platform, String platformId){
private User getUser(Platform platform, String platformId) {
return userRepository.findUserByPlatformAndPlatformId(platform, platformId)
.orElseGet(() -> saveUser(platform, platformId));
.orElseThrow(() -> new EntityNotFoundException(USER_NOT_FOUND));
}

private User saveUser(Platform platform, String platformId){
User createdUser = User.createUser(platform, platformId);
private User saveUser(Platform platform, String platformId, String userName) {
User createdUser = User.createUser(platform, platformId, userName);
userRepository.save(createdUser);
return createdUser;
}

private String getPlatformId(String token){
private String getPlatformId(String token) {
return naverOAuthProvider.getNaverPlatformId(token);
}

private Token issueAccessTokenAndRefreshToken(User user){
private Token issueAccessTokenAndRefreshToken(User user) {
return jwtProvider.issueToken(user.getId());
}

private void updateRefreshToken(String refreshToken, User user){
private void updateRefreshToken(String refreshToken, User user) {
refreshTokenRepository.save(createRefreshToken(user.getId(), refreshToken));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public enum ErrorCode {
* 404 Not Found
*/
ENTITY_NOT_FOUND(HttpStatus.NOT_FOUND, "엔티티를 찾을 수 없습니다."),
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "유저를 찾을 수 없습니다."),

/**
* 405 Method Not Allowed
Expand Down