-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthenticationController.java
More file actions
120 lines (110 loc) · 4.27 KB
/
AuthenticationController.java
File metadata and controls
120 lines (110 loc) · 4.27 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
package com.podzilla.auth.controller;
import com.podzilla.auth.dto.LoginRequest;
import com.podzilla.auth.dto.SignupRequest;
import com.podzilla.auth.service.AuthenticationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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 org.springframework.web.bind.annotation.GetMapping;
@RestController
@RequestMapping("/auth")
public class AuthenticationController {
private final AuthenticationService authenticationService;
private static final Logger LOGGER =
LoggerFactory.getLogger(AuthenticationController.class);
@Autowired
public AuthenticationController(
final AuthenticationService authenticationService) {
this.authenticationService = authenticationService;
}
@PostMapping("/login")
@Operation(
summary = "Login",
description = "Logs in a user and generates JWT tokens."
)
@ApiResponse(
responseCode = "200",
description = "User logged in successfully"
)
public ResponseEntity<?> login(
@Valid @RequestBody final LoginRequest loginRequest,
final HttpServletResponse response) {
String email = authenticationService.login(loginRequest, response);
LOGGER.info("User {} logged in", email);
return new ResponseEntity<>(
"User " + email + " logged in successfully",
HttpStatus.OK);
}
@PostMapping("/register")
@Operation(
summary = "Register",
description = "Registers a new user."
)
@ApiResponse(
responseCode = "201",
description = "User registered successfully"
)
public ResponseEntity<?> registerUser(
@Valid @RequestBody final SignupRequest signupRequest) {
authenticationService.registerAccount(signupRequest);
LOGGER.info("User {} registered", signupRequest.getEmail());
return new ResponseEntity<>("Account registered.",
HttpStatus.CREATED);
}
@PostMapping("/logout")
@Operation(
summary = "Logout",
description = "Logs out a user and invalidates JWT tokens."
)
@ApiResponse(
responseCode = "200",
description = "User logged out successfully"
)
public ResponseEntity<?> logoutUser(final HttpServletResponse response) {
authenticationService.logoutUser(response);
return new ResponseEntity<>("You've been signed out!", HttpStatus.OK);
}
@PostMapping("/refresh-token")
@Operation(
summary = "Refresh Token",
description = "Refreshes the JWT tokens for a logged-in user."
)
@ApiResponse(
responseCode = "200",
description = "Token refreshed successfully"
)
public ResponseEntity<?> refreshToken(
final HttpServletRequest request,
final HttpServletResponse response) {
String email = authenticationService.refreshToken(
request, response);
LOGGER.info("User {} refreshed token", email);
return new ResponseEntity<>(
"User " + email + " refreshed tokens successfully",
HttpStatus.OK);
}
@GetMapping("/me")
@Operation(
summary = "Get Current User",
description = "Fetches the details of the currently logged-in user."
)
@ApiResponse(
responseCode = "200",
description = "User details fetched successfully"
)
public void addUserDetailsInHeader(final HttpServletResponse response) {
authenticationService.addUserDetailsInHeader(response);
LOGGER.info("Fetching current user details and adding to header");
}
}