Created
March 19, 2020 20:20
-
-
Save thinkmicroservices/bfc5a744cd2a24f6f8cd3de5967bbe85 to your computer and use it in GitHub Desktop.
AuthenticationService: recoverPassword().java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @param user | |
* @return | |
* @throws RecoverPasswordException | |
*/ | |
public String recoverPassword(String email) throws RecoverPasswordException { | |
log.debug("recover password for:" + email); | |
User userModel = userRepository.findByUsername(email); | |
if (userModel == null) { | |
throw new RecoverPasswordException("error.passwordrecovery.invalid.user", email); | |
} | |
String recoveryCodeString = UUID.randomUUID().toString(); | |
userModel.setRecoveryCode(recoveryCodeString); | |
LocalDateTime expirationTimestamp = LocalDateTime.now(); | |
log.debug("recoveryCodeExpireationIntervalMinutes:{}", recoveryCodeExpirationIntervalMinutes); | |
log.debug("currentTime :{}" + Timestamp.valueOf(expirationTimestamp)); | |
// add the interval before we store it | |
expirationTimestamp = expirationTimestamp.plusMinutes(recoveryCodeExpirationIntervalMinutes); | |
log.debug("currentTime+interval:{}" + Timestamp.valueOf(expirationTimestamp)); | |
log.debug("{} -recovery code={}, expires={} ", email, recoveryCodeString, Timestamp.valueOf(expirationTimestamp)); | |
userModel.setRecoveryExpiresAt(Timestamp.valueOf(expirationTimestamp)); | |
userRepository.save(userModel); | |
this.emailClientService.sendRecoveryEmail(email, recoveryCodeString); | |
// send account event "RecoverPassword Requested" | |
this.accountEventSource.accountEvents().send(MessageBuilder.withPayload(new PasswordRecoveryRequestedEvent(userModel.getAccountId(), userModel.getEmail())) | |
.setHeader("type", "PASSWORD_RECOVERY_REQUESTED_EVENT").build()); | |
return recoveryCodeString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment