Skip to content

Commit 6f912b7

Browse files
author
chuanr
committed
Align recaptcha error codes with android
1 parent b8a481e commit 6f912b7

File tree

6 files changed

+53
-50
lines changed

6 files changed

+53
-50
lines changed

common/api-review/auth.api.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,14 @@ export const AuthErrorCodes: {
227227
readonly WEAK_PASSWORD: "auth/weak-password";
228228
readonly WEB_STORAGE_UNSUPPORTED: "auth/web-storage-unsupported";
229229
readonly ALREADY_INITIALIZED: "auth/already-initialized";
230-
readonly RECAPTCHA_CHECK_FAILED: "auth/recaptcha-check-failed";
231230
readonly RECAPTCHA_NOT_ENABLED: "auth/recaptcha-not-enabled";
231+
readonly MISSING_RECAPTCHA_TOKEN: "auth/missing-recaptcha-token";
232+
readonly INVALID_RECAPTCHA_TOKEN: "auth/invalid-recaptcha-token";
233+
readonly INVALID_RECAPTCHA_ACTION: "auth/invalide-recaptcha-action";
234+
readonly MISSING_CLIENT_TYPE: "auth/missing-client-type";
235+
readonly MISSING_RECAPTCHA_VERSION: "auth/missing-recaptcha-version";
236+
readonly INVALID_RECAPTCHA_VERSION: "auth/invalid-recaptcha-version";
237+
readonly INVALID_REQ_TYPE: "auth/invalid-req-type";
232238
};
233239

234240
// @public

