-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
199 additions
and
7 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
4 changes: 4 additions & 0 deletions
4
packages/react/src/components/AccountSettings/ChangePassword/types.ts
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 |
---|---|---|
@@ -1,6 +1,10 @@ | ||
import { FieldValidator } from '@aws-amplify/ui'; | ||
|
||
export interface ChangePasswordProps { | ||
// callback once password is successfully updated | ||
onSuccess?: () => void; | ||
// callback when there's an error | ||
onError?: (error: Error) => void; | ||
// custom password validation | ||
validate?: FieldValidator[]; | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { changePassword } from './utils'; | ||
export * from './utils'; | ||
export * from './validator'; |
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,84 @@ | ||
import { | ||
ConfirmPasswordValidator, | ||
FieldValidator, | ||
MinLengthValidator, | ||
} from '../../types/accountSettings/validator'; | ||
import { hasAllowedSpecialChars } from '../authenticator'; | ||
import { getPasswordSettings } from './utils'; | ||
|
||
// TODO: consolidate these with Authenticator validators | ||
export const minLength: MinLengthValidator = (minLength: number) => { | ||
return (field: string) => { | ||
if (field?.length < minLength) { | ||
return `Password must have at least ${minLength} characters`; | ||
} | ||
}; | ||
}; | ||
|
||
export const hasLowerCase: FieldValidator = (field: string) => { | ||
if (!/[a-z]/.test(field)) { | ||
return 'Password must have lower case letters'; | ||
} | ||
}; | ||
|
||
export const hasUpperCase: FieldValidator = (field: string) => { | ||
if (!/[a-z]/.test(field)) { | ||
return 'Password must have lower case letters'; | ||
} | ||
}; | ||
|
||
export const hasNumbers: FieldValidator = (field: string) => { | ||
if (!/[0-9]/.test(field)) { | ||
return 'Password must have numbers'; | ||
} | ||
}; | ||
|
||
export const hasSpecialChars = (field: string) => { | ||
if (!hasAllowedSpecialChars(field)) { | ||
return 'Password must have special characters'; | ||
} | ||
}; | ||
|
||
export const confirmPasswordMatch: ConfirmPasswordValidator = ( | ||
newPassword, | ||
confirmPassword | ||
) => { | ||
if (newPassword !== confirmPassword) { | ||
return 'Your passwords must match'; | ||
} | ||
}; | ||
|
||
export const getDefaultPasswordValidator = (): FieldValidator[] => { | ||
const validators: FieldValidator[] = []; | ||
const passwordSettings = getPasswordSettings(); | ||
|
||
const minLengthPoilicy = passwordSettings?.passwordPolicyMinLength as number; | ||
if (minLengthPoilicy) { | ||
validators.push(minLength(minLengthPoilicy)); | ||
} | ||
|
||
const passwordPolicy = passwordSettings?.passwordPolicyCharacters as string[]; | ||
if (passwordPolicy) { | ||
passwordPolicy.forEach((policyName) => { | ||
switch (policyName) { | ||
case 'REQUIRES_LOWERCASE': { | ||
validators.push(hasLowerCase); | ||
} | ||
case 'REQUIRES_UPPERCASE': { | ||
validators.push(hasUpperCase); | ||
} | ||
case 'REQUIRES_NUMBERS': { | ||
validators.push(hasNumbers); | ||
} | ||
case 'REQUIRES_SYMBOLS': { | ||
validators.push(hasSpecialChars); | ||
break; | ||
} | ||
default: { | ||
break; | ||
} | ||
} | ||
}); | ||
return validators; | ||
} | ||
}; |
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 @@ | ||
export * from './validator'; |
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,8 @@ | ||
export type FieldValidator = (password: string) => string; | ||
|
||
export type MinLengthValidator = (minLength: number) => FieldValidator; | ||
|
||
export type ConfirmPasswordValidator = ( | ||
newPassword: string, | ||
confirmPassword: string | ||
) => string; |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './authenticator'; | ||
export * from './accountSettings'; | ||
export * from './primitives'; | ||
export * from './util'; |