Skip to content

Commit 1067b56

Browse files
committed
Update confirmPasswordReset to handle password requirements error
1 parent 3a7c4a3 commit 1067b56

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

packages/auth/src/core/strategies/email_and_password.ts

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ import { injectRecaptchaFields } from '../../platform_browser/recaptcha/recaptch
4040
import { IdTokenResponse } from '../../model/id_token';
4141
import { RecaptchaActionName, RecaptchaClientType } from '../../api';
4242

43+
/**
44+
* Updates the password policy cached in the {@link Auth} instance if a policy is already
45+
* cached for the project or tenant.
46+
*
47+
* @remarks
48+
* We only fetch the password policy if the password did not meet policy requirements and
49+
* there is an existing policy cached. A developer must call validatePassword at least
50+
* once for the cache to be automatically updated.
51+
*
52+
* @param auth - The {@link Auth} instance.
53+
*
54+
* @private
55+
*/
56+
async function updatePasswordPolicyIfCached(auth: Auth): Promise<void> {
57+
const authInternal = _castAuth(auth);
58+
if (authInternal._getPasswordPolicy()) {
59+
await authInternal._updatePasswordPolicy();
60+
}
61+
}
62+
4363
/**
4464
* Sends a password reset email to the given email address.
4565
*
@@ -154,10 +174,21 @@ export async function confirmPasswordReset(
154174
oobCode: string,
155175
newPassword: string
156176
): Promise<void> {
157-
await account.resetPassword(getModularInstance(auth), {
158-
oobCode,
159-
newPassword
160-
});
177+
await account
178+
.resetPassword(getModularInstance(auth), {
179+
oobCode,
180+
newPassword
181+
})
182+
.catch(async error => {
183+
if (
184+
error.code ===
185+
`auth/${AuthErrorCode.PASSWORD_DOES_NOT_MEET_REQUIREMENTS}`
186+
) {
187+
await updatePasswordPolicyIfCached(auth);
188+
}
189+
190+
return Promise.reject(error);
191+
});
161192
// Do not return the email.
162193
}
163194

@@ -308,14 +339,11 @@ export async function createUserWithEmailAndPassword(
308339
);
309340
return signUp(authInternal, requestWithRecaptcha);
310341
} else {
311-
// Only fetch the password policy if the password did not meet policy requirements and there is an existing policy cached.
312-
// A developer must call validatePassword at least once for the cache to be automatically updated.
313342
if (
314343
error.code ===
315-
`auth/${AuthErrorCode.PASSWORD_DOES_NOT_MEET_REQUIREMENTS}` &&
316-
authInternal._getPasswordPolicy()
344+
`auth/${AuthErrorCode.PASSWORD_DOES_NOT_MEET_REQUIREMENTS}`
317345
) {
318-
await authInternal._updatePasswordPolicy();
346+
await updatePasswordPolicyIfCached(auth);
319347
}
320348

321349
return Promise.reject(error);

0 commit comments

Comments
 (0)