diff --git a/src/main/java/com/safetypin/authentication/controller/AuthenticationController.java b/src/main/java/com/safetypin/authentication/controller/AuthenticationController.java index 08f921c0b23bcc33706ec0d96e7a5113ba97268c..d7e04c022585ab7af770391f858c6b1cf6d11a97 100644 --- a/src/main/java/com/safetypin/authentication/controller/AuthenticationController.java +++ b/src/main/java/com/safetypin/authentication/controller/AuthenticationController.java @@ -1,9 +1,11 @@ package com.safetypin.authentication.controller; +import com.safetypin.authentication.dto.AuthResponse; import com.safetypin.authentication.dto.PasswordResetRequest; import com.safetypin.authentication.dto.RegistrationRequest; import com.safetypin.authentication.dto.SocialLoginRequest; import com.safetypin.authentication.exception.InvalidCredentialsException; +import com.safetypin.authentication.exception.UserAlreadyExistsException; import com.safetypin.authentication.model.User; import com.safetypin.authentication.service.AuthenticationService; import org.springframework.http.HttpStatus; @@ -24,14 +26,28 @@ public class AuthenticationController { // Endpoint for email registration @PostMapping("/register-email") - public User registerEmail(@Valid @RequestBody RegistrationRequest request) { - return authenticationService.registerUser(request); + public ResponseEntity<AuthResponse> registerEmail(@Valid @RequestBody RegistrationRequest request) { + User user; + try { + user = authenticationService.registerUser(request); + } catch (IllegalArgumentException | UserAlreadyExistsException e) { + AuthResponse response = new AuthResponse(false, e.getMessage(), null); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response); + } + return ResponseEntity.ok().body(new AuthResponse(true, "OK", user)); } // Endpoint for social registration/login @PostMapping("/register-social") - public User registerSocial(@Valid @RequestBody SocialLoginRequest request) { - return authenticationService.socialLogin(request); + public ResponseEntity<AuthResponse> registerSocial(@Valid @RequestBody SocialLoginRequest request) { + User user; + try { + user = authenticationService.socialLogin(request); + } catch (IllegalArgumentException | UserAlreadyExistsException e) { + AuthResponse response = new AuthResponse(false, e.getMessage(), null); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response); + } + return ResponseEntity.ok().body(new AuthResponse(true, "OK", user)); } // OTP verification endpoint @@ -45,7 +61,7 @@ public class AuthenticationController { // Endpoint for email login @PostMapping("/login-email") - public ResponseEntity<?> loginEmail(@RequestParam String email, @RequestParam String password) { + public ResponseEntity<Object> loginEmail(@RequestParam String email, @RequestParam String password) { try { return ResponseEntity.ok(authenticationService.loginUser(email, password)); } catch (InvalidCredentialsException e){ diff --git a/src/main/java/com/safetypin/authentication/dto/AuthResponse.java b/src/main/java/com/safetypin/authentication/dto/AuthResponse.java new file mode 100644 index 0000000000000000000000000000000000000000..a29962ff41457e80c4adbbb6f198b35b121751b4 --- /dev/null +++ b/src/main/java/com/safetypin/authentication/dto/AuthResponse.java @@ -0,0 +1,13 @@ +package com.safetypin.authentication.dto; + +import lombok.*; + +@Data +@Getter +@Setter +@AllArgsConstructor +public class AuthResponse { + private boolean success; + private String message; + private Object data; +} \ No newline at end of file diff --git a/src/main/java/com/safetypin/authentication/service/AuthenticationService.java b/src/main/java/com/safetypin/authentication/service/AuthenticationService.java index 5b926257d113dd142b5a666a03a0dc435f8f1ba9..96bca07a4ad8022ee973413da7c2203276a4a065 100644 --- a/src/main/java/com/safetypin/authentication/service/AuthenticationService.java +++ b/src/main/java/com/safetypin/authentication/service/AuthenticationService.java @@ -15,6 +15,7 @@ import java.time.Period; @Service public class AuthenticationService { + private static final String EMAIL_PROVIDER = "EMAIL"; private final UserRepository userRepository; private final PasswordEncoder passwordEncoder; @@ -37,7 +38,7 @@ public class AuthenticationService { } String encodedPassword = passwordEncoder.encode(request.getPassword()); User user = new User(request.getEmail(), encodedPassword, request.getName(), false, "USER", - request.getBirthdate(), "EMAIL", null); + request.getBirthdate(), EMAIL_PROVIDER, null); user = userRepository.save(user); otpService.generateOTP(request.getEmail()); logger.info("OTP generated for {} at {}", request.getEmail(), java.time.LocalDateTime.now()); @@ -51,8 +52,8 @@ public class AuthenticationService { } User existing = userRepository.findByEmail(request.getEmail()); if (existing != null) { - if ("EMAIL".equals(existing.getProvider())) { - throw new IllegalArgumentException("An account with this email exists. Please sign in using your email and password."); + if (EMAIL_PROVIDER.equals(existing.getProvider())) { + throw new UserAlreadyExistsException("An account with this email exists. Please sign in using your email and password."); } return existing; } @@ -109,7 +110,7 @@ public class AuthenticationService { // Forgot password – only applicable for email-registered users public void forgotPassword(String email) { User user = userRepository.findByEmail(email); - if (user == null || !"EMAIL".equals(user.getProvider())) { + if (user == null || !EMAIL_PROVIDER.equals(user.getProvider())) { throw new IllegalArgumentException("Password reset is only available for email-registered users."); } // In production, send a reset token via email. @@ -125,6 +126,7 @@ public class AuthenticationService { if (!user.isVerified()) { return "Your account is not verified. Please complete OTP verification. You may request a new OTP after 2 minutes."; } + logger.info("AuthenticationService.postContent :: Content posted: {}", content); // For demo purposes, we assume the post is successful. return "Content posted successfully"; }