mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
feat(auth): password rules checked while typing, with the server's own policy
The password policy moves to @trek/shared, where the server keeps enforcing it unchanged and every form that sets a password now runs it before sending: the forced change after the first login, registration, the reset link, the account settings on desktop and phone, and creating or editing a user in admin. Under each new password a checklist ticks off length, upper and lower case, a number and a special character, and a refusal names the actual reason, a too common or too repetitive password included. (#1200)
This commit is contained in:
@@ -1,28 +1,3 @@
|
||||
const COMMON_PASSWORDS = new Set([
|
||||
'password', '12345678', '123456789', '1234567890', 'password1',
|
||||
'qwerty123', 'iloveyou', 'admin123', 'letmein12', 'welcome1',
|
||||
'monkey123', 'dragon12', 'master12', 'qwerty12', 'abc12345',
|
||||
'trustno1', 'baseball', 'football', 'shadow12', 'michael1',
|
||||
'jennifer', 'superman', 'abcdefgh', 'abcd1234', 'password123',
|
||||
'admin1234', 'changeme', 'welcome123', 'passw0rd', 'p@ssword',
|
||||
]);
|
||||
|
||||
export function validatePassword(password: string): { ok: boolean; reason?: string } {
|
||||
if (password.length < 8) return { ok: false, reason: 'Password must be at least 8 characters' };
|
||||
|
||||
if (/^(.)\1+$/.test(password)) {
|
||||
return { ok: false, reason: 'Password is too repetitive' };
|
||||
}
|
||||
|
||||
if (COMMON_PASSWORDS.has(password.toLowerCase())) {
|
||||
return { ok: false, reason: 'Password is too common. Please choose a unique password.' };
|
||||
}
|
||||
|
||||
const requirementsMessage = 'Password must contain at least one uppercase letter, one lowercase letter, one number, and one special character';
|
||||
if (!/[A-Z]/.test(password)) return { ok: false, reason: requirementsMessage };
|
||||
if (!/[a-z]/.test(password)) return { ok: false, reason: requirementsMessage };
|
||||
if (!/[0-9]/.test(password)) return { ok: false, reason: requirementsMessage };
|
||||
if (!/[^A-Za-z0-9]/.test(password)) return { ok: false, reason: requirementsMessage };
|
||||
|
||||
return { ok: true };
|
||||
}
|
||||
// The policy lives in @trek/shared so the forms can check a password the same way
|
||||
// while it is typed (#1200); the server keeps enforcing it from here.
|
||||
export { validatePassword } from '@trek/shared';
|
||||
|
||||
Reference in New Issue
Block a user