Skip to content

Commit c6d7895

Browse files
authored
Merge e07adfb into 08ec55d
2 parents 08ec55d + e07adfb commit c6d7895

File tree

7 files changed

+85
-52
lines changed

7 files changed

+85
-52
lines changed

packages/auth/src/api/index.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,8 @@ describe('api/_performApiRequest', () => {
220220
Endpoint.SIGN_UP,
221221
request
222222
);
223-
let error: FirebaseError;
224-
try {
225-
await promise;
226-
} catch (e) {
227-
error = e;
228-
}
229-
expect(error!.customData!.message).to.eql('Text text text');
223+
await expect(promise)
224+
.to.be.rejectedWith(FirebaseError, 'Text text text');
230225
});
231226

232227
it('should handle unknown server errors', async () => {

packages/auth/src/api/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import { FirebaseError, querystring } from '@firebase/util';
1919

2020
import { AuthErrorCode, NamedErrorParams } from '../core/errors';
21-
import { _createError, _fail } from '../core/util/assert';
21+
import { _createError, _errorWithCustomMessage, _fail } from '../core/util/assert';
2222
import { Delay } from '../core/util/delay';
2323
import { _emulatorUrl } from '../core/util/emulator';
2424
import { FetchProvider } from '../core/util/fetch_provider';
@@ -168,7 +168,7 @@ export async function _performFetchWithErrorHandling<V>(
168168
.toLowerCase()
169169
.replace(/[_\s]+/g, '-') as unknown) as AuthErrorCode);
170170
if (serverErrorMessage) {
171-
_fail(auth, authError, {message: serverErrorMessage});
171+
throw _errorWithCustomMessage(auth, authError, serverErrorMessage);
172172
} else {
173173
_fail(auth, authError);
174174
}

packages/auth/src/core/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ export interface ErrorMapRetriever extends AuthErrorMap {
359359
(): ErrorMap<AuthErrorCode>;
360360
}
361361

362-
function _prodErrorMap(): ErrorMap<AuthErrorCode> {
362+
export function _prodErrorMap(): ErrorMap<AuthErrorCode> {
363363
// We will include this one message in the prod error map since by the very
364364
// nature of this error, developers will never be able to see the message
365365
// using the debugErrorMap (which is installed during auth initialization).

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import { expect } from 'chai';
1919

2020
import { FirebaseError } from '@firebase/util';
2121

22-
import { assertTypes, opt } from './assert';
22+
import { assertTypes, opt, _assertInstanceOf } from './assert';
23+
import { Auth } from '../../model/public_types';
24+
import { testAuth } from '../../../test/helpers/mock_auth';
2325

2426
class Parent {}
2527
class Child extends Parent {}
@@ -147,3 +149,43 @@ describe('assertTypes', () => {
147149
});
148150
});
149151
});
152+
153+
describe('_assertInstanceOf', () => {
154+
interface Constructable {
155+
new (): object;
156+
}
157+
158+
let auth: Auth;
159+
beforeEach(async () => {
160+
auth = await testAuth();
161+
});
162+
163+
function makeClass(): Constructable {
164+
class Test {};
165+
return Test;
166+
};
167+
168+
class WrongClass {}
169+
170+
it('fails with the wrong class', () => {
171+
expect(() => {
172+
const foo = new WrongClass();
173+
_assertInstanceOf(auth, foo, makeClass());
174+
}).to.throw(FirebaseError, 'auth/argument-error');
175+
});
176+
177+
it('fails with the right class wrong instance with custom message', () => {
178+
expect(() => {
179+
const a = makeClass();
180+
const b = makeClass();
181+
_assertInstanceOf(auth, new a(), b);
182+
}).to.throw(FirebaseError, 'Type of Test does not match');
183+
});
184+
185+
it('passes if all is well', () => {
186+
expect(() => {
187+
const a = makeClass();
188+
_assertInstanceOf(auth, new a(), a);
189+
}).not.to.throw();
190+
});
191+
});

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
*/
1717

1818
import { Auth } from '../../model/public_types';
19-
import { FirebaseError } from '@firebase/util';
19+
import { ErrorFactory, FirebaseError } from '@firebase/util';
2020
import { AuthInternal } from '../../model/auth';
2121
import {
2222
_DEFAULT_AUTH_ERROR_FACTORY,
2323
AuthErrorCode,
24-
AuthErrorParams
24+
AuthErrorParams,
25+
_prodErrorMap
2526
} from '../errors';
2627
import { _logError } from './log';
2728

@@ -81,6 +82,31 @@ export function _createError<K extends AuthErrorCode>(
8182
return createErrorInternal(authOrCode, ...rest);
8283
}
8384

85+
export function _errorWithCustomMessage(auth: Auth, code: AuthErrorCode, message: string): FirebaseError {
86+
const errorMap = {..._prodErrorMap(), [code]: message};
87+
const factory = new ErrorFactory<AuthErrorCode, AuthErrorParams>(
88+
'auth',
89+
'Firebase',
90+
errorMap
91+
);
92+
return factory.create(code, {
93+
appName: auth.name,
94+
});
95+
}
96+
97+
export function _assertInstanceOf(auth: Auth, object: object, instance: unknown): void {
98+
const constructorInstance = (instance as { new (...args: unknown[]): unknown });
99+
if (!(object instanceof constructorInstance)) {
100+
if (constructorInstance.name !== object.constructor.name) {
101+
_fail(auth, AuthErrorCode.ARGUMENT_ERROR);
102+
}
103+
104+
throw _errorWithCustomMessage(auth, AuthErrorCode.ARGUMENT_ERROR,
105+
`Type of ${object.constructor.name} does not match expected instance.` +
106+
`Did you pass a reference from a different Auth SDK?`);
107+
}
108+
}
109+
84110
function createErrorInternal<K extends AuthErrorCode>(
85111
authOrCode: Auth | K,
86112
...rest: unknown[]
@@ -244,3 +270,4 @@ export function debugAssert(
244270
debugFail(message);
245271
}
246272
}
273+

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

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

