added OtpRequestDTO to fix RequestBody for @PostMapping("/request-otp-for-password-reset")
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good

This commit is contained in:
Daniel
2026-02-20 12:28:13 +01:00
parent e333b3b9ce
commit 46a6b2fb7a
3 changed files with 93 additions and 28 deletions
@@ -3,9 +3,7 @@ package SuMoC.backend.security.controller;
import SuMoC.backend.security.dtos.*;
import SuMoC.backend.security.exceptions.InvalidTokenException;
import SuMoC.backend.security.models.PasswordResetToken;
import SuMoC.backend.security.services.AuthenticationService;
import SuMoC.backend.user.repository.UserRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
@@ -21,8 +19,6 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.security.Principal;
@RestController
@RequestMapping("/auth/user")
public class UserAuthenticationController {
@@ -82,39 +78,33 @@ public class UserAuthenticationController {
@Operation(summary = "OTP Code zur änderung des Passworts", description = "Erstellt bei Anfrage einen neuen 6-stelligen OTP Code, " +
"mit welchem das Passwort des Accounts mit der zugehörigen Email einmalig verändert werden kann." +
" Dieser wird per Mail an den Nutzer gesendet.")
@PostMapping("/request-password-reset")
public ResponseEntity<String> handleForgotPasswordRequest(@RequestBody String email, Principal principal){
System.out.println("####################### Handle Password Reset Request #######################");
System.out.println(email);
@PostMapping("/request-otp-for-password-reset")
public ResponseEntity<String> requestOtpForPasswordReset(@RequestBody OtpRequestDTO otpRequestDTO) {
try{
authenticationService.generateNewPasswordResetTokenForUser(email);
return ResponseEntity.ok("OTP Token erstellt");
authenticationService.generateNewPasswordResetTokenForUser(otpRequestDTO.getEmail());
return ResponseEntity.ok("OTP erstellt");
} catch (UsernameNotFoundException e) {
return ResponseEntity.badRequest().body(e.getMessage());
} catch(MailException e){
System.out.println(" ## MAIL EXCEPTION \n" +e.getMessage());
return ResponseEntity.internalServerError().body("Fehler beim versenden der Email, versuche es später erneut");
} catch (Exception e) {
return ResponseEntity.internalServerError().body("Fehler beim erstellen des OTP Tokens");
return ResponseEntity.internalServerError().body("Fehler beim erstellen des OTP");
}
}
@Operation(summary = "Veränderung des Passworts eines Profils ", description = "Durch Eingabe eines Validen OTP Codes " +
"zu einer zugehörigen Email kann das Passwort des Profils geändert werden.")
@PostMapping("/reset-password")
public ResponseEntity<String> handlePasswordResetRequest(@RequestBody PasswordResetDTO passwordResetDTO){
System.out.println("####################### Handle Password Reset Request RESET #######################");
System.out.println(passwordResetDTO.getEmail() + " | newPW: " + passwordResetDTO.getNew_password() + " | Code: " + passwordResetDTO.getOtp_code());
public ResponseEntity<String> resetPassword(@RequestBody PasswordResetDTO passwordResetDTO){
try {
authenticationService.resetPassword(passwordResetDTO);
return ResponseEntity.ok("Passwort erfolgreich geändert");
} catch (InvalidTokenException | UsernameNotFoundException e) {
// 400 Bad Request mit der genauen Fehlermeldung
return ResponseEntity.badRequest().body(e.getMessage());
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Fehler beim Reset");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Fehler beim Zurücksetzen des Passworts");
}
}
}