|
| 1 | +package com.zenfulcode.commercify.api.auth; |
| 2 | + |
| 3 | +import com.zenfulcode.commercify.api.auth.dto.request.LoginRequest; |
| 4 | +import com.zenfulcode.commercify.api.auth.dto.request.RefreshTokenRequest; |
| 5 | +import com.zenfulcode.commercify.api.auth.dto.request.RegisterRequest; |
| 6 | +import com.zenfulcode.commercify.api.auth.dto.response.AuthResponse; |
| 7 | +import com.zenfulcode.commercify.api.auth.dto.response.NextAuthResponse; |
| 8 | +import com.zenfulcode.commercify.auth.application.service.AuthenticationApplicationService; |
| 9 | +import com.zenfulcode.commercify.auth.application.service.AuthenticationResult; |
| 10 | +import com.zenfulcode.commercify.auth.domain.exception.InvalidAuthenticationException; |
| 11 | +import com.zenfulcode.commercify.auth.domain.model.AuthenticatedUser; |
| 12 | +import com.zenfulcode.commercify.shared.interfaces.ApiResponse; |
| 13 | +import com.zenfulcode.commercify.user.application.service.UserApplicationService; |
| 14 | +import lombok.RequiredArgsConstructor; |
| 15 | +import lombok.extern.slf4j.Slf4j; |
| 16 | +import org.springframework.http.HttpStatus; |
| 17 | +import org.springframework.http.ResponseEntity; |
| 18 | +import org.springframework.web.bind.annotation.*; |
| 19 | + |
| 20 | +@Slf4j |
| 21 | +@RestController |
| 22 | +@RequestMapping("/api/v2/auth") |
| 23 | +@RequiredArgsConstructor |
| 24 | +public class AuthController { |
| 25 | + private final AuthenticationApplicationService authService; |
| 26 | + private final UserApplicationService userService; |
| 27 | + |
| 28 | + @PostMapping("/nextauth") |
| 29 | + public ResponseEntity<ApiResponse<NextAuthResponse>> nextAuthSignIn(@RequestBody LoginRequest request) { |
| 30 | + log.info("Next auth request: {}", request); |
| 31 | + |
| 32 | + // Authenticate through the application service |
| 33 | + AuthenticationResult result = authService.authenticate(request.toCommand()); |
| 34 | + |
| 35 | + // Create and return the NextAuth response |
| 36 | + return ResponseEntity.ok(ApiResponse.success(NextAuthResponse.from(result))); |
| 37 | + } |
| 38 | + |
| 39 | + @GetMapping("/session") |
| 40 | + public ResponseEntity<ApiResponse<NextAuthResponse>> validateSession(@RequestHeader("Authorization") String authHeader) { |
| 41 | + try { |
| 42 | + // Extract token using a domain service method |
| 43 | + String token = authService.extractTokenFromHeader(authHeader).orElseThrow(() -> new InvalidAuthenticationException("Invalid authorization header")); |
| 44 | + |
| 45 | + // Validate token through the application service |
| 46 | + AuthenticatedUser user = authService.validateAccessToken(token); |
| 47 | + |
| 48 | + // Create and return the NextAuth session response |
| 49 | + return ResponseEntity.ok(ApiResponse.success(NextAuthResponse.fromUser(user))); |
| 50 | + } catch (InvalidAuthenticationException e) { |
| 51 | + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @PostMapping("/signin") |
| 56 | + public ResponseEntity<ApiResponse<AuthResponse>> login(@RequestBody LoginRequest request) { |
| 57 | + AuthenticationResult result = authService.authenticate(request.toCommand()); |
| 58 | + |
| 59 | + AuthResponse response = AuthResponse.from(result); |
| 60 | + return ResponseEntity.ok(ApiResponse.success(response)); |
| 61 | + } |
| 62 | + |
| 63 | + @PostMapping("/signup") |
| 64 | + public ResponseEntity<ApiResponse<AuthResponse>> register(@RequestBody RegisterRequest request) { |
| 65 | + |
| 66 | + userService.registerUser(request.firstName(), request.lastName(), request.email(), request.password(), request.phone()); |
| 67 | + |
| 68 | + // Authenticate the newly registered user |
| 69 | + AuthenticationResult result = authService.authenticate(new LoginRequest(request.email(), request.password(), false).toCommand()); |
| 70 | + |
| 71 | + AuthResponse response = AuthResponse.from(result); |
| 72 | + return ResponseEntity.ok(ApiResponse.success(response)); |
| 73 | + } |
| 74 | + |
| 75 | + @PostMapping("/refresh") |
| 76 | + public ResponseEntity<ApiResponse<AuthResponse>> refreshToken(@RequestBody RefreshTokenRequest request) { |
| 77 | + |
| 78 | + AuthenticationResult result = authService.refreshToken(request.refreshToken()); |
| 79 | + AuthResponse response = AuthResponse.from(result); |
| 80 | + return ResponseEntity.ok(ApiResponse.success(response)); |
| 81 | + } |
| 82 | +} |
0 commit comments