Skip to content

Commit

Permalink
Merge pull request #35 from 9oormthon-univ/dev
Browse files Browse the repository at this point in the history
Fix Token
  • Loading branch information
sumin220 authored Nov 22, 2024
2 parents 9a93f4b + f973669 commit 6934218
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.RedirectView;
import univ.yesummit.global.auth.util.JwtUtils;
import univ.yesummit.global.auth.service.AuthService;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/api/kakao")
public class AuthController {

private final JwtUtils jwtUtils;
private final AuthService authService;

@GetMapping("/login")
@Operation(summary = "카카오 로그인", description = "카카오 로그인을 진행합니다.")
Expand All @@ -28,14 +27,9 @@ public RedirectView login() {

@GetMapping("/token")
@Operation(summary = "로그인 후 토큰 요청", description = "로그인 후 accessToken과 refreshToken을 발급합니다.")
public ResponseEntity<Map<String, String>> getToken(@RequestParam Long userId) {
// JWT 토큰 생성
String accessToken = jwtUtils.createAccessToken(userId);
String refreshToken = jwtUtils.createRefreshToken(userId);

Map<String, String> tokens = new HashMap<>();
tokens.put("accessToken", accessToken);
tokens.put("refreshToken", refreshToken);
public ResponseEntity<Map<String, Object>> getToken(@RequestParam Long userId) throws Exception {

Map<String, Object> tokens = authService.generateTokens(userId);

return ResponseEntity.ok(tokens);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package univ.yesummit.global.auth.service;

import java.util.Map;

public interface AuthService {

Map<String, Object> generateTokens(Long userId) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package univ.yesummit.global.auth.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import univ.yesummit.domain.member.service.MemberService;
import univ.yesummit.global.auth.service.AuthService;
import univ.yesummit.global.auth.util.JwtUtils;

import java.util.HashMap;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class AuthServiceImpl implements AuthService {


private final JwtUtils jwtUtils;
private final MemberService memberService;

public Map<String, Object> generateTokens(Long userId) {
// JWT 토큰 생성
String accessToken = jwtUtils.createAccessToken(userId);
String refreshToken = jwtUtils.createRefreshToken(userId);

// 첫 로그인 여부 확인
boolean isFirstLogin = memberService.isFirstLogin(userId);

// 결과 데이터 구성
Map<String, Object> tokens = new HashMap<>();
tokens.put("accessToken", accessToken);
tokens.put("refreshToken", refreshToken);
tokens.put("firstLogin", isFirstLogin);

return tokens;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class OAuth2SuccessHandler implements AuthenticationSuccessHandler {
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
OAuth2Member oAuth2Member = (OAuth2Member) authentication.getPrincipal();
Long memberId = oAuth2Member.getMemberId();
log.info("userId: {}", memberId);

// 첫 로그인 여부 확인
String redirectUrl = "http://localhost:3000/login-success";
Expand Down

0 comments on commit 6934218

Please sign in to comment.