Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 53082b6

Browse files
Sending reset-password link
1 parent 2dfb9bf commit 53082b6

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

‎src/main/java/com/naturalprogrammer/spring5tutorial/domain/User.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public static enum Role {
4949
@Column(length=36)
5050
private String verificationCode;
5151

52+
@Column(length=36, unique=true)
53+
private String resetPasswordCode;
54+
5255
public Long getId() {
5356
return id;
5457
}
@@ -98,6 +101,14 @@ public void setVerificationCode(String verificationCode) {
98101
this.verificationCode = verificationCode;
99102
}
100103

104+
public String getResetPasswordCode() {
105+
return resetPasswordCode;
106+
}
107+
108+
public void setResetPasswordCode(String resetPasswordCode) {
109+
this.resetPasswordCode = resetPasswordCode;
110+
}
111+
101112
@Override
102113
public Collection<? extends GrantedAuthority> getAuthorities() {
103114

‎src/main/java/com/naturalprogrammer/spring5tutorial/services/UserServiceImpl.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,24 @@ private boolean isAdminOrSelfLoggedIn(User user) {
162162

163163
@Override
164164
public void forgotPassword(ForgotPasswordCommand forgotPasswordCommand) {
165-
log.info("Resetting password for " + forgotPasswordCommand.getEmail());
165+
166+
User user = userRepository.findByEmail(forgotPasswordCommand.getEmail()).get();
167+
168+
user.setResetPasswordCode(UUID.randomUUID().toString());
169+
userRepository.save(user);
170+
MyUtils.afterCommit(() -> mailResetPasswordLink(user));
171+
}
172+
173+
private void mailResetPasswordLink(User user) {
174+
175+
String resetPasswordLink = applicationUrl + "/reset-password/" +
176+
user.getResetPasswordCode();
177+
178+
try {
179+
mailSender.send(user.getEmail(), MyUtils.getMessage("resetPasswordSubject"),
180+
MyUtils.getMessage("resetPasswordBody", resetPasswordLink));
181+
} catch (MessagingException e) {
182+
log.warn("Error sending reset password mail to " + user.getEmail(), e);
183+
}
166184
}
167185
}

‎src/main/resources/messages.properties

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ verificationMailResent: Verification mail resent!
1818
userNotFound: User not found
1919
notPermitted: Not permitted
2020
21-
forgotPasswordMailSent: Check your mail box for resetting password
21+
forgotPasswordMailSent: Check your mail box for resetting password
22+
23+
resetPasswordSubject: Forgot password?
24+
resetPasswordBody: Hi,<br/><br/>Please click on the link below to reset password\
25+
<br/><br/>{0}<br/><br/>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /