Skip to content

Commit 5557a89

Browse files
Made proper repo and service calls
1 parent 14bb3c7 commit 5557a89

File tree

4 files changed

+34
-41
lines changed

4 files changed

+34
-41
lines changed

src/main/java/com/accolite/pru/health/AuthApp/repository/UserRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ public interface UserRepository extends JpaRepository<User, Long> {
2424
@Modifying
2525
@Query(value = "update USER set PASSWORD = :password where EMAIL= :email", nativeQuery = true)
2626
public void resetPassword(@Param("email") String email, @Param("password") String password);
27+
28+
@Query(value = "SELECT * from user where user.user_id=(select user_id from password_reset_token t where t.token_name=:token)", nativeQuery = true)
29+
User findByToken(@Param("token") String token);
2730
}

src/main/java/com/accolite/pru/health/AuthApp/service/PasswordResetService.java

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,72 +12,58 @@
1212
import com.accolite.pru.health.AuthApp.exception.ResourceNotFoundException;
1313
import com.accolite.pru.health.AuthApp.model.PasswordResetToken;
1414
import com.accolite.pru.health.AuthApp.model.User;
15-
import com.accolite.pru.health.AuthApp.repository.ForgotPasswordRepository;
1615

1716
@Service
1817
public class PasswordResetService {
19-
@Autowired
20-
ForgotPasswordRepository forgotPasswordRepository;
21-
2218
@Autowired
2319
PasswordResetTokenService passwordResetTokenService;
2420

2521
@Autowired
2622
private PasswordEncoder passwordEncoder;
2723

24+
@Autowired
25+
private UserService userService;
26+
2827
private static final Logger logger = Logger.getLogger(PasswordResetService.class);
2928

3029
/**
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
3932
*/
4033
public Optional<PasswordResetToken> generatePasswordResetToken(String mailId) {
41-
Boolean emailExists = forgotPasswordRepository.existsByEmail(mailId);
34+
Boolean emailExists = userService.existsByEmail(mailId);
4235
if (!(emailExists)) {
43-
throw new ResourceNotFoundException("User", "email", mailId);
36+
throw new ResourceNotFoundException("Address", "email id", mailId);
4437
}
4538
PasswordResetToken passwordResetToken = passwordResetTokenService.getPasswordResetToken(mailId);
4639
return Optional.ofNullable(passwordResetToken);
4740
}
4841

4942
/**
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.
6045
*/
61-
public Optional<String> resetPassword(String newPassword, String token) {
46+
public Optional<User> resetPassword(String newPassword, String token) {
6247
String encodedPassword;
6348
String mailId;
49+
User user;
6450
Boolean tokenExists = passwordResetTokenService.existsByToken(token);
6551
if (!(tokenExists)) {
6652
throw new ResourceNotFoundException("Token", "Token Id", token);
6753
}
6854
Instant currentTime = Instant.now();
6955
Instant expiryTime = passwordResetTokenService.findExpiryTimeByToken(token);
7056
if (currentTime.isBefore(expiryTime)) {
71-
User user = passwordResetTokenService.findUserByToken(token);
57+
user = passwordResetTokenService.findUserByToken(token);
7258
mailId = user.getEmail();
7359
encodedPassword = passwordEncoder.encode(newPassword);
7460
logger.info("password:-------------------" + newPassword + " " + encodedPassword + "CurrentTime"
7561
+ currentTime + "ExpiryTS:" + expiryTime);
76-
forgotPasswordRepository.resetPassword(mailId, encodedPassword);
62+
userService.resetPassword(mailId, encodedPassword);
7763
} else {
7864
throw new InvalidTokenRequestException("Reset Link Token", token,
7965
"Expired Link. Please issue a new request");
8066
}
81-
return Optional.ofNullable(mailId);
67+
return Optional.ofNullable(user);
8268
}
8369
}

src/main/java/com/accolite/pru/health/AuthApp/service/PasswordResetTokenService.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,24 @@ public class PasswordResetTokenService {
2727
@Autowired
2828
UserService userService;
2929

30-
public void addToken(PasswordResetToken passwordResetToken) {
30+
public void saveToken(PasswordResetToken passwordResetToken) {
3131
passwordResetTokenRepository.save(passwordResetToken);
3232
}
3333

3434
/**
35-
* Gets the password reset token.
36-
*
37-
* 1) Set the metadata for token and return it
38-
*
39-
* @param mailId
40-
* the mail id
41-
* @return the password reset token
35+
* Set the metadata for password reset token and return it to password reset
36+
* service
4237
*/
4338
public PasswordResetToken getPasswordResetToken(String mailId) {
4439
PasswordResetToken passwordResetToken = new PasswordResetToken();
45-
Util util = new Util();
46-
String token = util.generateResetLinkToken();
40+
String token = Util.generateResetLinkToken();
4741
passwordResetToken.setToken(token);
4842
Optional<User> userOpt = userService.findByEmail(mailId);
4943
User user = userOpt.get();
5044
passwordResetToken.setUser(user);
45+
logger.info(user);
5146
passwordResetToken.setExpiryTime(Instant.now().plusMillis(expiration));
52-
addToken(passwordResetToken);
47+
saveToken(passwordResetToken);
5348
return passwordResetToken;
5449
}
5550

@@ -58,8 +53,7 @@ public Instant findExpiryTimeByToken(String token) {
5853
}
5954

6055
public User findUserByToken(String token) {
61-
Long id = passwordResetTokenRepository.findUserIdByToken(token);
62-
Optional<User> userOpt = userService.findById(id);
56+
Optional<User> userOpt = userService.findByToken(token);
6357
User user = userOpt.get();
6458
return user;
6559
}

src/main/java/com/accolite/pru/health/AuthApp/service/UserService.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,18 @@ public Boolean existsByUsername(String username) {
6868
return userRepository.existsByUsername(username);
6969
}
7070

71+
/**
72+
* Update the password for given mail id
73+
*/
7174
public void resetPassword(String mailId, String encodedPassword) {
7275
userRepository.resetPassword(mailId, encodedPassword);
7376
}
7477

78+
/**
79+
* Find the user by token
80+
*/
81+
public Optional<User> findByToken(String token) {
82+
return Optional.ofNullable(userRepository.findByToken(token));
83+
}
84+
7585
}

0 commit comments

Comments
 (0)