-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathLoginController.java
More file actions
54 lines (45 loc) · 1.85 KB
/
LoginController.java
File metadata and controls
54 lines (45 loc) · 1.85 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
package roomescape.auth.controller;
import org.springframework.data.crossstore.ChangeSetPersister.NotFoundException;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
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 roomescape.auth.request.LoginRequest;
import roomescape.auth.response.LoginResponse;
import roomescape.auth.service.LoginService;
import roomescape.member.MemberInfo;
@RestController
@RequestMapping("/login")
public class LoginController {
private final LoginService loginService;
public LoginController(LoginService loginService) {
this.loginService = loginService;
}
@PostMapping
public ResponseEntity<Void> login(
@RequestBody LoginRequest loginRequest
) throws NotFoundException {
String token = loginService.login(loginRequest.email(), loginRequest.password());
ResponseCookie cookie = ResponseCookie.from("token", token)
.path("/")
.httpOnly(true)
.build();
return ResponseEntity.ok()
.header("Set-Cookie", cookie.toString())
.build();
}
@GetMapping("/check")
public ResponseEntity<LoginResponse> loginCheck(
@CookieValue(value = "token", required = false) String token
) throws NotFoundException {
if (token == null || token.isBlank()) {
return ResponseEntity.status(401).build();
}
MemberInfo memberInfo = loginService.check(token);
return ResponseEntity.ok(new LoginResponse(memberInfo.name()));
}
}