Skip to content

Create servlet to change password#54

Open
SophiaJefferson wants to merge 5 commits into
milestone_2from
change_password2
Open

Create servlet to change password#54
SophiaJefferson wants to merge 5 commits into
milestone_2from
change_password2

Conversation

@SophiaJefferson
Copy link
Copy Markdown
Collaborator

I didn't create the test file just because I'm behind but hopefully I updated the User entity in the datastore correctly -- let me know if y'all find anything I should fix! Thanks!

Copy link
Copy Markdown
Owner

@gauravmishra gauravmishra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few minor comments. The major change still needed is to persist the password changes to PersistentStore. Otherwise, the password will be set back to the original password once the session ends.

if (userStore.isUserRegistered(username)) {
User user = userStore.getUser(username);
if (BCrypt.checkpw(password, user.getPassword())) {
user.setPassword(oldpassword);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also need to update datastore for persistence.


if (userStore.isUserRegistered(username)) {
User user = userStore.getUser(username);
if (BCrypt.checkpw(password, user.getPassword())) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to check oldpassword here rather than password; then setPassword to password rather than oldpassword in the next line.

request.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(request, response);
}
} else {
request.setAttribute("error", "That username was not found.");
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would "No user logged in" be a more appropriate error message here?

Copy link
Copy Markdown
Owner

@gauravmishra gauravmishra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments on changes required...some other suggestions that would be good to have:

  • Add a change password button in profile that takes user to the /changepassword page
  • Invalid old password should redirect to profile instead of login as the user is logged in
  • Check and raise error if old password and new password are the same.
  • You don't need messageStore in ChangePasswordServlet; can remove it.

if (userStore.isUserRegistered(username)) {
User user = userStore.getUser(username);
if (BCrypt.checkpw(oldpassword, user.getPassword())) {
if (request.getParameter("update") != null) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be "newpassword" instead "update"

if (BCrypt.checkpw(oldpassword, user.getPassword())) {
if (request.getParameter("update") != null) {
String newPassword = request.getParameter("newpassword");
user.setPassword(newPassword);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you've to hash the password before setting it:
String passwordHash = BCrypt.hashpw(newPassword, BCrypt.gensalt());
user.setPassword(passwordHash);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants