Skip to content

Commit

Permalink
Merge pull request #69 from Gyuhyeok99/refactor/#68
Browse files Browse the repository at this point in the history
[refactor/#68] 카카오 로그인 수정
  • Loading branch information
Gyuhyeok99 authored Jun 20, 2024
2 parents 71d1a9b + 962b5c3 commit fb0f951
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package carbonneutral.academy.api.controller.auth;

import carbonneutral.academy.api.controller.auth.dto.request.PostLoginReq;
import carbonneutral.academy.api.controller.auth.dto.response.PostSocialRes;
import carbonneutral.academy.api.service.auth.AuthService;
import carbonneutral.academy.common.BaseResponse;
Expand All @@ -8,6 +9,7 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static carbonneutral.academy.common.code.status.SuccessStatus.OAUTH_OK;
Expand All @@ -27,12 +29,11 @@ public BaseResponse<String> health() {
return BaseResponse.onSuccess("I'm healthy");
}

@PostMapping("/{socialType}/login")
@PostMapping("/login")
@Operation(summary = "소셜 로그인 API",description = "소셜 로그인을 진행합니다.")
public BaseResponse<PostSocialRes> login(@PathVariable(name="socialType") String socialLoginPath,
@RequestParam("code") String code) {
SocialType socialType = SocialType.valueOf(socialLoginPath.toUpperCase());
return BaseResponse.of(OAUTH_OK, authService.socialLogin(socialType, code));
public BaseResponse<PostSocialRes> login(@Validated @RequestBody PostLoginReq postLoginReq) {
SocialType socialType = SocialType.valueOf(postLoginReq.getSocialType().toUpperCase());
return BaseResponse.of(OAUTH_OK, authService.socialLogin(postLoginReq.getCode(), socialType));
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package carbonneutral.academy.api.controller.auth.dto.request;

import carbonneutral.academy.domain.user.enums.SocialType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@Builder
@AllArgsConstructor
public class PostLoginReq {

private String socialType;
private String code;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public interface AuthService {

PostSocialRes socialLogin(SocialType socialType, String authorizationCode);
PostSocialRes socialLogin(String code, SocialType socialType);


}
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ public class AuthServiceImpl implements AuthService {

@Override
@Transactional
public PostSocialRes socialLogin(SocialType socialType, String authorizationCode) {
public PostSocialRes socialLogin(String code, SocialType socialType) {
switch (socialType){
case KAKAO: {
GetKakaoRes getKakaoRes = kakaoLoginService.getUserInfo(kakaoLoginService.getAccessToken(authorizationCode));
GetKakaoRes getKakaoRes = kakaoLoginService.getUserInfo(code);

boolean isRegistered = userJpaRepository.existsByUsernameAndSocialTypeAndState(getKakaoRes.getId(), SocialType.KAKAO, ACTIVE);

if (!isRegistered) {
User user = AuthConverter.toUser(getKakaoRes);
userJpaRepository.save(user);
pointJpaRepository.save(AuthConverter.toPoint(user));
useStatisticsJpaRepository.save(AuthConverter.toUseStatistics(user));
userJpaRepository.save(user);
}
User user = userJpaRepository.findByUsernameAndState(getKakaoRes.getId(), ACTIVE)
.orElseThrow(() -> new BaseException(NOT_FIND_USER));
Expand Down

0 comments on commit fb0f951

Please sign in to comment.