Skip to content

Commit 46fbfb5

Browse files
committed
Clean up our to/from JSON implementations
- Remove anonymous credential/provider since they're not exposed and don't do anything - rename toPlainObject on User to toJSON per API spec - relax restrictions around ProviderId & SignInMethod typing since they can sometimes have values outside of our enum
1 parent 3ff1898 commit 46fbfb5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+218
-398
lines changed

packages-exp/auth-exp/src/api/account_management/account.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ export async function deleteAccount(
3636
}
3737

3838
export interface ProviderUserInfo {
39+
providerId: string;
3940
rawId?: string;
40-
providerId?: string;
4141
email?: string;
4242
displayName?: string;
4343
photoUrl?: string;

packages-exp/auth-exp/src/core/auth/auth_impl.test.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717

1818
import { FirebaseApp } from '@firebase/app-types-exp';
19-
import * as externs from '@firebase/auth-types-exp';
2019
import { FirebaseError } from '@firebase/util';
2120
import { expect, use } from 'chai';
2221
import * as chaiAsPromised from 'chai-as-promised';
@@ -30,6 +29,7 @@ import { _getInstance } from '../util/instantiator';
3029
import * as navigator from '../util/navigator';
3130
import { ClientPlatform } from '../util/version';
3231
import { _castAuth, _initializeAuthForClientPlatform } from './auth_impl';
32+
import { Auth } from '../../model/auth';
3333

3434
use(sinonChai);
3535
use(chaiAsPromised);
@@ -46,14 +46,16 @@ const FAKE_APP: FirebaseApp = {
4646
const initializeAuth = _initializeAuthForClientPlatform(ClientPlatform.BROWSER);
4747

4848
describe('core/auth/auth_impl', () => {
49-
let auth: externs.Auth;
49+
let auth: Auth;
5050
let persistenceStub: sinon.SinonStubbedInstance<Persistence>;
5151

5252
beforeEach(() => {
5353
persistenceStub = sinon.stub(_getInstance(inMemoryPersistence));
54-
auth = initializeAuth(FAKE_APP, {
55-
persistence: inMemoryPersistence
56-
});
54+
auth = _castAuth(
55+
initializeAuth(FAKE_APP, {
56+
persistence: inMemoryPersistence
57+
})
58+
);
5759
});
5860

5961
afterEach(sinon.restore);
@@ -81,7 +83,7 @@ describe('core/auth/auth_impl', () => {
8183
for (let i = 0; i < 10; i++) {
8284
expect(persistenceStub.set.getCall(i)).to.have.been.calledWith(
8385
sinon.match.any,
84-
users[i].toPlainObject()
86+
users[i].toJSON()
8587
);
8688
}
8789
});
@@ -126,28 +128,28 @@ describe('core/auth/auth_impl', () => {
126128

127129
it('immediately calls authStateChange if initialization finished', done => {
128130
const user = testUser(auth, 'uid');
129-
_castAuth(auth).currentUser = user;
130-
_castAuth(auth)._isInitialized = true;
131-
auth.onAuthStateChanged(user => {
131+
auth.currentUser = user;
132+
auth._isInitialized = true;
133+
auth._onAuthStateChanged(user => {
132134
expect(user).to.eq(user);
133135
done();
134136
});
135137
});
136138

137139
it('immediately calls idTokenChange if initialization finished', done => {
138140
const user = testUser(auth, 'uid');
139-
_castAuth(auth).currentUser = user;
140-
_castAuth(auth)._isInitialized = true;
141-
auth.onIdTokenChanged(user => {
141+
auth.currentUser = user;
142+
auth._isInitialized = true;
143+
auth._onIdTokenChanged(user => {
142144
expect(user).to.eq(user);
143145
done();
144146
});
145147
});
146148

147149
it('immediate callback is done async', () => {
148-
_castAuth(auth)._isInitialized = true;
150+
auth._isInitialized = true;
149151
let callbackCalled = false;
150-
auth.onIdTokenChanged(() => {
152+
auth._onIdTokenChanged(() => {
151153
callbackCalled = true;
152154
});
153155

@@ -167,8 +169,8 @@ describe('core/auth/auth_impl', () => {
167169

168170
context('initially currentUser is null', () => {
169171
beforeEach(async () => {
170-
auth.onAuthStateChanged(authStateCallback);
171-
auth.onIdTokenChanged(idTokenCallback);
172+
auth._onAuthStateChanged(authStateCallback);
173+
auth._onIdTokenChanged(idTokenCallback);
172174
await auth.updateCurrentUser(null);
173175
authStateCallback.resetHistory();
174176
idTokenCallback.resetHistory();
@@ -187,8 +189,8 @@ describe('core/auth/auth_impl', () => {
187189

188190
context('initially currentUser is user', () => {
189191
beforeEach(async () => {
190-
auth.onAuthStateChanged(authStateCallback);
191-
auth.onIdTokenChanged(idTokenCallback);
192+
auth._onAuthStateChanged(authStateCallback);
193+
auth._onIdTokenChanged(idTokenCallback);
192194
await auth.updateCurrentUser(user);
193195
authStateCallback.resetHistory();
194196
idTokenCallback.resetHistory();
@@ -226,8 +228,8 @@ describe('core/auth/auth_impl', () => {
226228
it('onAuthStateChange works for multiple listeners', async () => {
227229
const cb1 = sinon.spy();
228230
const cb2 = sinon.spy();
229-
auth.onAuthStateChanged(cb1);
230-
auth.onAuthStateChanged(cb2);
231+
auth._onAuthStateChanged(cb1);
232+
auth._onAuthStateChanged(cb2);
231233
await auth.updateCurrentUser(null);
232234
cb1.resetHistory();
233235
cb2.resetHistory();
@@ -240,8 +242,8 @@ describe('core/auth/auth_impl', () => {
240242
it('onIdTokenChange works for multiple listeners', async () => {
241243
const cb1 = sinon.spy();
242244
const cb2 = sinon.spy();
243-
auth.onIdTokenChanged(cb1);
244-
auth.onIdTokenChanged(cb2);
245+
auth._onIdTokenChanged(cb1);
246+
auth._onIdTokenChanged(cb2);
245247
await auth.updateCurrentUser(null);
246248
cb1.resetHistory();
247249
cb2.resetHistory();

packages-exp/auth-exp/src/core/credentials/anonymous.test.ts

Lines changed: 0 additions & 95 deletions
This file was deleted.

packages-exp/auth-exp/src/core/credentials/anonymous.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.

packages-exp/auth-exp/src/core/credentials/index.d.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,26 @@ import * as externs from '@firebase/auth-types-exp';
2020
import { PhoneOrOauthTokenResponse } from '../../api/authentication/mfa';
2121
import { AuthCore } from '../../model/auth';
2222
import { IdTokenResponse } from '../../model/id_token';
23+
import { EmailAuthCredential } from './email';
24+
import { PhoneAuthCredential } from './phone';
25+
import { OAuthCredential } from './oauth';
2326

2427
export abstract class AuthCredential extends externs.AuthCredential {
25-
static fromJSON(json: object | string): AuthCredential | null;
28+
static fromJSON(json: object | string): AuthCredential | null {
29+
// const obj = (typeof json === 'string') ? JSON.parse(json) : json;
30+
for (const initializer of [
31+
/* SAMLAuthCredential.fromJSON, */
32+
OAuthCredential.fromJSON,
33+
EmailAuthCredential.fromJSON,
34+
PhoneAuthCredential.fromJSON
35+
]) {
36+
const credential = initializer(json);
37+
if (credential) {
38+
return credential;
39+
}
40+
}
41+
return null;
42+
}
2643

2744
_getIdTokenResponse(auth: AuthCore): Promise<PhoneOrOauthTokenResponse>;
2845
_linkToIdToken(auth: AuthCore, idToken: string): Promise<IdTokenResponse>;

packages-exp/auth-exp/src/core/credentials/oauth.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ export interface OAuthCredentialParams {
4545
pendingToken?: string;
4646

4747
// Utilities
48-
providerId: externs.ProviderId;
49-
signInMethod: externs.SignInMethod;
48+
providerId: string;
49+
signInMethod: string;
5050
}
5151

5252
export class OAuthCredential
@@ -58,8 +58,8 @@ export class OAuthCredential
5858
private pendingToken: string | null = null;
5959

6060
private constructor(
61-
readonly providerId: externs.ProviderId,
62-
readonly signInMethod: externs.SignInMethod
61+
readonly providerId: string,
62+
readonly signInMethod: string
6363
) {}
6464

6565
static _fromParams(params: OAuthCredentialParams): OAuthCredential {
@@ -106,7 +106,8 @@ export class OAuthCredential
106106
};
107107
}
108108

109-
static fromJSON(obj: object): externs.OAuthCredential | null {
109+
static fromJSON(json: string | object): externs.OAuthCredential | null {
110+
const obj = typeof json === 'string' ? JSON.parse(json) : json;
110111
const { providerId, signInMethod, ...rest }: Partial<OAuthCredential> = obj;
111112
if (!providerId || !signInMethod) {
112113
return null;

packages-exp/auth-exp/src/core/persistence/in_memory.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import { expect } from 'chai';
1919

20-
import { testUser } from '../../../test/helpers/mock_auth';
20+
import { testUser, testAuth } from '../../../test/helpers/mock_auth';
2121
import { _getInstance } from '../util/instantiator';
2222
import { Persistence, PersistenceType } from './';
2323
import { inMemoryPersistence } from './in_memory';
@@ -38,11 +38,12 @@ describe('core/persistence/in_memory', () => {
3838

3939
it('should work with user', async () => {
4040
const key = 'my-super-special-user';
41-
const value = testUser({}, 'uid');
41+
const auth = await testAuth();
42+
const value = testUser(auth, 'uid');
4243

4344
expect(await persistence.get(key)).to.be.null;
44-
await persistence.set(key, value.toPlainObject());
45-
expect(await persistence.get(key)).to.eql(value.toPlainObject());
45+
await persistence.set(key, value.toJSON());
46+
expect(await persistence.get(key)).to.eql(value.toJSON());
4647
expect(await persistence.get('other-key')).to.be.null;
4748
await persistence.remove(key);
4849
expect(await persistence.get(key)).to.be.null;

packages-exp/auth-exp/src/core/persistence/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ export enum PersistenceType {
2121
NONE = 'NONE'
2222
}
2323

24-
export interface PersistedBlob {
25-
[key: string]: unknown;
26-
}
24+
export type PersistedBlob = Record<string, unknown>;
2725

2826
export interface Instantiator<T> {
2927
(blob: PersistedBlob): T;

0 commit comments

Comments
 (0)