11package org .spacehub .controller ;
22
3+ import org .spacehub .DTO .EmailRequest ;
4+ import org .spacehub .DTO .LoginRequest ;
5+ import org .spacehub .DTO .OTPRequest ;
6+ import org .spacehub .entities .ApiResponse ;
37import org .spacehub .entities .User ;
48import org .spacehub .entities .RegistrationRequest ;
59import org .spacehub .security .EmailValidator ;
610import org .spacehub .service .OTPService ;
11+ import org .spacehub .service .UserService ;
712import org .spacehub .service .VerificationService ;
813import org .spacehub .service .RegistrationService ;
914import org .springframework .beans .factory .annotation .Autowired ;
1015import org .springframework .http .ResponseEntity ;
11- import org .springframework .web .bind .annotation .GetMapping ;
1216import org .springframework .web .bind .annotation .PostMapping ;
1317import org .springframework .web .bind .annotation .RequestBody ;
14- import org .springframework .web .bind .annotation .RequestParam ;
1518import org .springframework .web .bind .annotation .RequestMapping ;
1619import org .springframework .web .bind .annotation .RestController ;
1720
@@ -26,68 +29,86 @@ public class UserController {
2629 @ Autowired
2730 private OTPService otpService ;
2831
32+ @ Autowired
33+ private UserService userService ;
34+
2935 public UserController (VerificationService verificationService , RegistrationService registrationService , EmailValidator emailValidator ) {
3036 this .verificationService = verificationService ;
3137 this .registrationService = registrationService ;
3238 this .emailValidator = emailValidator ;
3339 }
3440
3541 @ PostMapping ("/login" )
36- public ResponseEntity <String > login (@ RequestBody User user ) {
37- String email = emailValidator .normalize (user .getEmail ());
42+ public ResponseEntity <ApiResponse < String >> login (@ RequestBody LoginRequest request ) {
43+ String email = emailValidator .normalize (request .getEmail ());
3844 if (!emailValidator .test (email )) {
39- return ResponseEntity .badRequest ().body ("Invalid email format!" );
45+ return ResponseEntity .badRequest ().body (new ApiResponse <>( 400 , "Invalid email format!" , null ) );
4046 }
47+
48+ User user = new User ();
4149 user .setEmail (email );
50+ user .setPassword (request .getPassword ());
51+
4252 String token = verificationService .check (user );
43- if (token == null ) return ResponseEntity .status (401 ).body ("Invalid credentials" );
44- return ResponseEntity .ok (token );
53+ if (token == null ) {
54+ return ResponseEntity .status (401 ).body (new ApiResponse <>(401 , "Invalid credentials" , null ));
55+ }
56+ return ResponseEntity .ok (new ApiResponse <>(200 , "Login successful" , token ));
4557 }
4658
4759 @ PostMapping ("/registration" )
48- public ResponseEntity <String > register (@ RequestBody RegistrationRequest request ) {
60+ public ResponseEntity <ApiResponse < String > > register (@ RequestBody RegistrationRequest request ) {
4961 String email = emailValidator .normalize (request .getEmail ());
5062 if (!emailValidator .test (email )) {
51- return ResponseEntity .badRequest ().body ("Invalid email format!" );
63+ return ResponseEntity .badRequest ().body (new ApiResponse <>( 400 , "Invalid email format!" , null ) );
5264 }
5365 request .setEmail (email );
5466 try {
55- String result = registrationService .register (request );
56- return ResponseEntity .status (201 ).body (result );
57- } catch (IllegalStateException ex ) {
58- return ResponseEntity .badRequest ().body (ex .getMessage ());
59- } catch (Exception ex ) {
60- return ResponseEntity .internalServerError ().body ("Registration failed" );
67+ String token = registrationService .register (request );
68+ return ResponseEntity .status (201 ).body (new ApiResponse <>( 201 , "Registration successful" , token ) );
69+ } catch (IllegalStateException e ) {
70+ return ResponseEntity .badRequest ().body (new ApiResponse <>( 400 , e .getMessage (), null ));
71+ } catch (Exception e ) {
72+ return ResponseEntity .internalServerError ().body (new ApiResponse <>( 500 , "Registration failed" , null ) );
6173 }
6274 }
6375
64- @ GetMapping ("/sendotp" )
65- public ResponseEntity <String > sendOTP (@ RequestParam ( required = false ) String email ) {
66- email = emailValidator .normalize (email );
76+ @ PostMapping ("/sendotp" )
77+ public ResponseEntity <ApiResponse < String >> sendOTP (@ RequestBody EmailRequest request ) {
78+ String email = emailValidator .normalize (request . getEmail () );
6779 if (email == null || !emailValidator .test (email )) {
68- return ResponseEntity .badRequest ().body ("Invalid or missing email!" );
80+ return ResponseEntity .badRequest ().body (new ApiResponse <>( 400 , "Invalid or missing email!" , null ) );
6981 }
70- try {
71- boolean allowed = otpService .canSendOTP (email );
72- if (!allowed ) {
73- long secondsLeft = otpService .cooldownTime (email );
74- return ResponseEntity .badRequest ().body ("Please wait " + secondsLeft + " seconds before requesting OTP again." );
75- }
76- otpService .sendOTP (email );
77- return ResponseEntity .ok ("OTP sent successfully to " + email );
78- } catch (Exception e ) {
79- return ResponseEntity .status (500 ).body ("Error sending OTP: " + e .getMessage ());
82+ if (!otpService .canSendOTP (email )) {
83+ long secondsLeft = otpService .cooldownTime (email );
84+ return ResponseEntity .badRequest ().body (new ApiResponse <>(400 , "Please wait " + secondsLeft + " seconds before requesting OTP again." , null ));
8085 }
86+ otpService .sendOTP (email );
87+ return ResponseEntity .ok (new ApiResponse <>(200 , "OTP sent successfully to " + email , null ));
8188 }
8289
83- @ GetMapping ("/validateotp" )
84- public ResponseEntity <String > validateOTP (@ RequestParam String otp ) {
85- boolean valid = otpService .validateOTP (otp );
90+ @ PostMapping ("/validateotp" )
91+ public ResponseEntity <ApiResponse < String >> validateOTP (@ RequestBody OTPRequest request ) {
92+ boolean valid = otpService .validateOTP (request . getEmail (), request . getOtp () );
8693 if (valid ) {
87- return ResponseEntity .ok ("OTP is valid" );
94+ return ResponseEntity .ok (new ApiResponse <>( 200 , "OTP is valid" , null ) );
8895 } else {
89- return ResponseEntity .status (400 ).body ("OTP is invalid or expired" );
96+ return ResponseEntity .status (400 ).body (new ApiResponse <>(400 , "OTP is invalid or expired" , null ));
97+ }
98+ }
99+
100+ @ PostMapping ("/forgotpassword" )
101+ public ResponseEntity <ApiResponse <String >> forgotPassword (@ RequestBody EmailRequest request ) {
102+ String email = emailValidator .normalize (request .getEmail ());
103+ if (email == null || !emailValidator .test (email )) {
104+ return ResponseEntity .badRequest ().body (new ApiResponse <>(400 , "Invalid email format!" , null ));
90105 }
106+ if (!userService .checkUser (email )) {
107+ return ResponseEntity .badRequest ().body (new ApiResponse <>(400 , "User with this email does not exist" , null ));
108+ }
109+ otpService .sendOTP (email );
110+ return ResponseEntity .ok (new ApiResponse <>(200 , "OTP sent to your email" , null ));
91111 }
92112
113+
93114}
0 commit comments