= (props) => {
operation,
readOnly,
requirePassword,
+ setSchemaPath,
+ setValidateBeforeSubmit,
useAPIKey,
username,
verify,
@@ -42,6 +45,7 @@ export const Auth: React.FC
= (props) => {
const { permissions } = useAuth()
const [changingPassword, setChangingPassword] = useState(requirePassword)
const enableAPIKey = useFormFields(([fields]) => (fields && fields?.enableAPIKey) || null)
+ const forceOpenChangePassword = useFormFields(([fields]) => (fields && fields?.password) || null)
const dispatchFields = useFormFields((reducer) => reducer[1])
const modified = useFormModified()
const { i18n, t } = useTranslation()
@@ -70,15 +74,32 @@ export const Auth: React.FC = (props) => {
}, [permissions, collectionSlug])
const handleChangePassword = useCallback(
- (state: boolean) => {
- if (!state) {
+ (showPasswordFields: boolean) => {
+ if (showPasswordFields) {
+ setValidateBeforeSubmit(true)
+ setSchemaPath(`_${collectionSlug}.auth`)
+ dispatchFields({
+ type: 'UPDATE',
+ errorMessage: t('validation:required'),
+ path: 'password',
+ valid: false,
+ })
+ dispatchFields({
+ type: 'UPDATE',
+ errorMessage: t('validation:required'),
+ path: 'confirm-password',
+ valid: false,
+ })
+ } else {
+ setValidateBeforeSubmit(false)
+ setSchemaPath(collectionSlug)
dispatchFields({ type: 'REMOVE', path: 'password' })
dispatchFields({ type: 'REMOVE', path: 'confirm-password' })
}
- setChangingPassword(state)
+ setChangingPassword(showPasswordFields)
},
- [dispatchFields],
+ [dispatchFields, t, collectionSlug, setSchemaPath, setValidateBeforeSubmit],
)
const unlock = useCallback(async () => {
@@ -99,7 +120,7 @@ export const Auth: React.FC = (props) => {
} else {
toast.error(t('authentication:failedToUnlock'))
}
- }, [i18n, serverURL, api, collectionSlug, email, username, t])
+ }, [i18n, serverURL, api, collectionSlug, email, username, t, loginWithUsername])
useEffect(() => {
if (!modified) {
@@ -113,6 +134,8 @@ export const Auth: React.FC = (props) => {
const disabled = readOnly || isInitializing
+ const showPasswordFields = changingPassword || forceOpenChangePassword
+
return (
{!disableLocalStrategy && (
@@ -136,22 +159,33 @@ export const Auth: React.FC
= (props) => {
name="email"
readOnly={readOnly}
required={!loginWithUsername || loginWithUsername?.requireEmail}
+ validate={(value) =>
+ emailValidation(value, {
+ name: 'email',
+ type: 'email',
+ data: {},
+ preferences: { fields: {} },
+ req: { t } as any,
+ required: true,
+ siblingData: {},
+ })
+ }
/>
)}
- {(changingPassword || requirePassword) && (
+ {(showPasswordFields || requirePassword) && (
)}
- {changingPassword && !requirePassword && (
+ {showPasswordFields && !requirePassword && (
)}
- {!changingPassword && !requirePassword && (
+ {!showPasswordFields && !requirePassword && (