@@ -40,6 +40,26 @@ import { injectRecaptchaFields } from '../../platform_browser/recaptcha/recaptch
40
40
import { IdTokenResponse } from '../../model/id_token' ;
41
41
import { RecaptchaActionName , RecaptchaClientType } from '../../api' ;
42
42
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
+
43
63
/**
44
64
* Sends a password reset email to the given email address.
45
65
*
@@ -154,10 +174,21 @@ export async function confirmPasswordReset(
154
174
oobCode : string ,
155
175
newPassword : string
156
176
) : 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
+ } ) ;
161
192
// Do not return the email.
162
193
}
163
194
@@ -308,14 +339,11 @@ export async function createUserWithEmailAndPassword(
308
339
) ;
309
340
return signUp ( authInternal , requestWithRecaptcha ) ;
310
341
} 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.
313
342
if (
314
343
error . code ===
315
- `auth/${ AuthErrorCode . PASSWORD_DOES_NOT_MEET_REQUIREMENTS } ` &&
316
- authInternal . _getPasswordPolicy ( )
344
+ `auth/${ AuthErrorCode . PASSWORD_DOES_NOT_MEET_REQUIREMENTS } `
317
345
) {
318
- await authInternal . _updatePasswordPolicy ( ) ;
346
+ await updatePasswordPolicyIfCached ( auth ) ;
319
347
}
320
348
321
349
return Promise . reject ( error ) ;
0 commit comments