Skip to content

Commit

Permalink
Merge pull request #86 from Genti2024/fix/oauth-accesstoken-login
Browse files Browse the repository at this point in the history
Fix: 카카오 앱 로그인 후 토큰 로그인 기능 수정
  • Loading branch information
LeeJae-H authored Jul 29, 2024
2 parents 5351b9c + a32f5e7 commit 54a695b
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import com.gt.genti.auth.dto.request.AppleLoginRequest;
import com.gt.genti.auth.dto.request.AppleLoginRequestDto;
import com.gt.genti.auth.dto.request.OauthSignRequestDto;
import com.gt.genti.auth.dto.request.SocialAppLoginRequest;
import com.gt.genti.auth.dto.request.SocialLoginRequestImpl;
import com.gt.genti.auth.dto.request.TokenRefreshRequestDto;
import com.gt.genti.auth.dto.response.OauthJwtResponse;
Expand Down Expand Up @@ -153,9 +153,8 @@ public ResponseEntity<ApiResult<Boolean>> logout(@AuthUser Long userId) {
@PostMapping("/login/oauth2/token")
@Logging(item = LogItem.OAUTH_APP, action = LogAction.LOGIN, requester = LogRequester.ANONYMOUS)
public ResponseEntity<ApiResult<OauthJwtResponse>> loginOrSignUpWithOAuthToken(
@RequestBody @Valid OauthSignRequestDto oauthSignRequestDto) {
return success(authService.appLogin(SocialLoginRequestImpl.of(oauthSignRequestDto.getOauthPlatform(),
oauthSignRequestDto.getToken())));
@RequestBody @Valid SocialAppLoginRequest socialAppLoginRequest) {
return success(authService.appLogin(socialAppLoginRequest));
}

@PostMapping("/reissue")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Schema(name = "[Auth][Anonymous] oauth 토큰으로 로그인or회원가입 처리 요청 dto", description = "Oauth 토큰 및 플랫폼")
public class OauthSignRequestDto {
public class SocialAppLoginRequest {
@NotBlank
@Schema(example = "rhtodaksgdkdyekemf")
String token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gt.genti.auth.dto.request.SocialAppLoginRequest;
import com.gt.genti.auth.dto.request.SocialLoginRequest;
import com.gt.genti.auth.dto.request.TokenRefreshRequestDto;
import com.gt.genti.auth.dto.response.OauthJwtResponse;
Expand Down Expand Up @@ -36,8 +37,8 @@ public SocialLoginResponse webLogin(final SocialLoginRequest request) {
return socialOauthContext.doLogin(request);
}

public OauthJwtResponse appLogin(final SocialLoginRequest request) {
return socialOauthContext.doLogin(request).getToken();
public OauthJwtResponse appLogin(final SocialAppLoginRequest request) {
return socialOauthContext.doAppLogin(request).getToken();
}

public HttpHeaders getOauthRedirect(OauthPlatform oauthPlatform) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gt.genti.auth.dto.request.SocialAppLoginRequest;
import com.gt.genti.auth.dto.request.SocialLoginRequest;
import com.gt.genti.auth.dto.request.SocialLoginRequestImpl;
import com.gt.genti.auth.dto.response.OauthJwtResponse;
import com.gt.genti.auth.dto.response.SocialLoginResponse;
import com.gt.genti.error.ExpectedException;
Expand Down Expand Up @@ -45,7 +47,7 @@ public class AppleOauthStrategy implements SocialLoginStrategy {

@Override
@Transactional
public SocialLoginResponse login(SocialLoginRequest request) {
public SocialLoginResponse webLogin(SocialLoginRequest request) {
AppleUserResponse userResponse = getApplePlatformMember(request.getCode());
Optional<User> findUser = userRepository.findUserBySocialId(userResponse.getPlatformId());
User user;
Expand All @@ -70,11 +72,17 @@ public SocialLoginResponse login(SocialLoginRequest request) {
.userId(user.getId().toString())
.role(user.getUserRole().getRoles())
.build();

OauthJwtResponse oauthJwtResponse = new OauthJwtResponse(jwtTokenProvider.generateAccessToken(tokenGenerateCommand),
jwtTokenProvider.generateRefreshToken(tokenGenerateCommand), user.getUserRole().getStringValue());
return SocialLoginResponse.of(user.getId(), user.getUsername(), user.getEmail(), isNewUser, oauthJwtResponse);
}

@Override
public SocialLoginResponse tokenLogin(SocialAppLoginRequest request) {
return webLogin(SocialLoginRequestImpl.of(request.getOauthPlatform(), request.getToken()));
}

@Override
public boolean support(String provider) {
return provider.equals(OauthPlatform.APPLE.getStringValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gt.genti.auth.dto.request.SocialAppLoginRequest;
import com.gt.genti.auth.dto.request.SocialLoginRequest;
import com.gt.genti.auth.dto.response.OauthJwtResponse;
import com.gt.genti.auth.dto.response.SocialLoginResponse;
Expand Down Expand Up @@ -69,7 +70,7 @@ public String getAuthUri() {

@Override
@Transactional
public SocialLoginResponse login(SocialLoginRequest request) {
public SocialLoginResponse webLogin(SocialLoginRequest request) {
GoogleTokenResponse tokenResponse = googleAuthApiClient.googleAuth(
request.getCode(),
googleClientId,
Expand Down Expand Up @@ -105,6 +106,11 @@ public SocialLoginResponse login(SocialLoginRequest request) {
return SocialLoginResponse.of(user.getId(), user.getUsername(), user.getEmail(), isNewUser, oauthJwtResponse);
}

@Override
public SocialLoginResponse tokenLogin(SocialAppLoginRequest request) {
return null;
}

@Override
public boolean support(String provider) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gt.genti.auth.dto.request.SocialAppLoginRequest;
import com.gt.genti.auth.dto.request.SocialLoginRequest;
import com.gt.genti.auth.dto.response.OauthJwtResponse;
import com.gt.genti.auth.dto.response.SocialLoginResponse;
Expand Down Expand Up @@ -65,24 +66,36 @@ public String getAuthUri() {

@Override
@Transactional
public SocialLoginResponse login(SocialLoginRequest request) {
public SocialLoginResponse webLogin(SocialLoginRequest request) {
KakaoTokenResponse tokenResponse = kakaoAuthApiClient.getOAuth2AccessToken(
"authorization_code",
kakaoClientId,
kakaoClientSecret,
serverBaseUri + ":" + serverPort + kakaoRedirectUri,
request.getCode()
);

OauthPlatform oauthPlatform = request.getOauthPlatform();
String accessToken = tokenResponse.accessToken();
return getUserInfo(oauthPlatform, accessToken);
}

@Override
public SocialLoginResponse tokenLogin(final SocialAppLoginRequest request) {
return getUserInfo(request.getOauthPlatform(), request.getToken());
}

private SocialLoginResponse getUserInfo(OauthPlatform oauthPlatform, String accessToken) {
KakaoUserResponse userResponse = kakaoApiClient.getUserInformation(
"Bearer " + tokenResponse.accessToken());
"Bearer " + accessToken);
Optional<User> findUser = userRepository.findUserBySocialId(userResponse.id());
User user;
boolean isNewUser = false;
if (isNewUser(findUser)) {
User newUser = userRepository.save(User.builderWithSignIn()
.socialId(userResponse.id())
.birthDate(getBirthDateStringFrom(userResponse))
.oauthPlatform(request.getOauthPlatform())
.oauthPlatform(oauthPlatform)
.username(userResponse.kakaoAccount().name())
.nickname(RandomUtil.generateRandomNickname())
.email(userResponse.kakaoAccount().email())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.gt.genti.user.service.social;

import com.gt.genti.auth.dto.request.SocialAppLoginRequest;
import com.gt.genti.auth.dto.request.SocialLoginRequest;
import com.gt.genti.auth.dto.response.SocialLoginResponse;

public interface SocialLoginStrategy {

SocialLoginResponse login(final SocialLoginRequest request);
SocialLoginResponse webLogin(final SocialLoginRequest request);
SocialLoginResponse tokenLogin(final SocialAppLoginRequest request);
boolean support(String provider);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

import org.springframework.stereotype.Component;

import com.gt.genti.error.ExpectedException;
import com.gt.genti.error.ResponseCode;
import com.gt.genti.auth.dto.request.SocialAppLoginRequest;
import com.gt.genti.auth.dto.request.SocialLoginRequest;
import com.gt.genti.auth.dto.response.SocialLoginResponse;
import com.gt.genti.error.ExpectedException;
import com.gt.genti.error.ResponseCode;
import com.gt.genti.user.model.OauthPlatform;

import jakarta.annotation.PostConstruct;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

@Component
Expand Down Expand Up @@ -48,11 +50,14 @@ private SocialAuthStrategy authStrategyOf(OauthPlatform oauthPlatform) {
}

public SocialLoginResponse doLogin(final SocialLoginRequest request) {
return loginStrategyOf(request.getOauthPlatform()).login(request);
return loginStrategyOf(request.getOauthPlatform()).webLogin(request);
}

public String getAuthUri(OauthPlatform oauthPlatform) {
return authStrategyOf(oauthPlatform).getAuthUri();
}

public SocialLoginResponse doAppLogin(@Valid SocialAppLoginRequest request) {
return loginStrategyOf(request.getOauthPlatform()).tokenLogin(request);
}
}

0 comments on commit 54a695b

Please sign in to comment.