Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] 로그인 API 코드 수정 #41

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ ResponseEntity<SuccessResponse<?>> kakaoLogin(
@Operation(summary = "카카오 로그아웃")
@PostMapping("/api/oauth/kakao/logout")
ResponseEntity<SuccessResponse<?>> kakaoLogout(
@RequestHeader("Authorization") String accessToken,
@Parameter(
description = "멤버 아이디",
description = "authorization token에서 얻은 userId, 임의입력하면 대체됩니다.",
required = true,
example = "1"
)
@RequestBody String memberId
example = "12345"
) @UserId Long userId
);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.sopt.sweet.domain.member.dto.response.MemberTokenResponseDto;
import org.sopt.sweet.domain.member.service.OAuthService;
import org.sopt.sweet.global.common.SuccessResponse;
import org.sopt.sweet.global.config.auth.UserId;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -21,7 +22,7 @@ public class OAuthController implements OAuthApi {
@PostMapping("/kakao/login")
public ResponseEntity<SuccessResponse<?>> kakaoLogin(@RequestParam("code") String code) {
KakaoUserInfoResponseDto userInfo = oAuthService.kakaoCallback(code);
MemberTokenResponseDto memberToken = oAuthService.saveToken(userInfo.socialId());
MemberTokenResponseDto memberToken = oAuthService.saveToken(userInfo.memberId());

Map<String, Object> loginResponse = new HashMap<>();
loginResponse.put("userInfo", userInfo);
Expand All @@ -32,8 +33,8 @@ public ResponseEntity<SuccessResponse<?>> kakaoLogin(@RequestParam("code") Strin


@PostMapping("/kakao/logout")
public ResponseEntity<SuccessResponse<?>> kakaoLogout(@RequestHeader("Authorization") String accessToken,@RequestBody String socialId) {
oAuthService.kakaoLogout(accessToken, socialId);
public ResponseEntity<SuccessResponse<?>> kakaoLogout(@UserId Long userId) {
oAuthService.kakaoLogout(userId);
return SuccessResponse.ok("로그아웃 성공");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ private String issueNewRefreshToken(Long memberId) {
@Value("${spring.security.oauth2.client.registration.kakao.redirect-uri}")
private String redirectUri;

@Value("${jwt.refresh-token-expire-time}")
private long REFRESH_TOKEN_EXPIRE_TIME;

private final ObjectMapper objectMapper;
private final RedisTemplate<String, String> redisTemplate;

Expand Down Expand Up @@ -122,6 +125,7 @@ public KakaoUserInfoResponseDto saveMember(Long socialId, String nickname, Strin
return new KakaoUserInfoResponseDto(existMember.getId(), socialId, nickname, profileImage);
}


// 카카오 로그인 시 토큰 저장
public MemberTokenResponseDto saveToken(Long memberId) {
String refreshToken = null;
Expand All @@ -134,7 +138,7 @@ public MemberTokenResponseDto saveToken(Long memberId) {
refreshToken = storedRefreshToken;
} else {
refreshToken = issueNewRefreshToken(memberId);
redisTemplate.opsForValue().set(redisKey, refreshToken, 7, TimeUnit.DAYS);
redisTemplate.opsForValue().set(redisKey, refreshToken, REFRESH_TOKEN_EXPIRE_TIME, TimeUnit.SECONDS);
}

System.out.println("카카오 로그인 성공 memberId: " + memberId + " accessToken :" + accessToken + " refreshToken: " + refreshToken);
Expand All @@ -143,33 +147,18 @@ public MemberTokenResponseDto saveToken(Long memberId) {
}


public void kakaoLogout(String accessToken, String memberId) {
RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", accessToken);
public void kakaoLogout(Long memberId) {

HttpEntity<String> request = new HttpEntity<>(headers);
String redisKey = "RT:" + memberId;
redisTemplate.delete(redisKey);

try {
restTemplate.exchange(
"https://kapi.kakao.com/v1/user/logout",
HttpMethod.POST,
request,
String.class
);
System.out.println("카카오 로그아웃 성공 :" + memberId);
}

String redisKey = "RT:" + memberId;
redisTemplate.delete(redisKey);
}

System.out.println("카카오 로그아웃 성공");
} catch (HttpClientErrorException e) {
System.err.println("Kakao API 요청 실패. 응답 코드: " + e.getRawStatusCode() + ", 응답 내용: " + e.getResponseBodyAsString());
}
}


}