-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthController.java
More file actions
175 lines (146 loc) · 5.92 KB
/
AuthController.java
File metadata and controls
175 lines (146 loc) · 5.92 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package project.flipnote.auth.controller;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import project.flipnote.auth.controller.docs.AuthControllerDocs;
import project.flipnote.auth.model.request.ChangePasswordRequest;
import project.flipnote.auth.model.request.EmailVerificationRequest;
import project.flipnote.auth.model.request.EmailVerifyRequest;
import project.flipnote.auth.model.request.PasswordResetCreateRequest;
import project.flipnote.auth.model.request.PasswordResetRequest;
import project.flipnote.auth.model.request.UserLoginRequest;
import project.flipnote.auth.model.request.UserRegisterRequest;
import project.flipnote.auth.model.response.UserRegisterResponse;
import project.flipnote.auth.model.vo.TokenPair;
import project.flipnote.auth.service.AuthService;
import project.flipnote.common.security.dto.AuthPrinciple;
import project.flipnote.common.security.jwt.JwtConstants;
import project.flipnote.common.security.jwt.JwtProperties;
import project.flipnote.common.util.CookieUtil;
import project.flipnote.user.model.SocialLinksResponse;
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/auth")
public class AuthController implements AuthControllerDocs {
private final AuthService authService;
private final JwtProperties jwtProperties;
private final CookieUtil cookieUtil;
@PostMapping("/register")
public ResponseEntity<UserRegisterResponse> register(@Valid @RequestBody UserRegisterRequest req) {
UserRegisterResponse res = authService.register(req);
return ResponseEntity.status(HttpStatus.CREATED).body(res);
}
@PostMapping("/login")
public ResponseEntity<Void> login(
@Valid @RequestBody UserLoginRequest req
) {
TokenPair tokenPair = authService.login(req);
long accessTokenExpire = jwtProperties.getAccessTokenExpiration().toSeconds();
ResponseCookie accessCookie = cookieUtil.createCookie(
JwtConstants.ACCESS_TOKEN,
tokenPair.accessToken(),
Math.toIntExact(accessTokenExpire)
);
long expirationSeconds = jwtProperties.getRefreshTokenExpiration().toSeconds();
ResponseCookie cookie = cookieUtil.createCookie(
JwtConstants.REFRESH_TOKEN,
tokenPair.refreshToken(),
Math.toIntExact(expirationSeconds)
);
return ResponseEntity.ok()
.header(HttpHeaders.SET_COOKIE, accessCookie.toString())
.header(HttpHeaders.SET_COOKIE, cookie.toString())
.build();
}
@PostMapping("/logout")
public ResponseEntity<Void> logout() {
ResponseCookie expiredCookie = cookieUtil.createExpiredCookie(JwtConstants.REFRESH_TOKEN);
return ResponseEntity.ok()
.header(HttpHeaders.SET_COOKIE, expiredCookie.toString())
.build();
}
@PostMapping("/email-verification/request")
public ResponseEntity<Void> sendEmailVerificationCode(@Valid @RequestBody EmailVerificationRequest req) {
authService.sendEmailVerificationCode(req);
return ResponseEntity.ok().build();
}
@PostMapping("/email-verification")
public ResponseEntity<Void> verifyEmail(
@Valid @RequestBody EmailVerifyRequest req
) {
authService.verifyEmail(req);
return ResponseEntity.ok().build();
}
@PostMapping("/token/refresh")
public ResponseEntity<Void> refreshToken(
@CookieValue(name = JwtConstants.REFRESH_TOKEN) String refreshToken
) {
TokenPair tokenPair = authService.refreshToken(refreshToken);
long accessTokenExpire = jwtProperties.getAccessTokenExpiration().toSeconds();
ResponseCookie accessCookie = cookieUtil.createCookie(
JwtConstants.ACCESS_TOKEN,
tokenPair.accessToken(),
Math.toIntExact(accessTokenExpire)
);
long expirationSeconds = jwtProperties.getRefreshTokenExpiration().toSeconds();
ResponseCookie cookie = cookieUtil.createCookie(
JwtConstants.REFRESH_TOKEN,
tokenPair.refreshToken(),
Math.toIntExact(expirationSeconds)
);
return ResponseEntity.ok()
.header(HttpHeaders.SET_COOKIE, accessCookie.toString())
.header(HttpHeaders.SET_COOKIE, cookie.toString())
.build();
}
@PostMapping("/password-reset/request")
public ResponseEntity<Void> requestPasswordReset(
@Valid @RequestBody PasswordResetCreateRequest req
) {
authService.requestPasswordReset(req);
return ResponseEntity.noContent().build();
}
@PostMapping("/password-reset")
public ResponseEntity<Void> resetPassword(
@Valid @RequestBody PasswordResetRequest req
) {
authService.resetPassword(req);
return ResponseEntity.noContent().build();
}
@PatchMapping("/password")
public ResponseEntity<Void> updatePassword(
@AuthenticationPrincipal AuthPrinciple userAuth,
@Valid @RequestBody ChangePasswordRequest req
) {
authService.changePassword(userAuth.authId(), req);
return ResponseEntity.noContent().build();
}
@GetMapping("/social-links")
public ResponseEntity<SocialLinksResponse> getSocialLinks(
@AuthenticationPrincipal AuthPrinciple userAuth
) {
SocialLinksResponse res = authService.getSocialLinks(userAuth.authId());
return ResponseEntity.ok(res);
}
@DeleteMapping("/social-links/{socialLinkId}")
public ResponseEntity<Void> deleteSocialLink(
@AuthenticationPrincipal AuthPrinciple userAuth,
@PathVariable("socialLinkId") Long socialLinkId
) {
authService.deleteSocialLink(userAuth.authId(), socialLinkId);
return ResponseEntity.noContent().build();
}
}