make reset password Kotlin/Android app functional #22
@@ -102,9 +102,11 @@ public class UserAuthenticationController {
|
||||
authenticationService.resetPassword(passwordResetDTO);
|
||||
return ResponseEntity.ok("Passwort erfolgreich geändert");
|
||||
} catch (InvalidTokenException | UsernameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return ResponseEntity.badRequest().body(e.getMessage());
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Fehler beim Zurücksetzen des Passworts");
|
||||
e.printStackTrace();
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package SuMoC.backend.security.dtos;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAlias;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
@@ -8,7 +9,9 @@ import lombok.Setter;
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
public class PasswordResetDTO {
|
||||
String otp_code;
|
||||
@JsonAlias({"otp_code", "otpCode"})
|
||||
String otpCode;
|
||||
String email;
|
||||
String new_password;
|
||||
@JsonAlias({"new_password", "newPassword"})
|
||||
String newPassword;
|
||||
}
|
||||
|
||||
@@ -17,12 +17,12 @@ public class PasswordResetToken{
|
||||
private Long id;
|
||||
|
||||
private String email;
|
||||
private String otp_code;
|
||||
private String otpCode;
|
||||
private LocalDateTime expiryDate;
|
||||
|
||||
public PasswordResetToken(String email, String code) {
|
||||
this.email = email;
|
||||
this.otp_code = code;
|
||||
this.otpCode = code;
|
||||
this.expiryDate = LocalDateTime.now().plusMinutes(EXPIRY_TIME_MINUTES);
|
||||
}
|
||||
|
||||
|
||||
@@ -13,8 +13,6 @@ import SuMoC.backend.user.models.User;
|
||||
import SuMoC.backend.user.repository.UserRepository;
|
||||
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;
|
||||
@@ -155,7 +153,6 @@ public class AuthenticationService {
|
||||
});
|
||||
|
||||
String otp = generate_six_digit_OTP();
|
||||
|
||||
passwordResetTokenRepository.save(new PasswordResetToken(email, passwordEncoder.encode(otp))); //encoded otp
|
||||
emailService.sendPasswordResetEmail(email, otp);
|
||||
}
|
||||
@@ -171,18 +168,17 @@ public class AuthenticationService {
|
||||
}
|
||||
|
||||
//use .matches() so compare raw otp to (salted!) encoded otp
|
||||
if(!passwordEncoder.matches(passwordResetDTO.getOtp_code(),token.getOtp_code())){
|
||||
if(!passwordEncoder.matches(passwordResetDTO.getOtpCode(),token.getOtpCode())){
|
||||
throw new InvalidTokenException("Code ist ungültig");
|
||||
}
|
||||
|
||||
if(!isPasswordFormatCorrect(passwordResetDTO.getNew_password())) {
|
||||
if(!isPasswordFormatCorrect(passwordResetDTO.getNewPassword())) {
|
||||
throw new InvalidTokenException("Passwortformat ist nicht korrekt");
|
||||
}
|
||||
|
||||
User user = userRepository.findByEmail(passwordResetDTO.getEmail())
|
||||
.orElseThrow(() -> new UsernameNotFoundException("User existiert nicht"));
|
||||
|
||||
user.setPassword(passwordEncoder.encode(passwordResetDTO.getNew_password()));
|
||||
user.setPassword(passwordEncoder.encode(passwordResetDTO.getNewPassword()));
|
||||
userRepository.save(user);
|
||||
passwordResetTokenRepository.delete(token);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user