Skip to content

Commit a6c0183

Browse files
committed
PR feedback and more cleanup
1 parent 70823f6 commit a6c0183

File tree

15 files changed

+192
-98
lines changed

15 files changed

+192
-98
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { PhoneOrOauthTokenResponse } from '../../api/authentication/mfa';
19+
import { AuthCore } from '../../model/auth';
20+
import { IdTokenResponse } from '../../model/id_token';
21+
import { EmailAuthCredential, OAuthCredential, PhoneAuthCredential } from './';
22+
import { debugFail } from '../util/assert';
23+
24+
export class AuthCredential {
25+
protected constructor(
26+
readonly providerId: string,
27+
readonly signInMethod: string
28+
) {}
29+
30+
static fromJSON(json: object | string): AuthCredential | null {
31+
for (const fn of [
32+
/* SAMLAuthCredential.fromJSON, */
33+
OAuthCredential.fromJSON,
34+
EmailAuthCredential.fromJSON,
35+
PhoneAuthCredential.fromJSON
36+
]) {
37+
const credential = fn(json);
38+
if (credential) {
39+
return credential;
40+
}
41+
}
42+
return null;
43+
}
44+
45+
toJSON(): object {
46+
return debugFail('not implemented');
47+
}
48+
49+
_getIdTokenResponse(_auth: AuthCore): Promise<PhoneOrOauthTokenResponse> {
50+
return debugFail('not implemented');
51+
}
52+
_linkToIdToken(_auth: AuthCore, _idToken: string): Promise<IdTokenResponse> {
53+
return debugFail('not implemented');
54+
}
55+
_getReauthenticationResolver(_auth: AuthCore): Promise<IdTokenResponse> {
56+
return debugFail('not implemented');
57+
}
58+
}

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

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { PhoneOrOauthTokenResponse } from '../../api/authentication/mfa';
19-
import { AuthCore } from '../../model/auth';
20-
import { IdTokenResponse } from '../../model/id_token';
21-
import { EmailAuthCredential } from './email';
22-
import { PhoneAuthCredential } from './phone';
23-
import { OAuthCredential } from './oauth';
24-
import { debugFail } from '../util/assert';
25-
26-
export abstract class AuthCredential {
27-
protected constructor(
28-
readonly providerId: string,
29-
readonly signInMethod: string
30-
) {}
31-
32-
static fromJSON(json: object | string): AuthCredential | null {
33-
for (const fn of [
34-
/* SAMLAuthCredential.fromJSON, */
35-
OAuthCredential.fromJSON,
36-
EmailAuthCredential.fromJSON,
37-
PhoneAuthCredential.fromJSON
38-
]) {
39-
const credential = fn(json);
40-
if (credential) {
41-
return credential;
42-
}
43-
}
44-
return null;
45-
}
46-
47-
toJSON(): object {
48-
return debugFail('not implemented');
49-
}
50-
51-
_getIdTokenResponse(_auth: AuthCore): Promise<PhoneOrOauthTokenResponse> {
52-
return debugFail('not implemented');
53-
}
54-
_linkToIdToken(_auth: AuthCore, _idToken: string): Promise<IdTokenResponse> {
55-
return debugFail('not implemented');
56-
}
57-
_getReauthenticationResolver(_auth: AuthCore): Promise<IdTokenResponse> {
58-
return debugFail('not implemented');
59-
}
60-
}
18+
export { AuthCredential } from './auth_credential';
19+
export { EmailAuthCredential } from './email';
20+
export { OAuthCredential } from './oauth';
21+
export { PhoneAuthCredential } from './phone';

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ describe('core/persistence/persistence_user_manager', () => {
134134

135135
it('#getCurrentUser calls with instantiator', async () => {
136136
const rawObject = {};
137-
const userImplStub = sinon.stub(UserImpl, 'fromPlainObject');
137+
const userImplStub = sinon.stub(UserImpl, '_fromJSON');
138138
persistenceStub.get.returns(Promise.resolve(rawObject));
139139

140140
await manager.getCurrentUser();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class PersistenceUserManager {
5858

5959
async getCurrentUser(): Promise<User | null> {
6060
const blob = await this.persistence.get<PersistedBlob>(this.fullUserKey);
61-
return blob ? UserImpl.fromPlainObject(this.auth, blob) : null;
61+
return blob ? UserImpl._fromJSON(this.auth, blob) : null;
6262
}
6363

6464
removeCurrentUser(): Promise<void> {

packages-exp/auth-exp/src/core/user/id_token_result.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { base64Decode } from '@firebase/util';
2121
import { User } from '../../model/user';
2222
import { assert } from '../util/assert';
2323
import { _logError } from '../util/log';
24+
import { utcTimestampToDateString } from '../util/time';
2425

2526
export function getIdToken(
2627
user: externs.User,
@@ -67,19 +68,6 @@ function secondsStringToMilliseconds(seconds: string): number {
6768
return Number(seconds) * 1000;
6869
}
6970

70-
function utcTimestampToDateString(timestamp: string | number): string {
71-
try {
72-
const date = new Date(Number(timestamp));
73-
if (!isNaN(date.getTime())) {
74-
return date.toUTCString();
75-
}
76-
} catch {
77-
// Do nothing, return null
78-
}
79-
80-
return ''; // TODO(avolkovi): is this the right fallback?
81-
}
82-
8371
export function _parseToken(token: string): externs.ParsedToken | null {
8472
const [algorithm, payload, signature] = token.split('.');
8573
if (

packages-exp/auth-exp/src/core/user/reload.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
ProviderUserInfo
3232
} from '../../api/account_management/account';
3333
import { _reloadWithoutSaving, reload } from './reload';
34+
import { UserMetadata } from './user_impl';
3435

3536
use(chaiAsPromised);
3637
use(sinonChai);
@@ -89,9 +90,9 @@ describe('core/user/reload', () => {
8990
expect(user.emailVerified).to.be.true;
9091
expect(user.phoneNumber).to.eq('phone-number');
9192
expect(user.tenantId).to.eq('tenant-id');
92-
expect(user.metadata).to.eql({
93-
creationTime: '123',
94-
lastSignInTime: '456'
93+
expect((user.metadata as UserMetadata).toJSON()).to.eql({
94+
createdAt: '123',
95+
lastLoginAt: '456'
9596
});
9697
});
9798

packages-exp/auth-exp/src/core/user/reload.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
ProviderUserInfo
2323
} from '../../api/account_management/account';
2424
import { User } from '../../model/user';
25+
import { UserMetadata } from './user_impl';
2526
import { assert } from '../util/assert';
2627

2728
export async function _reloadWithoutSaving(user: User): Promise<void> {
@@ -47,10 +48,10 @@ export async function _reloadWithoutSaving(user: User): Promise<void> {
4748
phoneNumber: coreAccount.phoneNumber || null,
4849
tenantId: coreAccount.tenantId || null,
4950
providerData: mergeProviderData(user.providerData, newProviderData),
50-
metadata: {
51-
creationTime: coreAccount.createdAt?.toString(),
52-
lastSignInTime: coreAccount.lastLoginAt?.toString()
53-
}
51+
metadata: new UserMetadata(
52+
coreAccount.createdAt?.toString(),
53+
coreAccount.lastLoginAt?.toString()
54+
)
5455
};
5556

5657
Object.assign(user, updates);

packages-exp/auth-exp/src/core/user/token_manager.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ describe('core/user/token_manager', () => {
170170
});
171171
});
172172

173-
describe('.fromPlainObject', () => {
173+
describe('.fromJSON', () => {
174174
const errorString =
175175
'Firebase: An internal AuthError has occurred. (auth/internal-error).';
176176

packages-exp/auth-exp/src/core/user/user_impl.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,20 +150,20 @@ describe('core/user/user_impl', () => {
150150
});
151151
});
152152

153-
describe('.fromPlainObject', () => {
153+
describe('.fromJSON', () => {
154154
const errorString =
155155
'Firebase: An internal AuthError has occurred. (auth/internal-error).';
156156

157157
it('throws an error if uid is not present', () => {
158-
expect(() => UserImpl.fromPlainObject(auth, { name: 'foo' })).to.throw(
158+
expect(() => UserImpl._fromJSON(auth, { name: 'foo' })).to.throw(
159159
FirebaseError,
160160
errorString
161161
);
162162
});
163163

164164
it('throws if a key is not undefined or string', () => {
165165
expect(() =>
166-
UserImpl.fromPlainObject(auth, { uid: 'foo', displayName: 3 })
166+
UserImpl._fromJSON(auth, { uid: 'foo', displayName: 3 })
167167
).to.throw(FirebaseError, errorString);
168168
});
169169

@@ -178,10 +178,12 @@ describe('core/user/user_impl', () => {
178178
displayName: 'name',
179179
email: 'email',
180180
phoneNumber: 'number',
181-
photoURL: 'photo'
181+
photoURL: 'photo',
182+
emailVerified: false,
183+
isAnonymous: false
182184
};
183185

184-
const user = UserImpl.fromPlainObject(auth, params);
186+
const user = UserImpl._fromJSON(auth, params);
185187
expect(user.uid).to.eq(params.uid);
186188
expect(user.displayName).to.eq(params.displayName);
187189
expect(user.email).to.eq(params.email);

0 commit comments

Comments
 (0)