Files
supabase/apps/studio/lib/password-strength.ts
T
Joshen Lim e539b892a6 Chore/more refactoring of data fetchers methods (#33553)
* Update postgres-service-status-query, password-strength, and free-project-limit-check-query

* Update exit-survey-send

* Update project-postgrest-config-query

* Update project-disk-resize-mutation

* Update project-update-mutation

* Update project-postgrest-config-update-mutation

* Update organization-audit-logs-query

* Fix types

* Update bucket-object-download-mutation

* Update organization-member-delete-invitation

* remove console log

* Add integer validation for disk size (enforced on the API, but lacking client side validation)

* Fix audit logs
2025-02-13 13:34:12 +08:00

49 lines
1.5 KiB
TypeScript

import { post as post_ } from 'data/fetchers'
import { DEFAULT_MINIMUM_PASSWORD_STRENGTH, PASSWORD_STRENGTH } from 'lib/constants'
import { toast } from 'sonner'
import { ResponseError } from 'types'
export default async function passwordStrength(value: string) {
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 { data, error } = await post_('/platform/profile/password-check', {
body: { password: value },
})
if (!error) {
const { result } = data
const resultScore = result?.score ?? 0
const score = (PASSWORD_STRENGTH as any)[resultScore]
const suggestions = result.feedback?.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.`
}
} else {
toast.error(`Failed to check password strength: ${(error as ResponseError).message}`)
}
}
}
return {
message,
warning,
strength,
}
}