|
12 | 12 | import com.accolite.pru.health.AuthApp.exception.ResourceNotFoundException; |
13 | 13 | import com.accolite.pru.health.AuthApp.model.PasswordResetToken; |
14 | 14 | import com.accolite.pru.health.AuthApp.model.User; |
15 | | -import com.accolite.pru.health.AuthApp.repository.ForgotPasswordRepository; |
16 | 15 |
|
17 | 16 | @Service |
18 | 17 | public class PasswordResetService { |
19 | | - @Autowired |
20 | | - ForgotPasswordRepository forgotPasswordRepository; |
21 | | - |
22 | 18 | @Autowired |
23 | 19 | PasswordResetTokenService passwordResetTokenService; |
24 | 20 |
|
25 | 21 | @Autowired |
26 | 22 | private PasswordEncoder passwordEncoder; |
27 | 23 |
|
| 24 | + @Autowired |
| 25 | + private UserService userService; |
| 26 | + |
28 | 27 | private static final Logger logger = Logger.getLogger(PasswordResetService.class); |
29 | 28 |
|
30 | 29 | /** |
31 | | - * Generate Password Reset Token. |
32 | | - * |
33 | | - * 1) Verifying if the email id exists 2) Generate a password reset token and |
34 | | - * return |
35 | | - * |
36 | | - * @param mailId |
37 | | - * the mail id |
38 | | - * @return the password reset token |
| 30 | + * Verifying if the email id exists.Generate a password reset token and return |
| 31 | + * the token to the controller |
39 | 32 | */ |
40 | 33 | public Optional<PasswordResetToken> generatePasswordResetToken(String mailId) { |
41 | | - Boolean emailExists = forgotPasswordRepository.existsByEmail(mailId); |
| 34 | + Boolean emailExists = userService.existsByEmail(mailId); |
42 | 35 | if (!(emailExists)) { |
43 | | - throw new ResourceNotFoundException("User", "email", mailId); |
| 36 | + throw new ResourceNotFoundException("Address", "email id", mailId); |
44 | 37 | } |
45 | 38 | PasswordResetToken passwordResetToken = passwordResetTokenService.getPasswordResetToken(mailId); |
46 | 39 | return Optional.ofNullable(passwordResetToken); |
47 | 40 | } |
48 | 41 |
|
49 | 42 | /** |
50 | | - * Reset password. |
51 | | - * |
52 | | - * 1) Checking the current time with expiry time of that particular token. 2) If |
53 | | - * the current time doesn't exceed the expiry time, encode and update the |
54 | | - * password. |
55 | | - * |
56 | | - * @param newPassword |
57 | | - * the new password |
58 | | - * @param token |
59 | | - * the token |
| 43 | + * Checking the current time with expiry time of that particular token.If the |
| 44 | + * current time doesn't exceed the expiry time, encode and update the password. |
60 | 45 | */ |
61 | | - public Optional<String> resetPassword(String newPassword, String token) { |
| 46 | + public Optional<User> resetPassword(String newPassword, String token) { |
62 | 47 | String encodedPassword; |
63 | 48 | String mailId; |
| 49 | + User user; |
64 | 50 | Boolean tokenExists = passwordResetTokenService.existsByToken(token); |
65 | 51 | if (!(tokenExists)) { |
66 | 52 | throw new ResourceNotFoundException("Token", "Token Id", token); |
67 | 53 | } |
68 | 54 | Instant currentTime = Instant.now(); |
69 | 55 | Instant expiryTime = passwordResetTokenService.findExpiryTimeByToken(token); |
70 | 56 | if (currentTime.isBefore(expiryTime)) { |
71 | | - User user = passwordResetTokenService.findUserByToken(token); |
| 57 | + user = passwordResetTokenService.findUserByToken(token); |
72 | 58 | mailId = user.getEmail(); |
73 | 59 | encodedPassword = passwordEncoder.encode(newPassword); |
74 | 60 | logger.info("password:-------------------" + newPassword + " " + encodedPassword + "CurrentTime" |
75 | 61 | + currentTime + "ExpiryTS:" + expiryTime); |
76 | | - forgotPasswordRepository.resetPassword(mailId, encodedPassword); |
| 62 | + userService.resetPassword(mailId, encodedPassword); |
77 | 63 | } else { |
78 | 64 | throw new InvalidTokenRequestException("Reset Link Token", token, |
79 | 65 | "Expired Link. Please issue a new request"); |
80 | 66 | } |
81 | | - return Optional.ofNullable(mailId); |
| 67 | + return Optional.ofNullable(user); |
82 | 68 | } |
83 | 69 | } |
0 commit comments