Skip to content

Commit 34ae33a

Browse files
committed
Initial user object implementation
1 parent a627ebf commit 34ae33a

File tree

5 files changed

+250
-46
lines changed

5 files changed

+250
-46
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 { expect } from 'chai';
19+
import { UserImpl } from './user_impl';
20+
import { mockAuth } from '../../test/mock_auth';
21+
22+
describe('core/user_impl', () => {
23+
const auth = mockAuth('foo', 'i-am-the-api-key');
24+
25+
describe('constructor', () => {
26+
it('attaches required fields', () => {
27+
const user = new UserImpl({uid: 'uid', auth});
28+
expect(user.auth).to.eq(auth);
29+
expect(user.uid).to.eq('uid');
30+
});
31+
32+
it('attaches optional fields if provided', () => {
33+
const user = new UserImpl({
34+
uid: 'uid',
35+
auth,
36+
displayName: 'displayName',
37+
email: 'email',
38+
phoneNumber: 'phoneNumber',
39+
photoURL: 'photoURL',
40+
});
41+
42+
expect(user.displayName).to.eq('displayName');
43+
expect(user.email).to.eq('email');
44+
expect(user.phoneNumber).to.eq('phoneNumber');
45+
expect(user.photoURL).to.eq('photoURL');
46+
});
47+
48+
it('sets optional fields to null if not provided', () => {
49+
const user = new UserImpl({uid: 'uid', auth});
50+
expect(user.displayName).to.eq(null);
51+
expect(user.email).to.eq(null);
52+
expect(user.phoneNumber).to.eq(null);
53+
expect(user.photoURL).to.eq(null);
54+
});
55+
});
56+
57+
describe('#getIdToken', () => {
58+
it('throws', () => {
59+
const user = new UserImpl({uid: 'uid', auth});
60+
expect(() => user.getIdToken()).to.throw();
61+
});
62+
});
63+
64+
describe('#getIdTokenResult', () => {
65+
it('throws', () => {
66+
const user = new UserImpl({uid: 'uid', auth});
67+
expect(() => user.getIdTokenResult()).to.throw();
68+
});
69+
});
70+
71+
describe('#reload', () => {
72+
it('throws', () => {
73+
const user = new UserImpl({uid: 'uid', auth});
74+
expect(() => user.reload()).to.throw();
75+
});
76+
});
77+
78+
describe('#delete', () => {
79+
it('throws', () => {
80+
const user = new UserImpl({uid: 'uid', auth});
81+
expect(() => user.delete()).to.throw();
82+
});
83+
});
84+
});
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 { User } from '../model/user';
19+
import { Auth } from '../model/auth';
20+
import { IdTokenResult } from '../model/id_token';
21+
import { ProviderId } from './providers';
22+
23+
export interface UserParameters {
24+
uid: string;
25+
auth: Auth;
26+
27+
displayName?: string;
28+
email?: string;
29+
phoneNumber?: string;
30+
photoURL?: string;
31+
}
32+
33+
export class UserImpl implements User {
34+
// For the user object, provider is always Firebase.
35+
readonly providerId = ProviderId.FIREBASE;
36+
37+
uid: string;
38+
auth: Auth;
39+
40+
// Optional fields from UserInfo
41+
displayName: string | null;
42+
email: string | null;
43+
phoneNumber: string | null;
44+
photoURL: string | null;
45+
46+
constructor({uid, auth, ...opt}: UserParameters) {
47+
this.uid = uid;
48+
this.auth = auth;
49+
this.displayName = opt.displayName || null;
50+
this.email = opt.email || null;
51+
this.phoneNumber = opt.phoneNumber || null;
52+
this.photoURL = opt.photoURL || null;
53+
}
54+
55+
getIdToken(forceRefresh?: boolean): Promise<string> {
56+
throw new Error(`Method not implemented. forceRefresh: ${forceRefresh}`);
57+
}
58+
59+
getIdTokenResult(forceRefresh?: boolean): Promise<IdTokenResult> {
60+
throw new Error(`Method not implemented. forceRefresh: ${forceRefresh}`);
61+
}
62+
63+
reload(): Promise<void> {
64+
throw new Error('Method not implemented.');
65+
}
66+
67+
delete(): Promise<void> {
68+
throw new Error('Method not implemented.');
69+
}
70+
}

packages-exp/auth-exp/src/model/user.d.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,23 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { IdTokenResult } from "./id_token";
19+
import { ProviderId } from "../core/providers";
20+
1821
export interface UserInfo {
1922
readonly uid: string;
20-
}
21-
22-
export interface UserParameters {
23-
uid: string;
23+
readonly providerId: ProviderId;
24+
readonly displayName: string | null;
25+
readonly email: string | null;
26+
readonly phoneNumber: string | null;
27+
readonly photoURL: string | null;
2428
}
2529

