From 852346ade3dbcb85e6a75317190cb1af0d1ea0c1 Mon Sep 17 00:00:00 2001 From: YoungJun Park Date: Mon, 4 Oct 2021 00:17:38 +0900 Subject: [PATCH] =?UTF-8?q?[#4]=20Kakao=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20?= =?UTF-8?q?=EC=9A=94=EC=B2=AD=20API=20=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lazy/api/controller/OAuthController.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 api/src/main/java/com/teamnexters/lazy/api/controller/OAuthController.java diff --git a/api/src/main/java/com/teamnexters/lazy/api/controller/OAuthController.java b/api/src/main/java/com/teamnexters/lazy/api/controller/OAuthController.java new file mode 100644 index 0000000..11f6bc2 --- /dev/null +++ b/api/src/main/java/com/teamnexters/lazy/api/controller/OAuthController.java @@ -0,0 +1,65 @@ +package com.teamnexters.lazy.api.controller; + +import com.teamnexters.lazy.api.config.auth.jwt.JwtTokenProvider; +import com.teamnexters.lazy.api.config.auth.jwt.Token; +import com.teamnexters.lazy.api.domain.oauthDto.KakaoProfileDto; +import com.teamnexters.lazy.api.domain.oauthDto.KakaoTokenDto; +import com.teamnexters.lazy.api.exception.MemberNotFoundException; +import com.teamnexters.lazy.api.service.KakaoService; +import com.teamnexters.lazy.common.domain.member.Member; +import com.teamnexters.lazy.common.domain.member.MemberRepository; +import com.teamnexters.lazy.common.domain.member.Provider; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@Slf4j +@Tag(name = "OAuth 소셜로그인", description = "소셜로그인 관련 API") +@RestController +@AllArgsConstructor +public class OAuthController { + + private KakaoService kakaoService; + private MemberRepository memberRepository; + private JwtTokenProvider jwtTokenProvider; + + @Operation(summary = "✅ Kakao Callback - AccessToken 받아오기", + description = "카카오 로그인 후 코드로 AccessToken 을 받아옵니다.", + responses = { + @ApiResponse( + responseCode = "200", description = "[Ok] GET AccessToken", + content = @Content(schema = @Schema(implementation = String.class)))}) + @GetMapping(value = "/login/oauth2/code/kakao") + public String kakaoLogin(@RequestParam("code") String code) { + KakaoTokenDto kakaoToken = kakaoService.getKakaoTokenInfo(code); + log.info(">>> Callback : Kakao Token = {}", kakaoToken); + return kakaoToken.getAccessToken(); + } + + @Operation(summary = "✅ 소셜로그인 요청 API", + description = "소셜로그인 토큰으로 밍굴맹굴 로그인을 시도합니다.", + responses = { + @ApiResponse( + responseCode = "200", description = "[Ok] Social Login complete", + content = @Content(schema = @Schema(implementation = Token.class)))}) + @GetMapping(value = "api/v1/oauth/sign-in/kakao") + public ResponseEntity signInByProvider( + @Parameter(description = "소셜 Access Token", required = true) @RequestBody String accessToken) { + + KakaoProfileDto profile = kakaoService.getKakaoProfile(accessToken); + + Integer oauthId = Math.toIntExact(profile.getId()); + Member member = memberRepository.findByOauthIdAndProvider(oauthId, Provider.KAKAO) + .orElseThrow(() -> new MemberNotFoundException(String.valueOf(oauthId))); + return ResponseEntity.ok().body(jwtTokenProvider.createToken(String.valueOf(member.getMemIdx()), member.getRole())); + } + + +}