|
1 | | -import type { PasswordSettingsData } from '@clerk/types'; |
2 | 1 | import { useCallback, useEffect, useMemo, useState } from 'react'; |
3 | 2 |
|
| 3 | +import type { ComplexityErrors, UsePasswordComplexityConfig } from '../../utils/passwords/complexity'; |
| 4 | +import { validate } from '../../utils/passwords/complexity'; |
4 | 5 | import type { LocalizationKey } from '../localization'; |
5 | 6 | import { localizationKeys, useLocalizations } from '../localization'; |
6 | 7 | import { addFullStop, createListFormat } from '../utils'; |
7 | 8 |
|
8 | | -export type ComplexityErrors = { |
9 | | - [key in keyof Partial<Omit<PasswordSettingsData, 'disable_hibp' | 'min_zxcvbn_strength' | 'show_zxcvbn'>>]?: boolean; |
10 | | -}; |
11 | | - |
12 | | -type UsePasswordComplexityConfig = Omit<PasswordSettingsData, 'disable_hibp' | 'min_zxcvbn_strength' | 'show_zxcvbn'>; |
13 | | - |
14 | | -const createTestComplexityCases = (config: Pick<UsePasswordComplexityConfig, 'allowed_special_characters'>) => { |
15 | | - let specialCharsRegex: RegExp; |
16 | | - if (config.allowed_special_characters) { |
17 | | - // Avoid a nested group by escaping the `[]` characters |
18 | | - let escaped = config.allowed_special_characters.replace('[', '\\['); |
19 | | - escaped = escaped.replace(']', '\\]'); |
20 | | - specialCharsRegex = new RegExp(`[${escaped}]`); |
21 | | - } else { |
22 | | - specialCharsRegex = /[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/; |
23 | | - } |
24 | | - |
25 | | - return ( |
26 | | - password: string, |
27 | | - { |
28 | | - minLength, |
29 | | - maxLength, |
30 | | - }: { |
31 | | - minLength: number; |
32 | | - maxLength: number; |
33 | | - }, |
34 | | - ) => { |
35 | | - return { |
36 | | - max_length: password.length < maxLength, |
37 | | - min_length: password.length >= minLength, |
38 | | - require_numbers: /\d/.test(password), |
39 | | - require_lowercase: /[a-z]/.test(password), |
40 | | - require_uppercase: /[A-Z]/.test(password), |
41 | | - require_special_char: specialCharsRegex.test(password), |
42 | | - }; |
43 | | - }; |
44 | | -}; |
45 | | - |
46 | | -const errorMessages = { |
| 9 | +const errorMessages: Record<keyof Omit<ComplexityErrors, 'allowed_special_characters'>, [string, string] | string> = { |
47 | 10 | max_length: ['unstable__errors.passwordComplexity.maximumLength', 'length'], |
48 | 11 | min_length: ['unstable__errors.passwordComplexity.minimumLength', 'length'], |
49 | 12 | require_numbers: 'unstable__errors.passwordComplexity.requireNumbers', |
@@ -89,44 +52,6 @@ export const generateErrorTextUtil = ({ |
89 | 52 | ); |
90 | 53 | }; |
91 | 54 |
|
92 | | -const validate = (password: string, config: UsePasswordComplexityConfig): ComplexityErrors => { |
93 | | - const { max_length, min_length, require_special_char, require_lowercase, require_numbers, require_uppercase } = |
94 | | - config; |
95 | | - const testComplexityCases = createTestComplexityCases(config); |
96 | | - const testCases = testComplexityCases(password, { |
97 | | - maxLength: config.max_length, |
98 | | - minLength: config.min_length, |
99 | | - }); |
100 | | - |
101 | | - const keys = { |
102 | | - max_length, |
103 | | - min_length, |
104 | | - require_special_char, |
105 | | - require_numbers, |
106 | | - require_lowercase, |
107 | | - require_uppercase, |
108 | | - }; |
109 | | - |
110 | | - const _validationsFailedMap = new Map(); |
111 | | - for (const k in keys) { |
112 | | - const key = k as keyof typeof keys; |
113 | | - |
114 | | - if (!keys[key]) { |
115 | | - continue; |
116 | | - } |
117 | | - |
118 | | - if (!testCases[key]) { |
119 | | - _validationsFailedMap.set(key, true); |
120 | | - } |
121 | | - } |
122 | | - |
123 | | - return Object.freeze(Object.fromEntries(_validationsFailedMap)); |
124 | | -}; |
125 | | - |
126 | | -export const createValidateComplexity = (config: UsePasswordComplexityConfig) => { |
127 | | - return (password: string) => validate(password, config); |
128 | | -}; |
129 | | - |
130 | 55 | export const usePasswordComplexity = (config: UsePasswordComplexityConfig) => { |
131 | 56 | const [password, _setPassword] = useState(''); |
132 | 57 | const [failedValidations, setFailedValidations] = useState<ComplexityErrors>(); |
|
0 commit comments