mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 12:24:54 +00:00
Merge pull request #62 from SyncrowIOT/SP-275-be-implement-validation-for-the-password-in-the-sign-up-and-forget-password
Sp 275 be implement validation for the password in the sign up and forget password
This commit is contained in:
@ -1,5 +1,6 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsEmail, IsNotEmpty, IsString } from 'class-validator';
|
import { IsEmail, IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
import { IsPasswordStrong } from 'src/validators/password.validator';
|
||||||
|
|
||||||
export class UserSignUpDto {
|
export class UserSignUpDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
@ -16,6 +17,10 @@ export class UserSignUpDto {
|
|||||||
})
|
})
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
|
@IsPasswordStrong({
|
||||||
|
message:
|
||||||
|
'password must be at least 8 characters long and include at least one uppercase letter, one lowercase letter, one numeric digit, and one special character.',
|
||||||
|
})
|
||||||
public password: string;
|
public password: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
|
|||||||
@ -1,14 +1,25 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsEmail, IsNotEmpty, IsString } from 'class-validator';
|
import { IsEmail, IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
import { IsPasswordStrong } from 'src/validators/password.validator';
|
||||||
|
|
||||||
export class ForgetPasswordDto {
|
export class ForgetPasswordDto {
|
||||||
@ApiProperty()
|
@ApiProperty({
|
||||||
|
description: 'email',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
@IsEmail()
|
@IsEmail()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
email: string;
|
public email: string;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty({
|
||||||
|
description: 'password',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
password: string;
|
@IsPasswordStrong({
|
||||||
|
message:
|
||||||
|
'password must be at least 8 characters long and include at least one uppercase letter, one lowercase letter, one numeric digit, and one special character.',
|
||||||
|
})
|
||||||
|
public password: string;
|
||||||
}
|
}
|
||||||
|
|||||||
33
src/validators/password.validator.ts
Normal file
33
src/validators/password.validator.ts
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import {
|
||||||
|
registerDecorator,
|
||||||
|
ValidationOptions,
|
||||||
|
ValidatorConstraint,
|
||||||
|
ValidatorConstraintInterface,
|
||||||
|
} from 'class-validator';
|
||||||
|
|
||||||
|
@ValidatorConstraint({ async: false })
|
||||||
|
export class IsPasswordStrongConstraint
|
||||||
|
implements ValidatorConstraintInterface
|
||||||
|
{
|
||||||
|
validate(password: string) {
|
||||||
|
const regex =
|
||||||
|
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
|
||||||
|
return regex.test(password);
|
||||||
|
}
|
||||||
|
|
||||||
|
defaultMessage() {
|
||||||
|
return 'password must be at least 8 characters long and include at least one uppercase letter, one lowercase letter, one numeric digit, and one special character.';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function IsPasswordStrong(validationOptions?: ValidationOptions) {
|
||||||
|
return function (object: Record<string, any>, propertyName: string) {
|
||||||
|
registerDecorator({
|
||||||
|
target: object.constructor,
|
||||||
|
propertyName: propertyName,
|
||||||
|
options: validationOptions,
|
||||||
|
constraints: [],
|
||||||
|
validator: IsPasswordStrongConstraint,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user