2630
export interface User extends UserInfo {
27-
uid: string;
31+
providerId: ProviderId.FIREBASE;
32+
33+
getIdToken(forceRefresh?: boolean): Promise<string>;
34+
getIdTokenResult(forceRefresh?: boolean): Promise<IdTokenResult>;
35+
reload(): Promise<void>;
36+
delete(): Promise<void>;
2837
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 { AppName, ApiKey, Auth } from '../src/model/auth';
19+
20+
export function mockAuth(name: AppName, apiKey: ApiKey): Auth {
21+
return {
22+
name,
23+
config: {
24+
apiKey,
25+
}
26+
};
27+
}

yarn.lock

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10397,6 +10397,36 @@ [email protected]:
1039710397
yargs-parser "13.1.1"
1039810398
yargs-unparser "1.6.0"
1039910399

10400+
mocha@^7.1.1:
10401+
version "7.1.1"
10402+
resolved "https://registry.npmjs.org/mocha/-/mocha-7.1.1.tgz#89fbb30d09429845b1bb893a830bf5771049a441"
10403+
integrity sha512-3qQsu3ijNS3GkWcccT5Zw0hf/rWvu1fTN9sPvEd81hlwsr30GX2GcDSSoBxo24IR8FelmrAydGC6/1J5QQP4WA==
10404+
dependencies:
10405+
ansi-colors "3.2.3"
10406+
browser-stdout "1.3.1"
10407+
chokidar "3.3.0"
10408+
debug "3.2.6"
10409+
diff "3.5.0"
10410+
escape-string-regexp "1.0.5"
10411+
find-up "3.0.0"
10412+
glob "7.1.3"
10413+
growl "1.10.5"
10414+
he "1.2.0"
10415+
js-yaml "3.13.1"
10416+
log-symbols "3.0.0"
10417+
minimatch "3.0.4"
10418+
mkdirp "0.5.3"
10419+
ms "2.1.1"
10420+
node-environment-flags "1.0.6"
10421+
object.assign "4.1.0"
10422+
strip-json-comments "2.0.1"
10423+
supports-color "6.0.0"
10424+
which "1.3.1"
10425+
wide-align "1.1.3"
10426+
yargs "13.3.2"
10427+
yargs-parser "13.1.2"
10428+
yargs-unparser "1.6.0"
10429+
1040010430
modify-values@^1.0.0:
1040110431
version "1.0.1"
1040210432
resolved "https://registry.npmjs.org/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"
@@ -10692,22 +10722,6 @@ node-localstorage@^1.3.1:
1069210722
dependencies:
1069310723
write-file-atomic "^1.1.4"
1069410724

10695-
node-pre-gyp@*:
10696-
version "0.14.0"
10697-
resolved "https://registry.npmjs.org/node-pre-gyp/-/node-pre-gyp-0.14.0.tgz#9a0596533b877289bcad4e143982ca3d904ddc83"
10698-
integrity sha512-+CvDC7ZttU/sSt9rFjix/P05iS43qHCOOGzcr3Ry99bXG7VX953+vFyEuph/tfqoYu8dttBkE86JSKBO2OzcxA==
10699-
dependencies:
10700-
detect-libc "^1.0.2"
10701-
mkdirp "^0.5.1"
10702-
needle "^2.2.1"
10703-
nopt "^4.0.1"
10704-
npm-packlist "^1.1.6"
10705-
npmlog "^4.0.2"
10706-
rc "^1.2.7"
10707-
rimraf "^2.6.1"
10708-
semver "^5.3.0"
10709-
tar "^4.4.2"
10710-
1071110725
node-pre-gyp@^0.11.0:
1071210726
version "0.11.0"
1071310727
resolved "https://registry.npmjs.org/node-pre-gyp/-/node-pre-gyp-0.11.0.tgz#db1f33215272f692cd38f03238e3e9b47c5dd054"
@@ -13915,7 +13929,7 @@ [email protected]:
1391513929
safe-buffer "^5.1.2"
1391613930
yallist "^3.0.2"
1391713931

13918-
tar@^4, tar@^4.3.0, tar@^4.4.10, tar@^4.4.12, tar@^4.4.2, tar@^4.4.8:
13932+
tar@^4, tar@^4.3.0, tar@^4.4.10, tar@^4.4.12, tar@^4.4.8:
1391913933
version "4.4.13"
1392013934
resolved "https://registry.npmjs.org/tar/-/tar-4.4.13.tgz#43b364bc52888d555298637b10d60790254ab525"
1392113935
integrity sha512-w2VwSrBoHa5BsSyH+KxEqeQBAllHhccyMFVHtGtdMpF4W7IRWfZjFiQceJPChOeTsSDVUpER2T8FA93pr0L+QA==
@@ -15483,21 +15497,21 @@ [email protected]:
1548315497
camelcase "^5.0.0"
1548415498
decamelize "^1.2.0"
1548515499

15486-
yargs-parser@^10.0.0:
15487-
version "10.1.0"
15488-
resolved "https://registry.npmjs.org/yargs-parser/-/yargs-parser-10.1.0.tgz#7202265b89f7e9e9f2e5765e0fe735a905edbaa8"
15489-
integrity sha512-VCIyR1wJoEBZUqk5PA+oOBF6ypbwh5aNB3I50guxAL/quggdfs4TtNHQrSazFA3fYZ+tEqfs0zIGlv0c/rgjbQ==
15490-
dependencies:
15491-
camelcase "^4.1.0"
15492-
15493-
yargs-parser@^13.1.0, yargs-parser@^13.1.1, yargs-parser@^13.1.2:
15500+
[email protected], yargs-parser@^13.1.0, yargs-parser@^13.1.1, yargs-parser@^13.1.2:
1549415501
version "13.1.2"
1549515502
resolved "https://registry.npmjs.org/yargs-parser/-/yargs-parser-13.1.2.tgz#130f09702ebaeef2650d54ce6e3e5706f7a4fb38"
1549615503
integrity sha512-3lbsNRf/j+A4QuSZfDRA7HRSfWrzO0YjqTJd5kjAq37Zep1CEgaYmrH9Q3GwPiB9cHyd1Y1UwggGhJGoxipbzg==
1549715504
dependencies:
1549815505
camelcase "^5.0.0"
1549915506
decamelize "^1.2.0"
1550015507

15508+
yargs-parser@^10.0.0:
15509+
version "10.1.0"
15510+
resolved "https://registry.npmjs.org/yargs-parser/-/yargs-parser-10.1.0.tgz#7202265b89f7e9e9f2e5765e0fe735a905edbaa8"
15511+
integrity sha512-VCIyR1wJoEBZUqk5PA+oOBF6ypbwh5aNB3I50guxAL/quggdfs4TtNHQrSazFA3fYZ+tEqfs0zIGlv0c/rgjbQ==
15512+
dependencies:
15513+
camelcase "^4.1.0"
15514+
1550115515
yargs-parser@^15.0.1:
1550215516
version "15.0.1"
1550315517
resolved "https://registry.npmjs.org/yargs-parser/-/yargs-parser-15.0.1.tgz#54786af40b820dcb2fb8025b11b4d659d76323b3"
@@ -15563,6 +15577,22 @@ [email protected]:
1556315577
y18n "^4.0.0"
1556415578
yargs-parser "^13.1.1"
1556515579

15580+
[email protected], yargs@^13.3.0:
15581+
version "13.3.2"
15582+
resolved "https://registry.npmjs.org/yargs/-/yargs-13.3.2.tgz#ad7ffefec1aa59565ac915f82dccb38a9c31a2dd"
15583+
integrity sha512-AX3Zw5iPruN5ie6xGRIDgqkT+ZhnRlZMLMHAs8tg7nRruy2Nb+i5o9bwghAogtM08q1dpr2LVoS8KSTMYpWXUw==
15584+
dependencies:
15585+
cliui "^5.0.0"
15586+
find-up "^3.0.0"
15587+
get-caller-file "^2.0.1"
15588+
require-directory "^2.1.1"
15589+
require-main-filename "^2.0.0"
15590+
set-blocking "^2.0.0"
15591+
string-width "^3.0.0"
15592+
which-module "^2.0.0"
15593+
y18n "^4.0.0"
15594+
yargs-parser "^13.1.2"
15595+
1556615596
[email protected], yargs@^15.0.2:
1556715597
version "15.3.1"
1556815598
resolved "https://registry.npmjs.org/yargs/-/yargs-15.3.1.tgz#9505b472763963e54afe60148ad27a330818e98b"
@@ -15580,22 +15610,6 @@ [email protected], yargs@^15.0.2:
1558015610
y18n "^4.0.0"
1558115611
yargs-parser "^18.1.1"
1558215612

15583-
yargs@^13.3.0:
15584-
version "13.3.2"
15585-
resolved "https://registry.npmjs.org/yargs/-/yargs-13.3.2.tgz#ad7ffefec1aa59565ac915f82dccb38a9c31a2dd"
15586-
integrity sha512-AX3Zw5iPruN5ie6xGRIDgqkT+ZhnRlZMLMHAs8tg7nRruy2Nb+i5o9bwghAogtM08q1dpr2LVoS8KSTMYpWXUw==
15587-
dependencies:
15588-
cliui "^5.0.0"
15589-
find-up "^3.0.0"
15590-
get-caller-file "^2.0.1"
15591-
require-directory "^2.1.1"
15592-
require-main-filename "^2.0.0"
15593-
set-blocking "^2.0.0"
15594-
string-width "^3.0.0"
15595-
which-module "^2.0.0"
15596-
y18n "^4.0.0"
15597-
yargs-parser "^13.1.2"
15598-
1559915613
yargs@^14.2.2:
1560015614
version "14.2.3"
1560115615
resolved "https://registry.npmjs.org/yargs/-/yargs-14.2.3.tgz#1a1c3edced1afb2a2fea33604bc6d1d8d688a414"

0 commit comments

Comments
 (0)