feat(auth): password strongest + docs
This commit is contained in:
@@ -22,6 +22,17 @@ export async function POST(request: Request) {
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
if (error instanceof Error && error.message === "PASSWORD_NOT_STRONG") {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: {
|
||||
code: "PASSWORD_NOT_STRONG",
|
||||
message: "Le mot de passe est trop faible",
|
||||
},
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
@@ -13,6 +13,11 @@ export class AuthServerService {
|
||||
static async createUser(email: string, password: string): Promise<UserData> {
|
||||
await connectDB();
|
||||
|
||||
//check if password is strong
|
||||
if (!AuthServerService.isPasswordStrong(password)) {
|
||||
throw new Error("PASSWORD_NOT_STRONG");
|
||||
}
|
||||
|
||||
// Check if user already exists
|
||||
const existingUser = await UserModel.findOne({ email: email.toLowerCase() });
|
||||
if (existingUser) {
|
||||
@@ -36,6 +41,22 @@ export class AuthServerService {
|
||||
|
||||
return userData;
|
||||
}
|
||||
static isPasswordStrong(password: string): boolean {
|
||||
//check if password is strong
|
||||
if (password.length < 8) {
|
||||
return false;
|
||||
}
|
||||
if (!/[A-Z]/.test(password)) {
|
||||
return false;
|
||||
}
|
||||
if (!/[0-9]/.test(password)) {
|
||||
return false;
|
||||
}
|
||||
if (!/[!@#$%^&*]/.test(password)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static setUserCookie(userData: UserData): void {
|
||||
// Encode user data in base64
|
||||
|
||||
Reference in New Issue
Block a user