packages/auth/src/api/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ export const SERVER_ERROR_MAP: Partial<ServerErrorMap<ServerError>> = {
216216
[ServerError.BLOCKING_FUNCTION_ERROR_RESPONSE]: AuthErrorCode.INTERNAL_ERROR,
217217

218218
// Recaptcha related errors.
219-
[ServerError.INVALID_RECAPTCHA_SCORE]: AuthErrorCode.RECAPTCHA_CHECK_FAILED,
219+
[ServerError.INVALID_RECAPTCHA_SCORE]: AuthErrorCode.CAPTCHA_CHECK_FAILED,
220220
[ServerError.RECAPTCHA_NOT_ENABLED]: AuthErrorCode.RECAPTCHA_NOT_ENABLED,
221221
[ServerError.MISSING_RECAPTCHA_TOKEN]: AuthErrorCode.INTERNAL_ERROR,
222222
[ServerError.INVALID_RECAPTCHA_TOKEN]: AuthErrorCode.INTERNAL_ERROR,

packages/auth/src/core/credentials/email.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import {
2929
import { AuthInternal } from '../../model/auth';
3030
import { IdTokenResponse } from '../../model/id_token';
3131
import { AuthErrorCode } from '../errors';
32-
import { ServerError } from '../../api/errors';
3332
import { _fail } from '../util/assert';
3433
import { AuthCredential } from './auth_credential';
3534
import { injectRecaptchaFields } from '../../platform_browser/recaptcha/recaptcha_enterprise_verifier';
@@ -132,7 +131,9 @@ export class EmailAuthCredential extends AuthCredential {
132131
return signInWithPassword(auth, requestWithRecaptcha);
133132
} else {
134133
return signInWithPassword(auth, request).catch(async error => {
135-
if (error.code === `auth/${ServerError.MISSING_RECAPTCHA_TOKEN}`) {
134+
if (
135+
error.code === `auth/${AuthErrorCode.MISSING_RECAPTCHA_TOKEN}`
136+
) {
136137
console.log(
137138
'Sign-in with email address and password is protected by reCAPTCHA for this project. Automatically triggering the reCAPTCHA flow and restarting the sign-in flow.'
138139
);

packages/auth/src/core/errors.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,14 @@ export const enum AuthErrorCode {
124124
WEAK_PASSWORD = 'weak-password',
125125
WEB_STORAGE_UNSUPPORTED = 'web-storage-unsupported',
126126
ALREADY_INITIALIZED = 'already-initialized',
127-
RECAPTCHA_CHECK_FAILED = 'recaptcha-check-failed',
128-
RECAPTCHA_NOT_ENABLED = 'recaptcha-not-enabled'
127+
RECAPTCHA_NOT_ENABLED = 'recaptcha-not-enabled',
128+
MISSING_RECAPTCHA_TOKEN = 'missing-recaptcha-token',
129+
INVALID_RECAPTCHA_TOKEN = 'invalid-recaptcha-token',
130+
INVALID_RECAPTCHA_ACTION = 'invalide-recaptcha-action',
131+
MISSING_CLIENT_TYPE = 'missing-client-type',
132+
MISSING_RECAPTCHA_VERSION = 'missing-recaptcha-version',
133+
INVALID_RECAPTCHA_VERSION = 'invalid-recaptcha-version',
134+
INVALID_REQ_TYPE = 'invalid-req-type'
129135
}
130136

131137
function _debugErrorMap(): ErrorMap<AuthErrorCode> {
@@ -359,10 +365,22 @@ function _debugErrorMap(): ErrorMap<AuthErrorCode> {
359365
'different options. To avoid this error, call initializeAuth() with the ' +
360366
'same options as when it was originally called, or call getAuth() to return the' +
361367
' already initialized instance.',
362-
[AuthErrorCode.RECAPTCHA_CHECK_FAILED]:
363-
'The ReCAPTCHA assessment failed for this request.',
368+
[AuthErrorCode.MISSING_RECAPTCHA_TOKEN]:
369+
'The reCAPTCHA token is missing when sending request to the backend.',
370+
[AuthErrorCode.INVALID_RECAPTCHA_TOKEN]:
371+
'The reCAPTCHA token is invalid when sending request to the backend.',
372+
[AuthErrorCode.INVALID_RECAPTCHA_ACTION]:
373+
'The reCAPTCHA action is invalid when sending request to the backend.',
364374
[AuthErrorCode.RECAPTCHA_NOT_ENABLED]:
365-
'reCAPTCHA integration is not enabled for this project.'
375+
'reCAPTCHA integration is not enabled for this project.',
376+
[AuthErrorCode.MISSING_CLIENT_TYPE]:
377+
'The reCAPTCHA client type is missing when sending request to the backend.',
378+
[AuthErrorCode.MISSING_RECAPTCHA_VERSION]:
379+
'The reCAPTCHA version is missing when sending request to the backend.',
380+
[AuthErrorCode.INVALID_REQ_TYPE]:
381+
'The reCAPTCHA client type or version is invalid when retrieving the site key.',
382+
[AuthErrorCode.INVALID_RECAPTCHA_VERSION]:
383+
'The reCAPTCHA version is invalid when sending request to the backend.'
366384
};
367385
}
368386

@@ -565,6 +583,12 @@ export const AUTH_ERROR_CODES_MAP_DO_NOT_USE_INTERNALLY = {
565583
WEAK_PASSWORD: 'auth/weak-password',
566584
WEB_STORAGE_UNSUPPORTED: 'auth/web-storage-unsupported',
567585
ALREADY_INITIALIZED: 'auth/already-initialized',
568-
RECAPTCHA_CHECK_FAILED: 'auth/recaptcha-check-failed',
569-
RECAPTCHA_NOT_ENABLED: 'auth/recaptcha-not-enabled'
586+
RECAPTCHA_NOT_ENABLED: 'auth/recaptcha-not-enabled',
587+
MISSING_RECAPTCHA_TOKEN: 'auth/missing-recaptcha-token',
588+
INVALID_RECAPTCHA_TOKEN: 'auth/invalid-recaptcha-token',
589+
INVALID_RECAPTCHA_ACTION: 'auth/invalide-recaptcha-action',
590+
MISSING_CLIENT_TYPE: 'auth/missing-client-type',
591+
MISSING_RECAPTCHA_VERSION: 'auth/missing-recaptcha-version',
592+
INVALID_RECAPTCHA_VERSION: 'auth/invalid-recaptcha-version',
593+
INVALID_REQ_TYPE: 'auth/invalid-req-type'
570594
} as const;

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

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import { _setActionCodeSettingsOnRequest } from './action_code_settings';
3434
import { signInWithCredential } from './credential';
3535
import { _castAuth } from '../auth/auth_impl';
3636
import { AuthErrorCode } from '../errors';
37-
import { ServerError } from '../../api/errors';
3837
import { getModularInstance } from '@firebase/util';
3938
import { OperationType } from '../../model/enums';
4039
import { injectRecaptchaFields } from '../../platform_browser/recaptcha/recaptcha_enterprise_verifier';
@@ -109,32 +108,12 @@ export async function sendPasswordResetEmail(
109108
actionCodeSettings
110109
);
111110
}
112-
await authentication
113-
.sendPasswordResetEmail(authInternal, request)
114-
.catch(async error => {
115-
if (error.code === `auth/${ServerError.MISSING_RECAPTCHA_TOKEN}`) {
116-
console.log(
117-
'Password resets are protected by reCAPTCHA for this project. Automatically triggering the reCAPTCHA flow and restarting the password reset flow.'
118-
);
119-
const requestWithRecaptcha = await injectRecaptchaFields(
120-
authInternal,
121-
request,
122-
RecaptchaActionName.GET_OOB_CODE,
123-
true
124-
);
125-
if (actionCodeSettings) {
126-
_setActionCodeSettingsOnRequest(
127-
authInternal,
128-
requestWithRecaptcha,
129-
actionCodeSettings
130-
);
131-
}
132-
await authentication.sendPasswordResetEmail(
133-
authInternal,
134-
requestWithRecaptcha
135-
);
136-
} else {
137-
return Promise.reject(error);
111+
await authentication.sendPasswordResetEmail(authInternal, request).catch(async (error) => {
112+
if (error.code === `auth/${AuthErrorCode.MISSING_RECAPTCHA_TOKEN}`) {
113+
console.log("Password resets are protected by reCAPTCHA for this project. Automatically triggering the reCAPTCHA flow and restarting the password reset flow.");
114+
const requestWithRecaptcha = await injectRecaptchaFields(authInternal, request, RecaptchaActionName.GET_OOB_CODE, true);
115+
if (actionCodeSettings) {
116+
_setActionCodeSettingsOnRequest(authInternal, requestWithRecaptcha, actionCodeSettings);
138117
}
139118
});
140119
}
@@ -295,16 +274,10 @@ export async function createUserWithEmailAndPassword(
295274
);
296275
signUpResponse = signUp(authInternal, requestWithRecaptcha);
297276
} else {
298-
signUpResponse = signUp(authInternal, request).catch(async error => {
299-
if (error.code === `auth/${ServerError.MISSING_RECAPTCHA_TOKEN}`) {
300-
console.log(
301-
'Sign-up is protected by reCAPTCHA for this project. Automatically triggering the reCAPTCHA flow and restarting the sign-up flow.'
302-
);
303-
const requestWithRecaptcha = await injectRecaptchaFields(
304-
authInternal,
305-
request,
306-
RecaptchaActionName.SIGN_UP_PASSWORD
307-
);
277+
signUpResponse = signUp(authInternal, request).catch(async (error) => {
278+
if (error.code === `auth/${AuthErrorCode.MISSING_RECAPTCHA_TOKEN}`) {
279+
console.log("Sign-up is protected by reCAPTCHA for this project. Automatically triggering the reCAPTCHA flow and restarting the sign-up flow.");
280+
const requestWithRecaptcha = await injectRecaptchaFields(authInternal, request, RecaptchaActionName.SIGN_UP_PASSWORD);
308281
return signUp(authInternal, requestWithRecaptcha);
309282
} else {
310283
return Promise.reject(error);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import { _getCurrentUrl } from '../util/location';
2929
import { _setActionCodeSettingsOnRequest } from './action_code_settings';
3030
import { signInWithCredential } from './credential';
3131
import { AuthErrorCode } from '../errors';
32-
import { ServerError } from '../../api/errors';
3332
import { _assert } from '../util/assert';
3433
import { getModularInstance } from '@firebase/util';
3534
import { _castAuth } from '../auth/auth_impl';
@@ -115,7 +114,7 @@ export async function sendSignInLinkToEmail(
115114
await api
116115
.sendSignInLinkToEmail(authInternal, request)
117116
.catch(async error => {
118-
if (error.code === `auth/${ServerError.MISSING_RECAPTCHA_TOKEN}`) {
117+
if (error.code === `auth/${AuthErrorCode.MISSING_RECAPTCHA_TOKEN}`) {
119118
console.log(
120119
'Email link sign-in is protected by reCAPTCHA for this project. Automatically triggering the reCAPTCHA flow and restarting the sign-in flow.'
121120
);

0 commit comments

Comments
 (0)