Publish Docker image / build-and-push (push) Canceled after 0s
GitHub Release / release (push) Canceled after 0s
ci / ci (22, ubuntu-latest) (push) Canceled after 0s
Add i18n (zh-CN/en), mobile navigation menus, stats and upload UI improvements, and Docker reset-password CLI for emergency recovery.
40 lines
980 B
TypeScript
40 lines
980 B
TypeScript
export const PASSWORD_MIN_LENGTH = 8
|
|
|
|
export interface PasswordCriteria {
|
|
minLength: boolean
|
|
mixedCase: boolean
|
|
hasNumber: boolean
|
|
hasSpecial: boolean
|
|
}
|
|
|
|
export type PasswordStrengthLabel = 'weak' | 'medium' | 'strong'
|
|
|
|
export function analyzePassword(password: string): {
|
|
criteria: PasswordCriteria
|
|
score: number
|
|
label: PasswordStrengthLabel
|
|
isValid: boolean
|
|
} {
|
|
const criteria: PasswordCriteria = {
|
|
minLength: password.length >= PASSWORD_MIN_LENGTH,
|
|
mixedCase: /[a-z]/.test(password) && /[A-Z]/.test(password),
|
|
hasNumber: /\d/.test(password),
|
|
hasSpecial: /[^a-zA-Z0-9]/.test(password)
|
|
}
|
|
|
|
const score = Object.values(criteria).filter(Boolean).length
|
|
const label: PasswordStrengthLabel
|
|
= score <= 1 ? 'weak' : score <= 3 ? 'medium' : 'strong'
|
|
|
|
return {
|
|
criteria,
|
|
score,
|
|
label,
|
|
isValid: criteria.minLength
|
|
}
|
|
}
|
|
|
|
export function isPasswordValid(password: string): boolean {
|
|
return analyzePassword(password).isValid
|
|
}
|