brevo smtp server (300 mails/day free)
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good

Test written for changing Password based on otp

some other stuff
This commit is contained in:
Daniel
2026-02-18 15:06:21 +01:00
parent 80ba4c33b6
commit cc6028bab5
6 changed files with 136 additions and 10 deletions
@@ -15,6 +15,7 @@ import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.MailException;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
@@ -132,7 +133,7 @@ public class AuthenticationService {
* @param email the account for which a OTP shall be created to reset the password
*/
@Transactional
public void generateNewPasswordResetTokenForUser(String email) throws UsernameNotFoundException{
public void generateNewPasswordResetTokenForUser(String email) throws UsernameNotFoundException, MailException{
if (!userRepository.existsByEmail(email)) throw new UsernameNotFoundException("Es existiert kein Account mit dieser Email") ;
Optional<PasswordResetToken> token = passwordResetTokenRepository.findByEmail(email);
@@ -145,12 +146,15 @@ public class AuthenticationService {
PasswordResetToken newToken = new PasswordResetToken(email, otp);
passwordResetTokenRepository.save(newToken);
emailService.sendPasswordResetEmail(newToken.getEmail(), newToken.getOtp_code());
}
@Transactional
public void resetPassword(PasswordResetDTO passwordResetDTO) throws InvalidTokenException, UsernameNotFoundException {
System.out.println("## RESETING PASSWORD FOR: " + passwordResetDTO.getEmail() + " with code " + passwordResetDTO.getOtp_code() );
PasswordResetToken token = passwordResetTokenRepository.findByEmail(passwordResetDTO.getEmail())
.orElseThrow(() -> new InvalidTokenException("Token nicht gefunden"));
@@ -172,8 +176,10 @@ public class AuthenticationService {
private String generate_six_digit_OTP(){
SecureRandom random = new SecureRandom();
int code = 100000 + random.nextInt(900000);
return String.valueOf(code);
// Generiert eine Zahl von 0 bis 999.999
int code = random.nextInt(1000000);
// Formatiert die Zahl auf 6 Stellen und füllt links mit 0 auf
return String.format("%06d", code);
}
}