mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-08-26 06:09:41 +00:00
feat: add change password api
This commit is contained in:
@ -11,6 +11,7 @@ import { UserType } from '~/user/enums';
|
||||
import { DeviceService, UserService, UserTokenService } from '~/user/services';
|
||||
import { User } from '../../user/entities';
|
||||
import {
|
||||
ChangePasswordRequestDto,
|
||||
CreateUnverifiedUserRequestDto,
|
||||
DisableBiometricRequestDto,
|
||||
EnableBiometricRequestDto,
|
||||
@ -172,6 +173,7 @@ export class AuthService {
|
||||
|
||||
return { token, user };
|
||||
}
|
||||
|
||||
async resetPassword({
|
||||
countryCode,
|
||||
phoneNumber,
|
||||
@ -191,6 +193,15 @@ export class AuthService {
|
||||
throw new BadRequestException('AUTH.PASSWORD_MISMATCH');
|
||||
}
|
||||
|
||||
const isOldPassword = bcrypt.compareSync(password, user.password);
|
||||
|
||||
if (isOldPassword) {
|
||||
this.logger.error(
|
||||
`New password cannot be the same as the current password for user with phone number ${user.fullPhoneNumber}`,
|
||||
);
|
||||
throw new BadRequestException('AUTH.PASSWORD_SAME_AS_CURRENT');
|
||||
}
|
||||
|
||||
const hashedPassword = bcrypt.hashSync(password, user.salt);
|
||||
|
||||
await this.userService.setPassword(user.id, hashedPassword, user.salt);
|
||||
@ -198,6 +209,38 @@ export class AuthService {
|
||||
this.logger.log(`Passcode updated successfully for user with phone number ${user.fullPhoneNumber}`);
|
||||
}
|
||||
|
||||
async changePassword(userId: string, { currentPassword, newPassword, confirmNewPassword }: ChangePasswordRequestDto) {
|
||||
const user = await this.userService.findUserOrThrow({ id: userId });
|
||||
|
||||
if (!user.isPasswordSet) {
|
||||
this.logger.error(`Password not set for user with id ${userId}`);
|
||||
throw new BadRequestException('AUTH.PASSWORD_NOT_SET');
|
||||
}
|
||||
|
||||
if (currentPassword === newPassword) {
|
||||
this.logger.error('New password cannot be the same as current password');
|
||||
throw new BadRequestException('AUTH.PASSWORD_SAME_AS_CURRENT');
|
||||
}
|
||||
|
||||
if (newPassword !== confirmNewPassword) {
|
||||
this.logger.error('New password and confirm new password do not match');
|
||||
throw new BadRequestException('AUTH.PASSWORD_MISMATCH');
|
||||
}
|
||||
|
||||
this.logger.log(`Validating current password for user with id ${userId}`);
|
||||
const isCurrentPasswordValid = bcrypt.compareSync(currentPassword, user.password);
|
||||
|
||||
if (!isCurrentPasswordValid) {
|
||||
this.logger.error(`Invalid current password for user with id ${userId}`);
|
||||
throw new UnauthorizedException('AUTH.INVALID_CURRENT_PASSWORD');
|
||||
}
|
||||
|
||||
const salt = bcrypt.genSaltSync(SALT_ROUNDS);
|
||||
const hashedNewPassword = bcrypt.hashSync(newPassword, salt);
|
||||
await this.userService.setPassword(user.id, hashedNewPassword, salt);
|
||||
this.logger.log(`Password changed successfully for user with id ${userId}`);
|
||||
}
|
||||
|
||||
async setJuniorPasscode(body: setJuniorPasswordRequestDto) {
|
||||
this.logger.log(`Setting passcode for junior with qrToken ${body.qrToken}`);
|
||||
const juniorId = await this.userTokenService.validateToken(body.qrToken, UserType.JUNIOR);
|
||||
|
Reference in New Issue
Block a user