@adonis-agora/authkit-react
Version:
Frontend ergonomics over AuthKit for AdonisJS + Inertia + React apps: a typed useAuth() hook, role-gating hooks and gating components.
35 lines (34 loc) • 1.21 kB
JavaScript
import { useMemo } from 'react';
export function heuristicScorer(password) {
if (!password)
return { score: 0, feedback: ['Enter a password.'] };
const feedback = [];
const classes = [/[a-z]/, /[A-Z]/, /[0-9]/, /[^A-Za-z0-9]/];
const variety = classes.filter((re) => re.test(password)).length;
const length = password.length;
let points = 0;
if (length >= 8)
points += 1;
if (length >= 12)
points += 1;
if (length >= 16)
points += 1;
if (variety >= 2)
points += 1;
if (variety >= 3)
points += 1;
if (length < 12)
feedback.push('Use at least 12 characters.');
if (!/[A-Z]/.test(password))
feedback.push('Add an uppercase letter.');
if (!/[0-9]/.test(password))
feedback.push('Add a number.');
if (!/[^A-Za-z0-9]/.test(password))
feedback.push('Add a symbol.');
const score = Math.max(0, Math.min(4, points));
return { score, feedback: feedback.length ? feedback : undefined };
}
export function usePasswordStrength(password, options = {}) {
const scorer = options.scorer ?? heuristicScorer;
return useMemo(() => scorer(password), [password, scorer]);
}