Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thinkmicroservices/bfc5a744cd2a24f6f8cd3de5967bbe85 to your computer and use it in GitHub Desktop.
Save thinkmicroservices/bfc5a744cd2a24f6f8cd3de5967bbe85 to your computer and use it in GitHub Desktop.
AuthenticationService: recoverPassword().java
/**
*
* @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