package com.billpaymentservice.bbps.controller; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.CrossOrigin; 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 com.billpaymentservice.bbps.dto.auth.AuthResponse; import com.billpaymentservice.bbps.dto.response.AuthResponses; import com.billpaymentservice.bbps.dto.auth.LoginRequest; import com.billpaymentservice.bbps.dto.auth.OtpSendResponse; import com.billpaymentservice.bbps.dto.auth.RegisterRequest; import com.billpaymentservice.bbps.dto.auth.VerifyOtpRequest; import com.billpaymentservice.bbps.service.AuthService; import lombok.extern.slf4j.Slf4j; @RestController @RequestMapping("/auth") @CrossOrigin(origins = "*") @Slf4j public class AuthController { private final AuthService authService; public AuthController(AuthService authService) { this.authService = authService; } @PostMapping(value = "/sendOtp", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity sendOtp(@Validated @RequestBody LoginRequest request) { log.info("Received sendOtp request for mobile: {}", request.getMobile()); OtpSendResponse resp = authService.sendOtp(request); return ResponseEntity.ok(resp); } @PostMapping(value = "/verifyOtp", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity verifyOtp(@Validated @RequestBody VerifyOtpRequest request) { AuthResponses resp = authService.verifyOtp(request); return ResponseEntity.ok(resp); } @PostMapping(value = "/register", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity register(@Validated @RequestBody RegisterRequest request) { AuthResponse resp = authService.registerUser(request); return ResponseEntity.ok(resp); } @PostMapping("/loginWithoutDecryption") public ResponseEntity login(@RequestBody LoginRequest loginRequest) { try { AuthResponses authResponse = authService.authenticateUser(loginRequest); return ResponseEntity.ok(authResponse); } catch (Exception e) { return ResponseEntity.status(500).body(AuthResponses.builder().status("Unauthorized") .message("An unexpected error occurred").statusCode(500).build()); } } }