Skip to content

Commit

Permalink
feat: 카카오 로그아웃 API 구현 (#70)
Browse files Browse the repository at this point in the history
* feat: OAuthServerTypeConverter 제거 (#69)

* chore: 디렉터리 위치 수정 (#69)

* feat: 로그아웃을 위한 KakaoAccessToken 저장 (#69)

* feat: 카카오 로그아웃 API 구현 (#69)

* refactor: getById 메서드 사용하도록 리팩터링 (#69)
  • Loading branch information
kdkdhoho authored Feb 8, 2024
1 parent 34aeac9 commit cbce572
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.listywave.auth.application.domain.kakao;

import com.listywave.auth.infra.kakao.KakaoOauthApiClient;
import com.listywave.auth.infra.kakao.response.KakaoLogoutResponse;
import com.listywave.auth.infra.kakao.response.KakaoMember;
import com.listywave.auth.infra.kakao.response.KakaoTokenResponse;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -30,4 +31,9 @@ public KakaoTokenResponse requestToken(String authCode) {
public KakaoMember fetchMember(String accessToken) {
return apiClient.fetchKakaoMember("Bearer " + accessToken);
}

public Long logout(String oauthAccessToken) {
KakaoLogoutResponse response = apiClient.logout(oauthAccessToken);
return response.id();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.listywave.auth.presentation.dto;
package com.listywave.auth.application.dto;

import com.listywave.user.application.domain.User;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.listywave.auth.application.domain.JwtManager;
import com.listywave.auth.application.domain.kakao.KakaoOauthClient;
import com.listywave.auth.application.domain.kakao.KakaoRedirectUriProvider;
import com.listywave.auth.application.dto.LoginResponse;
import com.listywave.auth.infra.kakao.response.KakaoMember;
import com.listywave.auth.infra.kakao.response.KakaoTokenResponse;
import com.listywave.auth.presentation.dto.LoginResponse;
import com.listywave.user.application.domain.User;
import com.listywave.user.repository.user.UserRepository;
import java.util.Optional;
Expand Down Expand Up @@ -33,7 +33,11 @@ public LoginResponse login(String authCode) {

Optional<User> foundUser = userRepository.findByOauthId(kakaoMember.id());
if (foundUser.isEmpty()) {
User user = User.initialCreate(kakaoMember.id(), kakaoMember.kakaoAccount().email());
User user = User.initialCreate(
kakaoMember.id(),
kakaoMember.kakaoAccount().email(),
kakaoTokenResponse.accessToken()
);
User createdUser = userRepository.save(user);
return LoginResponse.of(createdUser, true);
}
Expand All @@ -43,4 +47,10 @@ public LoginResponse login(String authCode) {
public String createToken(Long userId) {
return jwtManager.createToken(userId);
}

public void logout(String accessToken) {
Long userId = jwtManager.read(accessToken);
User user = userRepository.getById(userId);
kakaoOauthClient.logout(user.getKakaoAccessToken());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.springframework.http.HttpHeaders.AUTHORIZATION;

import com.listywave.auth.infra.kakao.response.KakaoLogoutResponse;
import com.listywave.auth.infra.kakao.response.KakaoMember;
import com.listywave.auth.infra.kakao.response.KakaoTokenResponse;
import org.springframework.util.MultiValueMap;
Expand All @@ -22,4 +23,7 @@ public interface KakaoOauthApiClient {
contentType = "application/x-www-form-urlencoded;charset=utf-8"
)
KakaoMember fetchKakaoMember(@RequestHeader(name = AUTHORIZATION) String accessToken);

@PostExchange(url = "https://kapi.kakao.com/v1/user/logout")
KakaoLogoutResponse logout(@RequestHeader(value = AUTHORIZATION) String accessToken);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.listywave.auth.infra.kakao.response;

public record KakaoLogoutResponse(
Long id
) {
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.listywave.auth.presentation.controller;
package com.listywave.auth.presentation;

import static org.springframework.http.HttpHeaders.AUTHORIZATION;

import com.listywave.auth.application.dto.LoginResponse;
import com.listywave.auth.application.service.AuthService;
import com.listywave.auth.presentation.dto.LoginResponse;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

Expand Down Expand Up @@ -38,4 +42,10 @@ ResponseEntity<LoginResponse> login(
response.setHeader(HttpHeaders.SET_COOKIE, accessToken);
return ResponseEntity.ok(loginResponse);
}

@PatchMapping("/auth/kakao")
ResponseEntity<Void> logout(@RequestHeader(value = AUTHORIZATION) String accessToken) {
authService.logout(accessToken);
return ResponseEntity.noContent().build();
}
}
7 changes: 0 additions & 7 deletions src/main/java/com/listywave/common/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.listywave.common.config;

import com.listywave.auth.config.OAuthServerTypeConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.HttpMethod;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
Expand All @@ -29,9 +27,4 @@ public void addCorsMappings(CorsRegistry registry) {
.allowCredentials(true)
.exposedHeaders("*");
}

@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new OAuthServerTypeConverter());
}
}
8 changes: 6 additions & 2 deletions src/main/java/com/listywave/user/application/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ public class User extends BaseEntity {
@Column(nullable = false, length = 5)
private Boolean allPrivate;

public static User initialCreate(Long oauthId, String oauthEmail) {
@Column(nullable = false, length = 200)
private String kakaoAccessToken;

public static User initialCreate(Long oauthId, String oauthEmail, String kakaoAccessToken) {
return new User(
oauthId,
oauthEmail,
Expand All @@ -60,7 +63,8 @@ public static User initialCreate(Long oauthId, String oauthEmail) {
new Description(""),
0,
0,
false
false,
kakaoAccessToken
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.listywave.user.application.service;

import static com.listywave.common.exception.ErrorCode.NOT_FOUND;

import com.listywave.auth.application.domain.JwtManager;
import com.listywave.common.exception.CustomException;
import com.listywave.common.util.UserUtil;
import com.listywave.list.application.domain.CategoryType;
import com.listywave.list.application.domain.Lists;
Expand All @@ -16,7 +13,6 @@
import com.listywave.user.repository.follow.FollowRepository;
import com.listywave.user.repository.user.UserRepository;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -32,18 +28,17 @@ public class UserService {

@Transactional(readOnly = true)
public UserInfoResponse getUserInfo(Long userId, String accessToken) {
Optional<User> found = userRepository.findById(userId);
User foundUser = found.orElseThrow(() -> new CustomException(NOT_FOUND));
User user = userRepository.getById(userId);

if (isSignedIn(accessToken)) {
return UserInfoResponse.of(foundUser, false, false);
return UserInfoResponse.of(user, false, false);
}

Long loginUserId = jwtManager.read(accessToken);
if (foundUser.isSame(loginUserId)) {
return UserInfoResponse.of(foundUser, false, true);
if (user.isSame(loginUserId)) {
return UserInfoResponse.of(user, false, true);
}
return UserInfoResponse.of(foundUser, false, false);
return UserInfoResponse.of(user, false, false);
}

private boolean isSignedIn(String accessToken) {
Expand Down

0 comments on commit cbce572

Please sign in to comment.