New password cannot be same as the current one

This commit is contained in:
aditya.chandel
2025-03-12 14:31:39 -06:00
parent a8c9d1324c
commit 89eb7724a0
4 changed files with 22 additions and 16 deletions
@@ -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;
@@ -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);
@@ -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) => {
@@ -56,7 +56,7 @@
</td>
<td class="text-center">
<ng-container *ngIf="user.username !== 'admin'">
<p-button *ngIf="!user.isEditing" icon="pi pi-pencil" outlined="true" severity="primary" (onClick)="toggleEdit(user)"></p-button>
<p-button *ngIf="!user.isEditing" icon="pi pi-pencil" outlined="true" severity="info" (onClick)="toggleEdit(user)"></p-button>
<p-button *ngIf="user.isEditing" icon="pi pi-check" outlined="true" severity="success" (onClick)="saveUser(user)"></p-button>
<p-button *ngIf="user.isEditing" icon="pi pi-times" outlined="true" severity="danger" (onClick)="toggleEdit(user)"></p-button>
</ng-container>