Skip to content

Commit f9ca3b7

Browse files
committed
Descriptive errors when changing the current user.
1 parent 725f843 commit f9ca3b7

File tree

12 files changed

+54
-31
lines changed

12 files changed

+54
-31
lines changed

packages/auth/src/core/auth/auth_impl.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ import {
6262
PersistenceUserManager
6363
} from '../persistence/persistence_user_manager';
6464
import { _reloadWithoutSaving } from '../user/reload';
65-
import { _assert, _createError } from '../util/assert';
65+
import {
66+
_assert,
67+
_serverAppCurrentUserOperationNotSupportedError
68+
} from '../util/assert';
6669
import { _getInstance } from '../util/instantiator';
6770
import { _getUserLanguage } from '../util/navigator';
6871
import { _getClientVersion } from '../util/version';
@@ -356,7 +359,7 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
356359
async updateCurrentUser(userExtern: User | null): Promise<void> {
357360
if (_isFirebaseServerApp(this.app)) {
358361
return Promise.reject(
359-
_createError(this, AuthErrorCode.OPERATION_NOT_SUPPORTED)
362+
_serverAppCurrentUserOperationNotSupportedError(this)
360363
);
361364
}
362365
// The public updateCurrentUser method needs to make a copy of the user,
@@ -402,7 +405,7 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
402405
async signOut(): Promise<void> {
403406
if (_isFirebaseServerApp(this.app)) {
404407
return Promise.reject(
405-
_createError(this, AuthErrorCode.OPERATION_NOT_SUPPORTED)
408+
_serverAppCurrentUserOperationNotSupportedError(this)
406409
);
407410
}
408411
// Run first, to block _setRedirectUser() if any callbacks fail.
@@ -420,7 +423,7 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
420423
setPersistence(persistence: Persistence): Promise<void> {
421424
if (_isFirebaseServerApp(this.app)) {
422425
return Promise.reject(
423-
_createError(this, AuthErrorCode.OPERATION_NOT_SUPPORTED)
426+
_serverAppCurrentUserOperationNotSupportedError(this)
424427
);
425428
}
426429
return this.queue(async () => {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { UserCredentialImpl } from '../user/user_credential_impl';
2222
import { _castAuth } from '../auth/auth_impl';
2323
import { OperationType } from '../../model/enums';
2424
import { _isFirebaseServerApp } from '@firebase/app';
25-
import { _createError } from '../../core/util/assert';
25+
import { _serverAppCurrentUserOperationNotSupportedError } from '../../core/util/assert';
2626
import { AuthErrorCode } from '../../core/errors';
2727

2828
/**
@@ -42,7 +42,7 @@ import { AuthErrorCode } from '../../core/errors';
4242
export async function signInAnonymously(auth: Auth): Promise<UserCredential> {
4343
if (_isFirebaseServerApp(auth.app)) {
4444
return Promise.reject(
45-
_createError(auth, AuthErrorCode.OPERATION_NOT_SUPPORTED)
45+
_serverAppCurrentUserOperationNotSupportedError(auth)
4646
);
4747
}
4848
const authInternal = _castAuth(auth);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { _castAuth } from '../auth/auth_impl';
2828
import { getModularInstance } from '@firebase/util';
2929
import { OperationType } from '../../model/enums';
3030
import { _isFirebaseServerApp } from '@firebase/app';
31-
import { _createError } from '../../core/util/assert';
31+
import { _serverAppCurrentUserOperationNotSupportedError } from '../../core/util/assert';
3232
import { AuthErrorCode } from '../../core/errors';
3333

3434
export async function _signInWithCredential(
@@ -38,7 +38,7 @@ export async function _signInWithCredential(
3838
): Promise<UserCredential> {
3939
if (_isFirebaseServerApp(auth.app)) {
4040
return Promise.reject(
41-
_createError(auth, AuthErrorCode.OPERATION_NOT_SUPPORTED)
41+
_serverAppCurrentUserOperationNotSupportedError(auth)
4242
);
4343
}
4444
const operationType = OperationType.SIGN_IN;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ import { UserCredentialImpl } from '../user/user_credential_impl';
2323
import { _castAuth } from '../auth/auth_impl';
2424
import { OperationType } from '../../model/enums';
2525
import { _isFirebaseServerApp } from '@firebase/app';
26-
import { _createError } from '../../core/util/assert';
27-
import { AuthErrorCode } from '../../core/errors';
26+
import { _serverAppCurrentUserOperationNotSupportedError } from '../../core/util/assert';
2827
/**
2928
* Asynchronously signs in using a custom token.
3029
*
@@ -50,7 +49,7 @@ export async function signInWithCustomToken(
5049
): Promise<UserCredential> {
5150
if (_isFirebaseServerApp(auth.app)) {
5251
return Promise.reject(
53-
_createError(auth, AuthErrorCode.OPERATION_NOT_SUPPORTED)
52+
_serverAppCurrentUserOperationNotSupportedError(auth)
5453
);
5554
}
5655
const authInternal = _castAuth(auth);

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ import { signUp, SignUpRequest } from '../../api/authentication/sign_up';
2929
import { MultiFactorInfoImpl } from '../../mfa/mfa_info';
3030
import { EmailAuthProvider } from '../providers/email';
3131
import { UserCredentialImpl } from '../user/user_credential_impl';
32-
import { _assert, _createError } from '../util/assert';
32+
import {
33+
_assert,
34+
_serverAppCurrentUserOperationNotSupportedError
35+
} from '../util/assert';
3336
import { _setActionCodeSettingsOnRequest } from './action_code_settings';
3437
import { signInWithCredential } from './credential';
3538
import { _castAuth } from '../auth/auth_impl';
@@ -273,7 +276,7 @@ export async function createUserWithEmailAndPassword(
273276
): Promise<UserCredential> {
274277
if (_isFirebaseServerApp(auth.app)) {
275278
return Promise.reject(
276-
_createError(auth, AuthErrorCode.OPERATION_NOT_SUPPORTED)
279+
_serverAppCurrentUserOperationNotSupportedError(auth)
277280
);
278281
}
279282
const authInternal = _castAuth(auth);
@@ -338,7 +341,7 @@ export function signInWithEmailAndPassword(
338341
): Promise<UserCredential> {
339342
if (_isFirebaseServerApp(auth.app)) {
340343
return Promise.reject(
341-
_createError(auth, AuthErrorCode.OPERATION_NOT_SUPPORTED)
344+
_serverAppCurrentUserOperationNotSupportedError(auth)
342345
);
343346
}
344347
return signInWithCredential(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { _castAuth } from '../auth/auth_impl';
3535
import { handleRecaptchaFlow } from '../../platform_browser/recaptcha/recaptcha_enterprise_verifier';
3636
import { RecaptchaActionName, RecaptchaClientType } from '../../api';
3737
import { _isFirebaseServerApp } from '@firebase/app';
38-
import { _createError } from '../../core/util/assert';
38+
import { _serverAppCurrentUserOperationNotSupportedError } from '../../core/util/assert';
3939

4040
/**
4141
* Sends a sign-in email link to the user with the specified email.
@@ -173,7 +173,7 @@ export async function signInWithEmailLink(
173173
): Promise<UserCredential> {
174174
if (_isFirebaseServerApp(auth.app)) {
175175
return Promise.reject(
176-
_createError(auth, AuthErrorCode.OPERATION_NOT_SUPPORTED)
176+
_serverAppCurrentUserOperationNotSupportedError(auth)
177177
);
178178
}
179179
const authModular = getModularInstance(auth);

packages/auth/src/core/user/account_info.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ import { _logoutIfInvalidated } from './invalidation';
2727
import { getModularInstance } from '@firebase/util';
2828
import { ProviderId } from '../../model/enums';
2929
import { _isFirebaseServerApp } from '@firebase/app';
30-
import { _createError } from '../../core/util/assert';
31-
import { AuthErrorCode } from '../../core/errors';
30+
import { _serverAppCurrentUserOperationNotSupportedError } from '../../core/util/assert';
3231

3332
/**
3433
* Updates a user's profile data.
@@ -103,7 +102,7 @@ export function updateEmail(user: User, newEmail: string): Promise<void> {
103102
const userInternal = getModularInstance(user) as UserInternal;
104103
if (_isFirebaseServerApp(userInternal.auth.app)) {
105104
return Promise.reject(
106-
_createError(userInternal.auth, AuthErrorCode.OPERATION_NOT_SUPPORTED)
105+
_serverAppCurrentUserOperationNotSupportedError(userInternal.auth)
107106
);
108107
}
109108
return updateEmailOrPassword(userInternal, newEmail, null);

packages/auth/src/core/user/reauthenticate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { _parseToken } from './id_token_result';
2626
import { _logoutIfInvalidated } from './invalidation';
2727
import { UserCredentialImpl } from './user_credential_impl';
2828
import { _isFirebaseServerApp } from '@firebase/app';
29-
import { _createError } from '../../core/util/assert';
29+
import { _serverAppCurrentUserOperationNotSupportedError } from '../../core/util/assert';
3030

3131
export async function _reauthenticate(
3232
user: UserInternal,
@@ -36,7 +36,7 @@ export async function _reauthenticate(
3636
const { auth } = user;
3737
if (_isFirebaseServerApp(auth.app)) {
3838
return Promise.reject(
39-
_createError(auth, AuthErrorCode.OPERATION_NOT_SUPPORTED)
39+
_serverAppCurrentUserOperationNotSupportedError(auth)
4040
);
4141
}
4242
const operationType = OperationType.REAUTHENTICATE;

packages/auth/src/core/user/user_impl.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ import {
3232
} from '../../model/user';
3333
import { AuthErrorCode } from '../errors';
3434
import { PersistedBlob } from '../persistence';
35-
import { _assert, _createError } from '../util/assert';
35+
import {
36+
_assert,
37+
_serverAppCurrentUserOperationNotSupportedError
38+
} from '../util/assert';
3639
import { getIdTokenResult } from './id_token_result';
3740
import { _logoutIfInvalidated } from './invalidation';
3841
import { ProactiveRefresh } from './proactive_refresh';
@@ -203,7 +206,7 @@ export class UserImpl implements UserInternal {
203206
async delete(): Promise<void> {
204207
if (_isFirebaseServerApp(this.auth.app)) {
205208
return Promise.reject(
206-
_createError(this.auth, AuthErrorCode.OPERATION_NOT_SUPPORTED)
209+
_serverAppCurrentUserOperationNotSupportedError(this.auth)
207210
);
208211
}
209212
const idToken = await this.getIdToken();

packages/auth/src/core/util/assert.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ export function _errorWithCustomMessage(
102102
});
103103
}
104104

105+
export function _serverAppCurrentUserOperationNotSupportedError(
106+
auth: Auth
107+
): FirebaseError {
108+
return _errorWithCustomMessage(
109+
auth,
110+
AuthErrorCode.OPERATION_NOT_SUPPORTED,
111+
'Operations that alter the current user are not supported in conjunction with FirebaseServerApp'
112+
);
113+
}
114+
105115
export function _assertInstanceOf(
106116
auth: Auth,
107117
object: object,

packages/auth/src/platform_browser/strategies/phone.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ import { ApplicationVerifierInternal } from '../../model/application_verifier';
3131
import { PhoneAuthCredential } from '../../core/credentials/phone';
3232
import { AuthErrorCode } from '../../core/errors';
3333
import { _assertLinkedStatus, _link } from '../../core/user/link_unlink';
34-
import { _assert, _createError } from '../../core/util/assert';
34+
import {
35+
_assert,
36+
_serverAppCurrentUserOperationNotSupportedError
37+
} from '../../core/util/assert';
3538
import { AuthInternal } from '../../model/auth';
3639
import {
3740
linkWithCredential,
@@ -108,7 +111,7 @@ export async function signInWithPhoneNumber(
108111
): Promise<ConfirmationResult> {
109112
if (_isFirebaseServerApp(auth.app)) {
110113
return Promise.reject(
111-
_createError(auth, AuthErrorCode.OPERATION_NOT_SUPPORTED)
114+
_serverAppCurrentUserOperationNotSupportedError(auth)
112115
);
113116
}
114117
const authInternal = _castAuth(auth);
@@ -174,7 +177,7 @@ export async function reauthenticateWithPhoneNumber(
174177
const userInternal = getModularInstance(user) as UserInternal;
175178
if (_isFirebaseServerApp(userInternal.auth.app)) {
176179
return Promise.reject(
177-
_createError(userInternal.auth, AuthErrorCode.OPERATION_NOT_SUPPORTED)
180+
_serverAppCurrentUserOperationNotSupportedError(userInternal.auth)
178181
);
179182
}
180183
const verificationId = await _verifyPhoneNumber(
@@ -298,7 +301,7 @@ export async function updatePhoneNumber(
298301
const userInternal = getModularInstance(user) as UserInternal;
299302
if (_isFirebaseServerApp(userInternal.auth.app)) {
300303
return Promise.reject(
301-
_createError(userInternal.auth, AuthErrorCode.OPERATION_NOT_SUPPORTED)
304+
_serverAppCurrentUserOperationNotSupportedError(userInternal.auth)
302305
);
303306
}
304307
await _link(userInternal, credential);

packages/auth/src/platform_browser/strategies/redirect.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ import {
2525

2626
import { _castAuth } from '../../core/auth/auth_impl';
2727
import { _assertLinkedStatus } from '../../core/user/link_unlink';
28-
import { _assertInstanceOf, _createError } from '../../core/util/assert';
28+
import {
29+
_assertInstanceOf,
30+
_serverAppCurrentUserOperationNotSupportedError
31+
} from '../../core/util/assert';
2932
import { _generateEventId } from '../../core/util/event_id';
3033
import { AuthEventType } from '../../model/popup_redirect';
3134
import { UserInternal } from '../../model/user';
@@ -98,7 +101,7 @@ export async function _signInWithRedirect(
98101
): Promise<void | never> {
99102
if (_isFirebaseServerApp(auth.app)) {
100103
return Promise.reject(
101-
_createError(auth, AuthErrorCode.OPERATION_NOT_SUPPORTED)
104+
_serverAppCurrentUserOperationNotSupportedError(auth)
102105
);
103106
}
104107
const authInternal = _castAuth(auth);
@@ -172,7 +175,7 @@ export async function _reauthenticateWithRedirect(
172175
_assertInstanceOf(userInternal.auth, provider, FederatedAuthProvider);
173176
if (_isFirebaseServerApp(userInternal.auth.app)) {
174177
return Promise.reject(
175-
_createError(userInternal.auth, AuthErrorCode.OPERATION_NOT_SUPPORTED)
178+
_serverAppCurrentUserOperationNotSupportedError(userInternal.auth)
176179
);
177180
}
178181
// Wait for auth initialization to complete, this will process pending redirects and clear the
@@ -311,7 +314,7 @@ export async function _getRedirectResult(
311314
): Promise<UserCredential | null> {
312315
if (_isFirebaseServerApp(auth.app)) {
313316
return Promise.reject(
314-
_createError(auth, AuthErrorCode.OPERATION_NOT_SUPPORTED)
317+
_serverAppCurrentUserOperationNotSupportedError(auth)
315318
);
316319
}
317320
const authInternal = _castAuth(auth);

0 commit comments

Comments
 (0)