-
Notifications
You must be signed in to change notification settings - Fork 2
/
JwtController.java
42 lines (33 loc) · 1.34 KB
/
JwtController.java
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
package com.amorgakco.backend.jwt.controller;
import com.amorgakco.backend.jwt.dto.AccessTokenResponse;
import com.amorgakco.backend.jwt.dto.MemberJwt;
import com.amorgakco.backend.jwt.service.JwtService;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/tokens")
public class JwtController {
private final JwtService jwtService;
private final JwtCookieLoader jwtCookieLoader;
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public AccessTokenResponse reissueAccessToken(
@CookieValue(value = "refresh-token") final String refreshToken,
final HttpServletResponse response) {
final MemberJwt memberJwt = jwtService.reissue(refreshToken);
jwtCookieLoader.loadCookies(response, memberJwt.refreshToken(), memberJwt.accessToken());
return new AccessTokenResponse(memberJwt.accessToken());
}
@DeleteMapping
@ResponseStatus(HttpStatus.NO_CONTENT)
public void logout(@CookieValue(value = "refresh-token") final Cookie cookie) {
jwtService.logout(Optional.ofNullable(cookie));
}
}