-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clerk-js): Expose password validation from SignIn and SignUp res…
…ources (#1445) * feat(clerk-js): Expose password validation from SignIn and SignUp resources * chore(clerk-js): Use ZxcvbnResult from `@types` * chore(repo): Example usage password validation in custom flows
- Loading branch information
1 parent
4ea30e8
commit 8538cd0
Showing
16 changed files
with
277 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@clerk/clerk-js': minor | ||
'@clerk/types': minor | ||
--- | ||
|
||
Introducing validatePassword for SignIn and SignUp resources | ||
- Validate a password based on the instance's configuration set in Password Policies in Dashboard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import type { PasswordSettingsData } from '@clerk/types'; | ||
|
||
export type ComplexityErrors = { | ||
[key in keyof Partial<Omit<PasswordSettingsData, 'disable_hibp' | 'min_zxcvbn_strength' | 'show_zxcvbn'>>]?: boolean; | ||
}; | ||
|
||
export type UsePasswordComplexityConfig = Omit< | ||
PasswordSettingsData, | ||
'disable_hibp' | 'min_zxcvbn_strength' | 'show_zxcvbn' | ||
>; | ||
|
||
const createTestComplexityCases = (config: Pick<UsePasswordComplexityConfig, 'allowed_special_characters'>) => { | ||
let specialCharsRegex: RegExp; | ||
if (config.allowed_special_characters) { | ||
// Avoid a nested group by escaping the `[]` characters | ||
let escaped = config.allowed_special_characters.replace('[', '\\['); | ||
escaped = escaped.replace(']', '\\]'); | ||
specialCharsRegex = new RegExp(`[${escaped}]`); | ||
} else { | ||
specialCharsRegex = /[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/; | ||
} | ||
|
||
return ( | ||
password: string, | ||
{ | ||
minLength, | ||
maxLength, | ||
}: { | ||
minLength: number; | ||
maxLength: number; | ||
}, | ||
) => { | ||
return { | ||
max_length: password.length < maxLength, | ||
min_length: password.length >= minLength, | ||
require_numbers: /\d/.test(password), | ||
require_lowercase: /[a-z]/.test(password), | ||
require_uppercase: /[A-Z]/.test(password), | ||
require_special_char: specialCharsRegex.test(password), | ||
}; | ||
}; | ||
}; | ||
|
||
export const validate = (password: string, config: UsePasswordComplexityConfig): ComplexityErrors => { | ||
const { max_length, min_length, require_special_char, require_lowercase, require_numbers, require_uppercase } = | ||
config; | ||
const testComplexityCases = createTestComplexityCases(config); | ||
const testCases = testComplexityCases(password, { | ||
maxLength: config.max_length, | ||
minLength: config.min_length, | ||
}); | ||
|
||
const keys = { | ||
max_length, | ||
min_length, | ||
require_special_char, | ||
require_lowercase, | ||
require_numbers, | ||
require_uppercase, | ||
}; | ||
|
||
const _validationsFailedMap = new Map(); | ||
for (const k in keys) { | ||
const key = k as keyof typeof keys; | ||
|
||
if (!keys[key]) { | ||
continue; | ||
} | ||
|
||
if (!testCases[key]) { | ||
_validationsFailedMap.set(key, true); | ||
} | ||
} | ||
|
||
return Object.freeze(Object.fromEntries(_validationsFailedMap)); | ||
}; | ||
|
||
export const createValidateComplexity = (config: UsePasswordComplexityConfig) => { | ||
return (password: string) => validate(password, config); | ||
}; |
Oops, something went wrong.