This repository was archived by the owner on Jul 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthController.java
More file actions
82 lines (66 loc) · 3.82 KB
/
AuthController.java
File metadata and controls
82 lines (66 loc) · 3.82 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
package com.zenfulcode.commercify.api.auth;
import com.zenfulcode.commercify.api.auth.dto.request.LoginRequest;
import com.zenfulcode.commercify.api.auth.dto.request.RefreshTokenRequest;
import com.zenfulcode.commercify.api.auth.dto.request.RegisterRequest;
import com.zenfulcode.commercify.api.auth.dto.response.AuthResponse;
import com.zenfulcode.commercify.api.auth.dto.response.NextAuthResponse;
import com.zenfulcode.commercify.auth.application.service.AuthenticationApplicationService;
import com.zenfulcode.commercify.auth.application.service.AuthenticationResult;
import com.zenfulcode.commercify.auth.domain.exception.InvalidAuthenticationException;
import com.zenfulcode.commercify.auth.domain.model.AuthenticatedUser;
import com.zenfulcode.commercify.shared.interfaces.ApiResponse;
import com.zenfulcode.commercify.user.application.service.UserApplicationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@Slf4j
@RestController
@RequestMapping("/api/v2/auth")
@RequiredArgsConstructor
public class AuthController {
private final AuthenticationApplicationService authService;
private final UserApplicationService userService;
@PostMapping("/nextauth")
public ResponseEntity<ApiResponse<NextAuthResponse>> nextAuthSignIn(@RequestBody LoginRequest request) {
log.info("Next auth request: {}", request);
// Authenticate through the application service
AuthenticationResult result = authService.authenticate(request.toCommand());
// Create and return the NextAuth response
return ResponseEntity.ok(ApiResponse.success(NextAuthResponse.from(result)));
}
@GetMapping("/session")
public ResponseEntity<ApiResponse<NextAuthResponse>> validateSession(@RequestHeader("Authorization") String authHeader) {
try {
// Extract token using a domain service method
String token = authService.extractTokenFromHeader(authHeader).orElseThrow(() -> new InvalidAuthenticationException("Invalid authorization header"));
// Validate token through the application service
AuthenticatedUser user = authService.validateAccessToken(token);
// Create and return the NextAuth session response
return ResponseEntity.ok(ApiResponse.success(NextAuthResponse.fromUser(user)));
} catch (InvalidAuthenticationException e) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
}
}
@PostMapping("/signin")
public ResponseEntity<ApiResponse<AuthResponse>> login(@RequestBody LoginRequest request) {
AuthenticationResult result = authService.authenticate(request.toCommand());
AuthResponse response = AuthResponse.from(result);
return ResponseEntity.ok(ApiResponse.success(response));
}
@PostMapping("/signup")
public ResponseEntity<ApiResponse<AuthResponse>> register(@RequestBody RegisterRequest request) {
userService.registerUser(request.firstName(), request.lastName(), request.email(), request.password(), request.phone());
// Authenticate the newly registered user
AuthenticationResult result = authService.authenticate(new LoginRequest(request.email(), request.password(), false).toCommand());
AuthResponse response = AuthResponse.from(result);
return ResponseEntity.ok(ApiResponse.success(response));
}
@PostMapping("/refresh")
public ResponseEntity<ApiResponse<AuthResponse>> refreshToken(@RequestBody RefreshTokenRequest request) {
AuthenticationResult result = authService.refreshToken(request.refreshToken());
AuthResponse response = AuthResponse.from(result);
return ResponseEntity.ok(ApiResponse.success(response));
}
}