From b26348fcce70576d844a836b49b85df51a1f110d Mon Sep 17 00:00:00 2001 From: Miodec Date: Wed, 28 Sep 2022 12:43:19 +0200 Subject: [PATCH] extracted strong password check to a util function --- .../src/ts/controllers/account-controller.ts | 40 ++++++++----------- frontend/src/ts/utils/misc.ts | 8 ++++ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/frontend/src/ts/controllers/account-controller.ts b/frontend/src/ts/controllers/account-controller.ts index fd226876c..8bb375a80 100644 --- a/frontend/src/ts/controllers/account-controller.ts +++ b/frontend/src/ts/controllers/account-controller.ts @@ -540,30 +540,6 @@ async function signUp(): Promise { return; } - // Force user to use a capital letter, number, special character when setting up an account and changing password - if (password.length < 8) { - Notifications.add("Password must be at least 8 characters", 0, 3); - LoginPage.hidePreloader(); - LoginPage.enableInputs(); - LoginPage.updateSignupButton(); - return; - } - - const hasCapital = password.match(/[A-Z]/); - const hasNumber = password.match(/[\d]/); - const hasSpecial = password.match(/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/); - if (!hasCapital || !hasNumber || !hasSpecial) { - Notifications.add( - "Password must contain at least one capital letter, number, and special character", - 0, - 3 - ); - LoginPage.hidePreloader(); - LoginPage.enableInputs(); - LoginPage.updateSignupButton(); - return; - } - if (password !== passwordVerify) { Notifications.add("Passwords do not match", 0, 3); LoginPage.hidePreloader(); @@ -572,6 +548,22 @@ async function signUp(): Promise { return; } + // Force user to use a capital letter, number, special character when setting up an account and changing password + if ( + window.location.hostname !== "localhost" && + !Misc.isPasswordStrong(password) + ) { + Notifications.add( + "Password must contain at least one capital letter, number, a special character and at least 8 characters long", + 0, + 4 + ); + LoginPage.hidePreloader(); + LoginPage.enableInputs(); + LoginPage.updateSignupButton(); + return; + } + authListener(); let createdAuthUser; diff --git a/frontend/src/ts/utils/misc.ts b/frontend/src/ts/utils/misc.ts index 5cb31d492..b72fa1f3f 100644 --- a/frontend/src/ts/utils/misc.ts +++ b/frontend/src/ts/utils/misc.ts @@ -1235,3 +1235,11 @@ export function abbreviateNumber(num: number): string { export async function sleep(ms: number): Promise { return new Promise((resolve) => setTimeout(resolve, ms)); } + +export function isPasswordStrong(password: string): boolean { + const hasCapital = !!password.match(/[A-Z]/); + const hasNumber = !!password.match(/[\d]/); + const hasSpecial = !!password.match(/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/); + const isLong = password.length >= 8; + return hasCapital && hasNumber && hasSpecial && isLong; +}