-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocialLoginController.java
More file actions
70 lines (60 loc) · 2.79 KB
/
SocialLoginController.java
File metadata and controls
70 lines (60 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package NextLevel.demo.user.controller;
import NextLevel.demo.common.SuccessResponse;
import NextLevel.demo.user.auth.GoogleService;
import NextLevel.demo.user.auth.KakaoService;
import NextLevel.demo.user.auth.NaverService;
import NextLevel.demo.user.entity.UserDetailEntity;
import NextLevel.demo.util.jwt.JWTUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Slf4j
@RequiredArgsConstructor
@Controller
@RequestMapping("/public/auth")
public class SocialLoginController {
private final KakaoService kakaoService;
private final NaverService naverService;
private final GoogleService googleService;
private final JWTUtil jwtUtil;
@GetMapping("/google")
public ResponseEntity<?> googleSocial(@RequestParam("code") String code, HttpServletRequest request, HttpServletResponse response) {
try {
return total(googleService.getUserFromGoogle(code), request, response);
}catch (RuntimeException e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}
}
@GetMapping("/kakao")
public ResponseEntity<?> kakaoSocial(@RequestParam("code") String code, HttpServletRequest request, HttpServletResponse response) {
try {
return total(kakaoService.login(code), request, response);
}catch (RuntimeException e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}
}
@GetMapping("/naver")
public ResponseEntity<?> naverSocial(@RequestParam("code") String code, HttpServletRequest request, HttpServletResponse response) {
try {
return total(naverService.login(code), request, response);
}catch (RuntimeException e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}
}
private ResponseEntity<?> total(UserDetailEntity userDetail, HttpServletRequest request, HttpServletResponse response) {
jwtUtil.addRefresh(response, userDetail.getUserId(), userDetail.getUUID());
jwtUtil.addAccess(response, userDetail.getUserId(), request, userDetail.getUser().getRole());
log.info("social login success");
return ResponseEntity.status(HttpStatus.OK).body(new SuccessResponse("social login success", null));
}
}