From 89eb7724a0d4f762aefc6943817265fb1205b91a Mon Sep 17 00:00:00 2001 From: "aditya.chandel" <> Date: Wed, 12 Mar 2025 14:31:39 -0600 Subject: [PATCH] New password cannot be same as the current one --- .../booklore/exception/ApiError.java | 1 + .../booklore/service/user/UserService.java | 10 +++++++- .../change-password.component.ts | 25 ++++++++----------- .../settings/admin/admin.component.html | 2 +- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/exception/ApiError.java b/booklore-api/src/main/java/com/adityachandel/booklore/exception/ApiError.java index 79870d593..53bba745b 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/exception/ApiError.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/exception/ApiError.java @@ -33,6 +33,7 @@ public enum ApiError { UNAUTHORIZED(HttpStatus.FORBIDDEN, "%s"), PASSWORD_INCORRECT(HttpStatus.BAD_REQUEST, "Incorrect current password"), PASSWORD_WEAK(HttpStatus.BAD_REQUEST, "New password is not strong enough. It must be at least 8 characters long, contain an uppercase letter, a lowercase letter, a number, and a special character."), + PASSWORD_SAME_AS_CURRENT(HttpStatus.BAD_REQUEST, "New password cannot be the same as the current password"), INVALID_CREDENTIALS(HttpStatus.BAD_REQUEST, "Invalid credentials"); private final HttpStatus status; diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/user/UserService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/user/UserService.java index 68fb27c61..a4b4f3b7e 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/user/UserService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/user/UserService.java @@ -92,13 +92,21 @@ public class UserService { public void changePassword(ChangePasswordRequest changePasswordRequest) { BookLoreUser bookLoreUser = authenticationService.getAuthenticatedUser(); - BookLoreUserEntity bookLoreUserEntity = userRepository.findById(bookLoreUser.getId()).orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(bookLoreUser.getId())); + BookLoreUserEntity bookLoreUserEntity = userRepository.findById(bookLoreUser.getId()) + .orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(bookLoreUser.getId())); + if (!passwordEncoder.matches(changePasswordRequest.getCurrentPassword(), bookLoreUserEntity.getPasswordHash())) { throw ApiError.PASSWORD_INCORRECT.createException(); } + + if (passwordEncoder.matches(changePasswordRequest.getNewPassword(), bookLoreUserEntity.getPasswordHash())) { + throw ApiError.PASSWORD_SAME_AS_CURRENT.createException(); + } + if (!isPasswordStrong(changePasswordRequest.getNewPassword())) { throw ApiError.PASSWORD_WEAK.createException(); } + bookLoreUserEntity.setDefaultPassword(false); bookLoreUserEntity.setPasswordHash(passwordEncoder.encode(changePasswordRequest.getNewPassword())); userRepository.save(bookLoreUserEntity); diff --git a/booklore-ui/src/app/core/component/change-password/change-password.component.ts b/booklore-ui/src/app/core/component/change-password/change-password.component.ts index ce46dc0f2..211ab3b4b 100644 --- a/booklore-ui/src/app/core/component/change-password/change-password.component.ts +++ b/booklore-ui/src/app/core/component/change-password/change-password.component.ts @@ -1,10 +1,10 @@ import {Component, inject} from '@angular/core'; -import { Button } from 'primeng/button'; -import { Card } from 'primeng/card'; -import { FormsModule, ReactiveFormsModule } from '@angular/forms'; -import { Message } from 'primeng/message'; -import { NgIf } from '@angular/common'; -import { Password } from 'primeng/password'; +import {Button} from 'primeng/button'; +import {Card} from 'primeng/card'; +import {FormsModule, ReactiveFormsModule} from '@angular/forms'; +import {Message} from 'primeng/message'; +import {NgIf} from '@angular/common'; +import {Password} from 'primeng/password'; import {MessageService, PrimeTemplate} from 'primeng/api'; import {UserService} from '../../../user.service'; import {AuthService} from '../../service/auth.service'; @@ -54,17 +54,14 @@ export class ChangePasswordComponent { return; } + if (this.currentPassword === this.newPassword) { + this.errorMessage = 'New password cannot be the same as the current password.'; + return; + } + this.userService.changePassword(this.currentPassword, this.newPassword).subscribe({ next: () => { this.successMessage = 'Password changed successfully!'; - this.currentPassword = ''; - this.newPassword = ''; - this.confirmNewPassword = ''; - this.messageService.add({ - severity: 'success', - summary: 'Password Changed', - detail: 'Your password has been changed successfully. Please log in again.' - }); this.logout(); }, error: (err) => { diff --git a/booklore-ui/src/app/core/component/settings/admin/admin.component.html b/booklore-ui/src/app/core/component/settings/admin/admin.component.html index 2f7269f03..22165af95 100644 --- a/booklore-ui/src/app/core/component/settings/admin/admin.component.html +++ b/booklore-ui/src/app/core/component/settings/admin/admin.component.html @@ -56,7 +56,7 @@ - +