Working Implementation for resetting passwords based on a One Time Passcode (OTP). first request Token by stating your email to /request-password-forgot. OTP will be sent via email. Then just type in email, the OTP and a new password to /reset-password.
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
IMPORTANT - EMAIL SETUP in application.properties (if gmail): #mail provider spring.mail.host=smtp.gmail.com spring.mail.port=587 spring.mail.username=<email> # Nutze bei Gmail ein "App-Passwort", nicht dein normales (16 stellig) spring.mail.password=xxxx xxxx xxxx xxxx spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
package SuMoC.backend.security.controller;
|
||||
|
||||
|
||||
import SuMoC.backend.security.dtos.LoginResponseDTO;
|
||||
import SuMoC.backend.security.dtos.RegistrationResponseDTO;
|
||||
import SuMoC.backend.security.dtos.UserAuthenticationPairDTO;
|
||||
import SuMoC.backend.security.dtos.UserRegistrationDTO;
|
||||
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;
|
||||
@@ -14,6 +14,7 @@ import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@@ -74,4 +75,36 @@ public class UserAuthenticationController {
|
||||
|
||||
return new ResponseEntity<>(responseDTO, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PostMapping("/request-password-forgot")
|
||||
public ResponseEntity<String> handleForgotPasswordRequest(@RequestBody String email){
|
||||
System.out.println("####################### Handle Password Reset Request #######################");
|
||||
System.out.println(email);
|
||||
|
||||
try{
|
||||
authenticationService.generateNewPasswordResetTokenForUser(email);
|
||||
return ResponseEntity.ok("OTP Token erstellt");
|
||||
} catch (UsernameNotFoundException e) {
|
||||
return ResponseEntity.badRequest().body(e.getMessage());
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.internalServerError().body("Fehler beim erstellen des OTP Tokens");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@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());
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user