Skip to content

Commit

Permalink
refactor: 카카오 로그인 방식 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Arachneee committed Nov 15, 2024
1 parent 40cb461 commit cf7de2e
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package server.haengdong.application;

import java.net.URI;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.HttpHeaders;
Expand All @@ -22,11 +21,11 @@ public class KakaoClient {
private final KakaoProperties kakaoProperties;
private final RestClient restClient;

public KakaoTokenResponse join(String code) {
public KakaoTokenResponse join(String code, String redirectUri) {
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("grant_type", "authorization_code");
params.add("client_id", kakaoProperties.clientId());
params.add("redirect_uri", kakaoProperties.redirectUri());
params.add("redirect_uri", redirectUri);
params.add("code", code);

try {
Expand All @@ -41,7 +40,7 @@ public KakaoTokenResponse join(String code) {
}
}

public URI getKakaoPageURI() {
return URI.create(kakaoProperties.oauthCodeUri().formatted(kakaoProperties.clientId(), kakaoProperties.redirectUri()));
public String getClientId() {
return kakaoProperties.clientId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import java.net.URI;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import server.haengdong.application.response.KakaoTokenResponse;
Expand All @@ -16,8 +15,8 @@ public class KakaoUserService {
private final UserService userService;
private final KakaoClient kakaoClient;

public Long joinByKakao(String code) {
KakaoTokenResponse kakaoToken = kakaoClient.join(code);
public Long joinByKakao(String code, String redirectUri) {
KakaoTokenResponse kakaoToken = kakaoClient.join(code, redirectUri);
String idToken = kakaoToken.idToken();
DecodedJWT decodedJWT = JWT.decode(idToken);

Expand All @@ -27,7 +26,7 @@ public Long joinByKakao(String code) {
return userService.join(memberNumber, nickname);
}

public URI getRedirectURI() {
return kakaoClient.getKakaoPageURI();
public String getClientId() {
return kakaoClient.getClientId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
public record KakaoProperties(
String baseUri,
String clientId,
String redirectUri,
String tokenRequestUri,
String oauthCodeUri
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package server.haengdong.presentation;

import jakarta.validation.Valid;
import java.net.URI;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
Expand All @@ -21,6 +18,7 @@
import server.haengdong.config.Login;
import server.haengdong.infrastructure.auth.CookieProperties;
import server.haengdong.presentation.request.UserUpdateRequest;
import server.haengdong.presentation.response.KakaoClientId;

@Slf4j
@RequiredArgsConstructor
Expand All @@ -33,38 +31,35 @@ public class UserController {
private final AuthService authService;
private final CookieProperties cookieProperties;

@Value("${login-success.uri}")
private String loginSuccessUri;

@PatchMapping("/api/admin/users")
public ResponseEntity<Void> updateUser(
@Login Long userId,
@Valid @RequestBody UserUpdateRequest request
) {
userService.updateUser(request.toAppRequest(userId));

return ResponseEntity.ok().build();
}

@GetMapping("/api/login/kakao-page")
public ResponseEntity<Void> kakaoPage() {
URI redirectURI = kakaoUserService.getRedirectURI();
@GetMapping("/api/kakao-client-id")
public ResponseEntity<KakaoClientId> kakaoPage() {
String clientId = kakaoUserService.getClientId();
KakaoClientId kakaoClientId = new KakaoClientId(clientId);

return ResponseEntity.status(HttpStatus.MOVED_PERMANENTLY)
.location(redirectURI)
.build();
return ResponseEntity.ok(kakaoClientId);
}

@GetMapping("/api/login/kakao")
public ResponseEntity<Void> kakaoLogin(@RequestParam String code) {
log.info("Kakao login code: {}", code);
Long userId = kakaoUserService.joinByKakao(code);
public ResponseEntity<Void> kakaoLogin(
@RequestParam String code,
@RequestParam("redirect_uri") String redirectUri
) {
log.info("Kakao login code, redirectUri: {}, {}", code, redirectUri);
Long userId = kakaoUserService.joinByKakao(code, redirectUri);
String jwtToken = authService.createGuestToken(userId);

ResponseCookie responseCookie = createResponseCookie(jwtToken);
return ResponseEntity.status(HttpStatus.MOVED_PERMANENTLY)
return ResponseEntity.ok()
.header(HttpHeaders.SET_COOKIE, responseCookie.toString())
.location(URI.create(loginSuccessUri))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package server.haengdong.presentation.response;

public record KakaoClientId(
String clientId
) {
}
4 changes: 0 additions & 4 deletions server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,10 @@ server:

kakao:
base-uri: https://kauth.kakao.com
redirect-uri: http://localhost:8080/api/login/kakao
token-request-uri: /oauth/token
client-id: 52f24834ff7304ed2c47294b3f57b053
oauth-code-uri: https://kauth.kakao.com/oauth/authorize?client_id=%s&redirect_uri=%s&response_type=code&scope=openid

login-success:
uri: https://dev.haengdong.pro/event/create

---

spring:
Expand Down
2 changes: 1 addition & 1 deletion server/src/main/resources/config

0 comments on commit cf7de2e

Please sign in to comment.