Skip to content

Commit bf5d04f

Browse files
author
chuanr
committed
Align recaptcha error codes with android
1 parent d72b97c commit bf5d04f

File tree

6 files changed

+40
-13
lines changed

6 files changed

+40
-13
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: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {
2626
import { AuthInternal } from '../../model/auth';
2727
import { IdTokenResponse } from '../../model/id_token';
2828
import { AuthErrorCode } from '../errors';
29-
import { ServerError } from '../../api/errors';
3029
import { _fail } from '../util/assert';
3130
import { AuthCredential } from './auth_credential';
3231
import { injectRecaptchaFields } from '../../platform_browser/recaptcha/recaptcha_enterprise_verifier';
@@ -125,7 +124,7 @@ export class EmailAuthCredential extends AuthCredential {
125124
return signInWithPassword(auth, requestWithRecaptcha);
126125
} else {
127126
return signInWithPassword(auth, request).catch(async (error) => {
128-
if (error.code === `auth/${ServerError.MISSING_RECAPTCHA_TOKEN}`) {
127+
if (error.code === `auth/${AuthErrorCode.MISSING_RECAPTCHA_TOKEN}`) {
129128
console.log("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.");
130129
const requestWithRecaptcha = await injectRecaptchaFields(auth, request, RecaptchaActionName.SIGN_IN_WITH_PASSWORD);
131130
return signInWithPassword(auth, requestWithRecaptcha);

packages/auth/src/core/errors.ts

Lines changed: 28 additions & 4 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',
128127
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]:
365375
'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',
569586
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: 2 additions & 3 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';
@@ -94,7 +93,7 @@ export async function sendPasswordResetEmail(
9493
_setActionCodeSettingsOnRequest(authInternal, request, actionCodeSettings);
9594
}
9695
await authentication.sendPasswordResetEmail(authInternal, request).catch(async (error) => {
97-
if (error.code === `auth/${ServerError.MISSING_RECAPTCHA_TOKEN}`) {
96+
if (error.code === `auth/${AuthErrorCode.MISSING_RECAPTCHA_TOKEN}`) {
9897
console.log("Password resets are protected by reCAPTCHA for this project. Automatically triggering the reCAPTCHA flow and restarting the password reset flow.");
9998
const requestWithRecaptcha = await injectRecaptchaFields(authInternal, request, RecaptchaActionName.GET_OOB_CODE, true);
10099
if (actionCodeSettings) {
@@ -260,7 +259,7 @@ export async function createUserWithEmailAndPassword(
260259
signUpResponse = signUp(authInternal, requestWithRecaptcha);
261260
} else {
262261
signUpResponse = signUp(authInternal, request).catch(async (error) => {
263-
if (error.code === `auth/${ServerError.MISSING_RECAPTCHA_TOKEN}`) {
262+
if (error.code === `auth/${AuthErrorCode.MISSING_RECAPTCHA_TOKEN}`) {
264263
console.log("Sign-up is protected by reCAPTCHA for this project. Automatically triggering the reCAPTCHA flow and restarting the sign-up flow.");
265264
const requestWithRecaptcha = await injectRecaptchaFields(authInternal, request, RecaptchaActionName.SIGN_UP_PASSWORD);
266265
return signUp(authInternal, requestWithRecaptcha);

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';
@@ -101,7 +100,7 @@ export async function sendSignInLinkToEmail(
101100
} else {
102101
setActionCodeSettings(request, actionCodeSettings);
103102
await api.sendSignInLinkToEmail(authInternal, request).catch(async (error) => {
104-
if (error.code === `auth/${ServerError.MISSING_RECAPTCHA_TOKEN}`) {
103+
if (error.code === `auth/${AuthErrorCode.MISSING_RECAPTCHA_TOKEN}`) {
105104
console.log("Email link sign-in is protected by reCAPTCHA for this project. Automatically triggering the reCAPTCHA flow and restarting the sign-in flow.");
106105
const requestWithRecaptcha = await injectRecaptchaFields(authInternal, request, RecaptchaActionName.GET_OOB_CODE, true);
107106
setActionCodeSettings(requestWithRecaptcha, actionCodeSettings);

0 commit comments

Comments
 (0)