2626
import { _castAuth } from '../../core/auth/auth_impl';
2727
import { AuthErrorCode } from '../../core/errors';
28-
import { _assert, debugAssert, _createError } from '../../core/util/assert';
28+
import { _assert, debugAssert, _createError, _assertInstanceOf } from '../../core/util/assert';
2929
import { Delay } from '../../core/util/delay';
3030
import { _generateEventId } from '../../core/util/event_id';
3131
import { AuthInternal } from '../../model/auth';
@@ -83,12 +83,7 @@ export async function signInWithPopup(
8383
resolver?: PopupRedirectResolver
8484
): Promise<UserCredential> {
8585
const authInternal = _castAuth(auth);
86-
_assert(
87-
provider instanceof FederatedAuthProvider,
88-
auth,
89-
AuthErrorCode.ARGUMENT_ERROR
90-
);
91-
86+
_assertInstanceOf(auth, provider, FederatedAuthProvider);
9287
const resolverInternal = _withDefaultResolver(authInternal, resolver);
9388
const action = new PopupOperation(
9489
authInternal,
@@ -130,12 +125,7 @@ export async function reauthenticateWithPopup(
130125
resolver?: PopupRedirectResolver
131126
): Promise<UserCredential> {
132127
const userInternal = getModularInstance(user) as UserInternal;
133-
_assert(
134-
provider instanceof FederatedAuthProvider,
135-
userInternal.auth,
136-
AuthErrorCode.ARGUMENT_ERROR
137-
);
138-
128+
_assertInstanceOf(userInternal.auth, provider, FederatedAuthProvider);
139129
const resolverInternal = _withDefaultResolver(userInternal.auth, resolver);
140130
const action = new PopupOperation(
141131
userInternal.auth,
@@ -177,12 +167,7 @@ export async function linkWithPopup(
177167
resolver?: PopupRedirectResolver
178168
): Promise<UserCredential> {
179169
const userInternal = getModularInstance(user) as UserInternal;
180-
_assert(
181-
provider instanceof FederatedAuthProvider,
182-
userInternal.auth,
183-
AuthErrorCode.ARGUMENT_ERROR
184-
);
185-
170+
_assertInstanceOf(userInternal.auth, provider, FederatedAuthProvider);
186171
const resolverInternal = _withDefaultResolver(userInternal.auth, resolver);
187172

188173
const action = new PopupOperation(

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

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ import {
2424
} from '../../model/public_types';
2525

2626
import { _castAuth } from '../../core/auth/auth_impl';
27-
import { AuthErrorCode } from '../../core/errors';
2827
import { _assertLinkedStatus } from '../../core/user/link_unlink';
29-
import { _assert } from '../../core/util/assert';
28+
import { _assertInstanceOf } from '../../core/util/assert';
3029
import { _generateEventId } from '../../core/util/event_id';
3130
import { AuthEventType } from '../../model/popup_redirect';
3231
import { UserInternal } from '../../model/user';
@@ -91,12 +90,7 @@ export async function _signInWithRedirect(
9190
resolver?: PopupRedirectResolver
9291
): Promise<void | never> {
9392
const authInternal = _castAuth(auth);
94-
_assert(
95-
provider instanceof FederatedAuthProvider,
96-
auth,
97-
AuthErrorCode.ARGUMENT_ERROR
98-
);
99-
93+
_assertInstanceOf(auth, provider, FederatedAuthProvider);
10094
const resolverInternal = _withDefaultResolver(authInternal, resolver);
10195
await _setPendingRedirectStatus(resolverInternal, authInternal);
10296

@@ -152,12 +146,7 @@ export async function _reauthenticateWithRedirect(
152146
resolver?: PopupRedirectResolver
153147
): Promise<void | never> {
154148
const userInternal = getModularInstance(user) as UserInternal;
155-
_assert(
156-
provider instanceof FederatedAuthProvider,
157-
userInternal.auth,
158-
AuthErrorCode.ARGUMENT_ERROR
159-
);
160-
149+
_assertInstanceOf(userInternal.auth, provider, FederatedAuthProvider);
161150
// Allow the resolver to error before persisting the redirect user
162151
const resolverInternal = _withDefaultResolver(userInternal.auth, resolver);
163152
await _setPendingRedirectStatus(resolverInternal, userInternal.auth);
@@ -209,12 +198,7 @@ export async function _linkWithRedirect(
209198
resolver?: PopupRedirectResolver
210199
): Promise<void | never> {
211200
const userInternal = getModularInstance(user) as UserInternal;
212-
_assert(
213-
provider instanceof FederatedAuthProvider,
214-
userInternal.auth,
215-
AuthErrorCode.ARGUMENT_ERROR
216-
);
217-
201+
_assertInstanceOf(userInternal.auth, provider, FederatedAuthProvider);
218202
// Allow the resolver to error before persisting the redirect user
219203
const resolverInternal = _withDefaultResolver(userInternal.auth, resolver);
220204
await _assertLinkedStatus(false, userInternal, provider.providerId);

0 commit comments

Comments
 (0)