Files
supabase/apps/studio/lib/password-strength.ts
Alaister YoungandCharis a3e7cb7285 chore: move password check to frontend (#39927)
* chore: move password check to frontend

* remove barrel file export and fix test import

* move import inside function

* Update apps/studio/lib/password-strength.ts

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>

* fix test

---------

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>
2025-10-28 23:24:35 +08:00

40 lines
1.2 KiB
TypeScript

import { DEFAULT_MINIMUM_PASSWORD_STRENGTH, PASSWORD_STRENGTH } from 'lib/constants'
export async function passwordStrength(value: string) {
// [Alaister]: Lazy load zxcvbn to avoid bundling it with the main app (it's pretty chunky)
const zxcvbn = await import('zxcvbn').then((module) => module.default)
let message: string = ''
let warning: string = ''
let strength: number = 0
if (value && value !== '') {
if (value.length > 99) {
message = `${PASSWORD_STRENGTH[0]} Maximum length of password exceeded`
warning = `Password should be less than 100 characters`
} else {
const result = zxcvbn(value)
const resultScore = result?.score ?? 0
const score = (PASSWORD_STRENGTH as any)[resultScore]
const suggestions = result.feedback?.suggestions?.join(' ') ?? ''
message = `${score} ${suggestions}`
strength = resultScore
// warning message for anything below 4 strength :string
if (resultScore < DEFAULT_MINIMUM_PASSWORD_STRENGTH) {
warning = `${
result?.feedback?.warning ? result?.feedback?.warning + '.' : ''
} You need a stronger password.`
}
}
}
return {
message,
warning,
strength,
}
}