profileShare

rasmusjy / tasteprint

Read-only snapshot

No repository description.

main default branch 169 files Expires Sep 13, 2026, 9:06 AM
AuthController.java 2,436 bytes
1 package com.tasteprint.account;
2
3 import jakarta.validation.Valid;
4
5 import org.springframework.http.HttpHeaders;
6 import org.springframework.http.HttpStatus;
7 import org.springframework.security.core.annotation.AuthenticationPrincipal;
8 import org.springframework.web.bind.annotation.GetMapping;
9 import org.springframework.web.bind.annotation.DeleteMapping;
10 import org.springframework.web.bind.annotation.PatchMapping;
11 import org.springframework.web.bind.annotation.PostMapping;
12 import org.springframework.web.bind.annotation.RequestBody;
13 import org.springframework.web.bind.annotation.RequestHeader;
14 import org.springframework.web.bind.annotation.RequestMapping;
15 import org.springframework.web.bind.annotation.ResponseStatus;
16 import org.springframework.web.bind.annotation.RestController;
17
18 @RestController
19 @RequestMapping("/api/v1/auth")
20 class AuthController {
21
22 private final AccountService accounts;
23
24 AuthController(AccountService accounts) {
25 this.accounts = accounts;
26 }
27
28 @PostMapping("/register")
29 @ResponseStatus(HttpStatus.CREATED)
30 AuthSession register(@Valid @RequestBody RegisterRequest request) {
31 return accounts.register(request);
32 }
33
34 @PostMapping("/login")
35 AuthSession login(@Valid @RequestBody LoginRequest request) {
36 return accounts.login(request);
37 }
38
39 @PostMapping("/logout")
40 @ResponseStatus(HttpStatus.NO_CONTENT)
41 void logout(@RequestHeader(value = HttpHeaders.AUTHORIZATION, required = false) String authorization) {
42 accounts.logout(bearerToken(authorization));
43 }
44
45 @GetMapping("/me")
46 AccountView me(@AuthenticationPrincipal AuthenticatedUser user) {
47 return accounts.get(user.id());
48 }
49
50 @PatchMapping("/me")
51 AccountView update(@AuthenticationPrincipal AuthenticatedUser user,
52 @Valid @RequestBody UpdateProfileRequest request) {
53 return accounts.update(user.id(), request);
54 }
55
56 @DeleteMapping("/me")
57 @ResponseStatus(HttpStatus.NO_CONTENT)
58 void deleteAccount(@AuthenticationPrincipal AuthenticatedUser user,
59 @Valid @RequestBody DeleteAccountRequest request) {
60 accounts.delete(user.id(), request.password());
61 }
62
63 private String bearerToken(String authorization) {
64 if (authorization == null || !authorization.startsWith("Bearer ")) {
65 return null;
66 }
67 return authorization.substring(7).trim();
68 }
69 }
70