@thzero/library_client_firebase 0.17.8 → 0.17.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/boot/starter.js +5 -4
- package/package.json +4 -4
- package/service/index.js +95 -60
package/boot/starter.js
CHANGED
|
@@ -4,14 +4,15 @@
|
|
|
4
4
|
|
|
5
5
|
import LibraryClientConstants from '@thzero/library_client/constants';
|
|
6
6
|
|
|
7
|
-
import
|
|
7
|
+
import LibraryClientUtility from '@thzero/library_client/utility/index';
|
|
8
|
+
import LibraryCommonUtility from '@thzero/library_common/utility/index';
|
|
8
9
|
|
|
9
10
|
// import config from 'local-config';
|
|
10
11
|
|
|
11
12
|
// export default async ({
|
|
12
13
|
export default (router) => {
|
|
13
|
-
const auth =
|
|
14
|
-
return auth.initialize(router);
|
|
14
|
+
const auth = LibraryClientUtility.$injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_AUTH);
|
|
15
|
+
return auth.initialize(LibraryCommonUtility.correlationId(), router);
|
|
15
16
|
// const configExternal = config.external;
|
|
16
17
|
// if (!configExternal)
|
|
17
18
|
// throw Error('Invalid external config.');
|
|
@@ -33,7 +34,7 @@ export default (router) => {
|
|
|
33
34
|
// // eslint-disable-next-line
|
|
34
35
|
// let init = false;
|
|
35
36
|
// firebaseAuth.onAuthStateChanged(async function(user) {
|
|
36
|
-
// const auth =
|
|
37
|
+
// const auth = LibraryClientUtility.$injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_AUTH);
|
|
37
38
|
// await auth.onAuthStateChanged(user);
|
|
38
39
|
// if (!init) {
|
|
39
40
|
// init = true;
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thzero/library_client_firebase",
|
|
3
|
-
"version": "0.17.
|
|
3
|
+
"version": "0.17.10",
|
|
4
4
|
"version_major": 0,
|
|
5
5
|
"version_minor": 17,
|
|
6
|
-
"version_patch":
|
|
7
|
-
"version_date": "04/
|
|
6
|
+
"version_patch": 10,
|
|
7
|
+
"version_date": "06/04/2023",
|
|
8
8
|
"author": "thZero",
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"repository": {
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"firebase": "^9.
|
|
23
|
+
"firebase": "^9.21.0"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
26
|
"@thzero/library_client": "^0.17",
|
package/service/index.js
CHANGED
|
@@ -5,6 +5,7 @@ import { getAuth, signInWithPopup, GoogleAuthProvider } from "firebase/auth";
|
|
|
5
5
|
|
|
6
6
|
import LibraryClientConstants from '@thzero/library_client/constants';
|
|
7
7
|
|
|
8
|
+
import LibraryClientUtility from '@thzero/library_client/utility/index';
|
|
8
9
|
import LibraryCommonUtility from '@thzero/library_common/utility';
|
|
9
10
|
|
|
10
11
|
import UserAuthService from '@thzero/library_client/service/auth/user';
|
|
@@ -13,6 +14,8 @@ class FirebaseAuthService extends UserAuthService {
|
|
|
13
14
|
constructor() {
|
|
14
15
|
super();
|
|
15
16
|
|
|
17
|
+
this._auth = null;
|
|
18
|
+
|
|
16
19
|
// this._lock = false
|
|
17
20
|
this._polling = null;
|
|
18
21
|
|
|
@@ -27,7 +30,7 @@ class FirebaseAuthService extends UserAuthService {
|
|
|
27
30
|
|
|
28
31
|
async deleteUser(correlationId) {
|
|
29
32
|
try {
|
|
30
|
-
const user = await
|
|
33
|
+
const user = await this.getExternalUser();
|
|
31
34
|
if (!user)
|
|
32
35
|
return;
|
|
33
36
|
|
|
@@ -40,22 +43,25 @@ class FirebaseAuthService extends UserAuthService {
|
|
|
40
43
|
}
|
|
41
44
|
}
|
|
42
45
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
46
|
+
async getExternalUser() {
|
|
47
|
+
if (this._auth) {
|
|
48
|
+
this._logger.debug('FirebaseAuthService', 'tokenUser', 'user', this._auth.currentUser);
|
|
49
|
+
return this._auth.currentUser;
|
|
50
|
+
}
|
|
51
|
+
return null;
|
|
47
52
|
}
|
|
48
53
|
|
|
49
|
-
async initialize(router) {
|
|
54
|
+
async initialize(correlationId, router) {
|
|
50
55
|
const configExternal = this._config.getExternal();
|
|
51
56
|
if (!configExternal)
|
|
52
57
|
throw Error('Invalid external config.');
|
|
53
58
|
const configFirebase = configExternal.firebase;
|
|
54
59
|
if (!configFirebase)
|
|
55
60
|
throw Error('Invalid firebase config.');
|
|
56
|
-
initializeApp(configFirebase);
|
|
57
|
-
if (configFirebase.measurementId)
|
|
58
|
-
|
|
61
|
+
// initializeApp(configFirebase);
|
|
62
|
+
// if (configFirebase.measurementId)
|
|
63
|
+
// getAnalytics();
|
|
64
|
+
this._initializeFirebase(correlationId, configExternal, configFirebase);
|
|
59
65
|
|
|
60
66
|
let outsideResolve;
|
|
61
67
|
let outsideReject;
|
|
@@ -64,30 +70,17 @@ class FirebaseAuthService extends UserAuthService {
|
|
|
64
70
|
outsideReject = reject;
|
|
65
71
|
});
|
|
66
72
|
|
|
67
|
-
|
|
68
|
-
const firebaseAuth = getAuth();
|
|
69
|
-
// eslint-disable-next-line
|
|
70
|
-
let init = false;
|
|
71
|
-
firebaseAuth.onAuthStateChanged(async function(user) {
|
|
72
|
-
// const auth = LibrartyClientUtility.$injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_AUTH);
|
|
73
|
-
// await auth.onAuthStateChanged(user);
|
|
74
|
-
await self.onAuthStateChanged(user);
|
|
75
|
-
if (!init) {
|
|
76
|
-
init = true;
|
|
77
|
-
outsideResolve(true);
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
73
|
+
this._initializeAuth(correlationId, configExternal, configFirebase, outsideResolve, outsideReject);
|
|
80
74
|
|
|
81
|
-
|
|
82
|
-
});
|
|
75
|
+
this._initializeAnalytics(correlationId, configExternal, configFirebase);
|
|
83
76
|
|
|
84
77
|
return promiseAuth;
|
|
85
78
|
}
|
|
86
79
|
|
|
87
|
-
|
|
88
|
-
const user =
|
|
80
|
+
async isAuthenticated() {
|
|
81
|
+
const user = await this.getExternalUser();
|
|
89
82
|
this._logger.debug('FirebaseAuthService', 'isAuthenticated', 'user', user);
|
|
90
|
-
return user
|
|
83
|
+
return LibraryCommonUtility.isNotNull(user);
|
|
91
84
|
}
|
|
92
85
|
|
|
93
86
|
async onAuthStateChanged(user) {
|
|
@@ -123,6 +116,7 @@ class FirebaseAuthService extends UserAuthService {
|
|
|
123
116
|
|
|
124
117
|
async refreshToken(correlationId, user, forceRefresh) {
|
|
125
118
|
forceRefresh = forceRefresh !== null ? forceRefresh : false;
|
|
119
|
+
this._logger.debug('FirebaseAuthService', 'refreshToken', 'forceRefresh', forceRefresh, correlationId);
|
|
126
120
|
|
|
127
121
|
try {
|
|
128
122
|
this._logger.debug('FirebaseAuthService', 'refreshToken', 'user', user, correlationId);
|
|
@@ -135,49 +129,34 @@ class FirebaseAuthService extends UserAuthService {
|
|
|
135
129
|
}
|
|
136
130
|
|
|
137
131
|
this._logger.debug('FirebaseAuthService', 'refreshToken', 'forceRefresh', forceRefresh, correlationId);
|
|
138
|
-
const currentUser = await
|
|
132
|
+
const currentUser = await this.getExternalUser();
|
|
139
133
|
this._logger.debug('FirebaseAuthService', 'refreshToken', 'currentUser', currentUser, correlationId);
|
|
140
134
|
if (!currentUser)
|
|
141
135
|
return;
|
|
142
136
|
|
|
143
|
-
|
|
137
|
+
if (this._polling)
|
|
138
|
+
clearInterval(this._polling);
|
|
139
|
+
|
|
140
|
+
let token = null;
|
|
141
|
+
|
|
142
|
+
const tokenResult = await this.refreshTokenResult(correlationId, forceRefresh);
|
|
144
143
|
if (tokenResult) {
|
|
145
144
|
await this._serviceUser.setTokenResult(correlationId, tokenResult);
|
|
146
|
-
|
|
145
|
+
token = tokenResult.token;
|
|
147
146
|
let claims = token != null ? tokenResult.claims : null;
|
|
148
147
|
this._logger.debug('FirebaseAuthService', 'refreshToken', 'claims', claims, correlationId);
|
|
149
148
|
claims = claims != null ? claims.custom : null;
|
|
150
149
|
this._logger.debug('FirebaseAuthService', 'refreshToken', 'claims.custom', claims, correlationId);
|
|
151
150
|
await this._serviceUser.setClaims(correlationId, claims);
|
|
152
151
|
|
|
153
|
-
this.
|
|
154
|
-
|
|
155
|
-
const expired = LibraryCommonUtility.getDateParse(tokenResult.expirationTime);
|
|
156
|
-
const now = LibraryCommonUtility.getDate();
|
|
157
|
-
const diff = expired.diff(now);
|
|
158
|
-
const min = 5 * 60 * 1000;
|
|
159
|
-
if (diff <= min) {
|
|
160
|
-
await this.refreshToken(correlationId, getAuth().currentUser, true).then();
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
if (this._polling)
|
|
165
|
-
clearInterval(this._polling);
|
|
166
|
-
|
|
167
|
-
const self = this;
|
|
168
|
-
this._polling = setInterval(async () => {
|
|
169
|
-
await self.refreshToken(correlationId, self.user, true).then();
|
|
170
|
-
}, diff); // 60 * 1000)
|
|
152
|
+
this.refreshTokenExpiration(correlationId, tokenResult, user);
|
|
171
153
|
}
|
|
172
154
|
else {
|
|
173
155
|
await this._serviceUser.setTokenResult(correlationId, null);
|
|
174
156
|
await this._serviceUser.setClaims(correlationId, null);
|
|
175
|
-
|
|
176
|
-
this.announceToken(correlationId, user, token);
|
|
177
|
-
|
|
178
|
-
if (this._polling)
|
|
179
|
-
clearInterval(this._polling);
|
|
180
157
|
}
|
|
158
|
+
|
|
159
|
+
await this.announceToken(correlationId, user, token);
|
|
181
160
|
}
|
|
182
161
|
catch (err) {
|
|
183
162
|
this._logger.exception('FirebaseAuthService', 'refreshToken', err, correlationId);
|
|
@@ -185,13 +164,39 @@ class FirebaseAuthService extends UserAuthService {
|
|
|
185
164
|
}
|
|
186
165
|
}
|
|
187
166
|
|
|
167
|
+
async refreshTokenExpiration(correlationId, tokenResult, user) {
|
|
168
|
+
const expired = LibraryCommonUtility.getDateParse(tokenResult.expirationTime);
|
|
169
|
+
const now = LibraryCommonUtility.getDate();
|
|
170
|
+
const diff = expired.diff(now);
|
|
171
|
+
const min = 5 * 60 * 1000;
|
|
172
|
+
if (diff <= min) {
|
|
173
|
+
await this.refreshToken(correlationId, await this.getExternalUser(), true).then();
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (this._polling)
|
|
178
|
+
clearInterval(this._polling);
|
|
179
|
+
|
|
180
|
+
const self = this;
|
|
181
|
+
this._polling = setInterval(async () => {
|
|
182
|
+
await self.refreshToken(correlationId, user, true).then();
|
|
183
|
+
}, diff); // 60 * 1000);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async refreshTokenResult(correlationId, forceRefresh) {
|
|
187
|
+
const currentUser = await this.getExternalUser();
|
|
188
|
+
if (!currentUser)
|
|
189
|
+
return null;
|
|
190
|
+
return await currentUser.getIdTokenResult(forceRefresh);
|
|
191
|
+
}
|
|
192
|
+
|
|
188
193
|
async resolveAuthorization(correlationId, requiresAuthRoles, requiresAuthLogical) {
|
|
189
194
|
// const serviceAuth = LibraryClientUtility.$injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_AUTH);
|
|
190
195
|
// const serviceLogger = LibraryClientUtility.$injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_LOGGER);
|
|
191
196
|
// const serviceSecurity = LibraryClientUtility.$injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_SECURITY);
|
|
192
197
|
// const serviceStore = LibraryClientUtility.$injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_STORE);
|
|
193
198
|
this._serviceLogger.info2('requiresAuth');
|
|
194
|
-
let isLoggedIn = this.isAuthenticated;
|
|
199
|
+
let isLoggedIn = await this.isAuthenticated();
|
|
195
200
|
this._serviceLogger.info2('authorization.isLoggedIn', isLoggedIn);
|
|
196
201
|
console.log('authorization.isLoggedIn', isLoggedIn);
|
|
197
202
|
if (!isLoggedIn) {
|
|
@@ -210,7 +215,7 @@ class FirebaseAuthService extends UserAuthService {
|
|
|
210
215
|
break;
|
|
211
216
|
}
|
|
212
217
|
}
|
|
213
|
-
const isLoggedInAuthCompleted = this.isAuthenticated;
|
|
218
|
+
const isLoggedInAuthCompleted = await this.isAuthenticated();
|
|
214
219
|
this._serviceLogger.info2('authorization.isLoggedIn.userAuthCompleted', isLoggedInAuthCompleted);
|
|
215
220
|
console.log('authorization.isLoggedIn.userAuthCompleted', isLoggedInAuthCompleted);
|
|
216
221
|
isLoggedIn = isLoggedInAuthCompleted;
|
|
@@ -265,12 +270,12 @@ class FirebaseAuthService extends UserAuthService {
|
|
|
265
270
|
}
|
|
266
271
|
|
|
267
272
|
async signIn(correlationId) {
|
|
268
|
-
if (this.isAuthenticated)
|
|
273
|
+
if (await this.isAuthenticated())
|
|
269
274
|
return false;
|
|
270
275
|
|
|
271
276
|
try {
|
|
272
277
|
const provider = new GoogleAuthProvider();
|
|
273
|
-
const result = await signInWithPopup(
|
|
278
|
+
const result = await signInWithPopup(this._auth, provider);
|
|
274
279
|
if (result && result.user) {
|
|
275
280
|
//const credential = GoogleAuthProvider.credentialFromResult(result);
|
|
276
281
|
// const token = credential.accessToken;
|
|
@@ -293,7 +298,7 @@ class FirebaseAuthService extends UserAuthService {
|
|
|
293
298
|
async signInCompleted(correlationId) {
|
|
294
299
|
// if (await auth.isAuthenticated())
|
|
295
300
|
// return
|
|
296
|
-
//
|
|
301
|
+
// this._auth.getRedirectResult().then(function (result) {
|
|
297
302
|
// if (result.credential) {
|
|
298
303
|
// // This gives you a Google Access Token. You can use it to access the Google API.
|
|
299
304
|
// // eslint-disable-next-line
|
|
@@ -321,14 +326,14 @@ class FirebaseAuthService extends UserAuthService {
|
|
|
321
326
|
|
|
322
327
|
async signOut(correlationId) {
|
|
323
328
|
try {
|
|
324
|
-
// await
|
|
329
|
+
// await this._auth.signOut()
|
|
325
330
|
// await this._serviceUser.dispatcher.user.setTokenResult(correlationId, null)
|
|
326
331
|
// await this._serviceUser.dispatcher.user.setClaims(correlationId, null)
|
|
327
332
|
// await this._serviceUser.dispatcher.user.setUser(correlationId, null)
|
|
328
333
|
// await this._serviceUser.dispatcher.user.setLoggedIn(correlationId, false)
|
|
329
334
|
|
|
330
335
|
const list = [];
|
|
331
|
-
list.push(
|
|
336
|
+
list.push(this._auth.signOut());
|
|
332
337
|
// list.push(this._serviceUser.dispatcher.user.setTokenResult(correlationId, null))
|
|
333
338
|
// list.push(this._serviceUser.dispatcher.user.setClaims(correlationId, null))
|
|
334
339
|
// list.push(this._serviceUser.dispatcher.user.setUser(correlationId, null))
|
|
@@ -410,6 +415,36 @@ class FirebaseAuthService extends UserAuthService {
|
|
|
410
415
|
|
|
411
416
|
return null;
|
|
412
417
|
}
|
|
418
|
+
|
|
419
|
+
_initializeAnalytics(correlationId, configExternal, configFirebase) {
|
|
420
|
+
if (configFirebase.measurementId)
|
|
421
|
+
getAnalytics();
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
_initializeAuth(correlationId, configExternal, configFirebase, outsideResolve, outsideReject) {
|
|
425
|
+
this._auth = getAuth();
|
|
426
|
+
|
|
427
|
+
const self = this;
|
|
428
|
+
const firebaseAuth = this._auth;
|
|
429
|
+
// eslint-disable-next-line
|
|
430
|
+
let init = false;
|
|
431
|
+
firebaseAuth.onAuthStateChanged(async function(user) {
|
|
432
|
+
// const auth = LibraryClientUtility.$injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_AUTH);
|
|
433
|
+
// await auth.onAuthStateChanged(user);
|
|
434
|
+
await self.onAuthStateChanged(user);
|
|
435
|
+
if (!init) {
|
|
436
|
+
init = true;
|
|
437
|
+
outsideResolve(true);
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
outsideReject();
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
_initializeFirebase(correlationId, configExternal, configFirebase) {
|
|
446
|
+
initializeApp(configFirebase);
|
|
447
|
+
}
|
|
413
448
|
}
|
|
414
449
|
|
|
415
450
|
export default FirebaseAuthService;
|