Skip to content

Commit

Permalink
Merge pull request #21 from dnd-side-project/refactor/scope-main-api
Browse files Browse the repository at this point in the history
refactor : 구글, 카카오 로그인 Controller 통합
  • Loading branch information
1000kkannoo authored Aug 17, 2023
2 parents 35283eb + 7be26eb commit fc80c89
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 51 deletions.
32 changes: 9 additions & 23 deletions src/docs/asciidoc/api/user/user.adoc
Original file line number Diff line number Diff line change
@@ -1,40 +1,26 @@
=== 소셜로그인 API

==== 카카오 로그인
[source,http,options="nowrap"]
----
1. 카카오 로그인 페이지
- 카카오 로그인 페이지
https://kauth.kakao.com/oauth/authorize?client_id=e372da97369cf835ea1691792daa9370&redirect_uri=http://ec2-52-78-18-124.ap-northeast-2.compute.amazonaws.com/login/kakao&response_type=code
2. 카카오 로그인 RedirectURL
GET -> /login/kakao
3. 로그인 성공시 RedirectURL로 자동 이동
----

==== 구글 로그인
[source,http,options="nowrap"]
----
1. 구글 로그인 페이지
- 구글 로그인 페이지
https://accounts.google.com/o/oauth2/v2/auth?client_id=848453248473-levss4uvgkrum3h46knam82hlv83phvr.apps.googleusercontent.com&redirect_uri=http://ec2-52-78-18-124.ap-northeast-2.compute.amazonaws.com/login/google&response_type=code&scope=email%20profile%20openid&access_type=offline
2. 구글 로그인 RedirectURL
GET -> /login/google
1. 소셜 로그인 RedirectURL
GET -> /auth/signin
3. 로그인 성공시 RedirectURL로 자동 이동
2. 로그인 성공시 RedirectURL로 자동 이동
----

==== HTTP Request QueryParameter
include::{snippets}/login-by-kakao/http-request.adoc[]
[source,http,options="nowrap"]
----
GET /login/google?code=4%252F0AdEu5BVdKyrEElZENWgSJOrJSHUeAjsSJHvWUSi237TQ13FqUfqPOa-ZcES6lGID7DmVaJwSig HTTP/1.1
----
include::{snippets}/login-by-kakao/query-parameters.adoc[]
include::{snippets}/login-by-social/http-request.adoc[]
include::{snippets}/login-by-social/query-parameters.adoc[]

==== HTTP Response Body
include::{snippets}/login-by-kakao/http-response.adoc[]
include::{snippets}/login-by-kakao/response-fields.adoc[]
include::{snippets}/login-by-social/http-response.adoc[]
include::{snippets}/login-by-social/response-fields.adoc[]

=== 관심분야 추가 요청 API

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dnd/project/domain/user/config/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

@Getter
public enum Platform {
NAVER, GOOGLE, KAKAO;
GOOGLE, KAKAO;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dnd.project.domain.user.controller;

import dnd.project.domain.user.config.Platform;
import dnd.project.domain.user.request.controller.UserRequest;
import dnd.project.domain.user.response.UserResponse;
import dnd.project.domain.user.service.UserService;
Expand All @@ -9,45 +10,39 @@
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import static dnd.project.domain.user.config.Platform.GOOGLE;
import static dnd.project.domain.user.config.Platform.KAKAO;

@RestController
@RequiredArgsConstructor
@RequestMapping("/auth")
public class UserController {

private final UserService userService;

// 카카오 로그인 API
@GetMapping("/login/kakao")
public CustomResponseEntity<UserResponse.Login> loginByKakao(@RequestParam String code) {
return CustomResponseEntity.success(userService.loginByOAuth(code, KAKAO));
}

// 구글 로그인 API
@GetMapping("/login/google")
public CustomResponseEntity<UserResponse.Login> loginByGoogle(@RequestParam String code) {
return CustomResponseEntity.success(userService.loginByOAuth(code, GOOGLE));
// 소셜 로그인 API
@GetMapping("/signin")
public CustomResponseEntity<UserResponse.Login> loginByOAuth(
@RequestParam String code, @RequestParam Platform platform
) {
return CustomResponseEntity.success(userService.loginByOAuth(code, platform));
}

// 관심분야 추가 요청 API
@PostMapping("/auth")
@PostMapping("")
public CustomResponseEntity<Void> addInterests(
@AuthenticationPrincipal Long userId, UserRequest.Interests request
) {
return CustomResponseEntity.success(userService.addInterests(userId, request.toServiceRequest()));
}

// 내 프로필 조회하기 API
@GetMapping("/auth")
@GetMapping("")
public CustomResponseEntity<UserResponse.Detail> detailUser(
@AuthenticationPrincipal Long userId
) {
return CustomResponseEntity.success(userService.detailUser(userId));
}

// 내 정보 수정하기 API
@PatchMapping("/auth")
@PatchMapping("")
public CustomResponseEntity<UserResponse.Detail> updateUser(
@RequestBody @Valid UserRequest.Update request, @AuthenticationPrincipal Long userId
) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/dnd/project/global/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
Expand Down Expand Up @@ -52,7 +51,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.authorizeHttpRequests((request) -> request
.requestMatchers(
"/api/v1/version", "/auth","/auth/admin",
"/login/kakao", "/login/google", "/docs/*",
"/auth/signin", "/docs/*",
"/lectures","/lectures/scope",
"/review/recent","/review/keyword"
)
Expand Down
11 changes: 7 additions & 4 deletions src/test/java/dnd/project/docs/user/UserControllerDocsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected Object initController() {
return new UserController(userService);
}

@DisplayName("카카오 로그인 API")
@DisplayName("소셜 로그인 API")
@Test
void loginByKakao() throws Exception {
// given
Expand All @@ -57,16 +57,19 @@ void loginByKakao() throws Exception {

// when // then
mockMvc.perform(
RestDocumentationRequestBuilders.get("/login/kakao")
RestDocumentationRequestBuilders.get("/auth/signin")
.param("code", "4%2F0AdEu5BVdKyrEElZENWgSJOrJSHUeAjsSJHvWUSi237TQ13FqUfqPOa-ZcES6lGID7DmVaJwSig")
.param("platform","KAKAO")
)
.andDo(print())
.andExpect(status().isOk())
.andDo(document("login-by-kakao",
.andDo(document("login-by-social",
preprocessResponse(prettyPrint()),
queryParameters(
parameterWithName("code")
.description("소셜 로그인 후 발급된 인가코드")
.description("소셜 로그인 후 발급된 인가코드"),
parameterWithName("platform")
.description("'KAKAO' / 'GOOGLE'")
),
responseFields(
fieldWithPath("code").type(NUMBER)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package dnd.project.domain.user.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import dnd.project.domain.ControllerTestSupport;
import dnd.project.domain.user.request.controller.UserRequest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import java.util.List;
Expand All @@ -17,14 +14,15 @@

class UserControllerTest extends ControllerTestSupport {

@DisplayName("카카오 로그인 API")
@DisplayName("소셜 로그인 API")
@Test
void loginByKakao() throws Exception {
// given
// when // then
mockMvc.perform(
MockMvcRequestBuilders.get("/login/kakao")
MockMvcRequestBuilders.get("/auth/signin")
.param("code", "4%2F0AdEu5BVdKyrEElZENWgSJOrJSHUeAjsSJHvWUSi237TQ13FqUfqPOa-ZcES6lGID7DmVaJwSig")
.param("platform","KAKAO")
)
.andDo(print())
.andExpect(status().isOk());
Expand Down

0 comments on commit fc80c89

Please sign in to comment.