@thzero/library_client_firebase 0.17.6 → 0.17.8
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 +42 -40
- package/package.json +3 -3
- package/service/index.js +200 -76
package/boot/starter.js
CHANGED
|
@@ -1,48 +1,50 @@
|
|
|
1
|
-
import { initializeApp } from 'firebase/app';
|
|
2
|
-
import { getAuth } from "firebase/auth";
|
|
3
|
-
import { getAnalytics } from "firebase/analytics";
|
|
1
|
+
// import { initializeApp } from 'firebase/app';
|
|
2
|
+
// import { getAuth } from "firebase/auth";
|
|
3
|
+
// import { getAnalytics } from "firebase/analytics";
|
|
4
4
|
|
|
5
5
|
import LibraryClientConstants from '@thzero/library_client/constants';
|
|
6
6
|
|
|
7
7
|
import LibrartyClientUtility from '@thzero/library_client/utility/index';
|
|
8
8
|
|
|
9
|
-
import config from 'local-config';
|
|
9
|
+
// import config from 'local-config';
|
|
10
10
|
|
|
11
11
|
// export default async ({
|
|
12
|
-
export default (
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
12
|
+
export default (router) => {
|
|
13
|
+
const auth = LibrartyClientUtility.$injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_AUTH);
|
|
14
|
+
return auth.initialize(router);
|
|
15
|
+
// const configExternal = config.external;
|
|
16
|
+
// if (!configExternal)
|
|
17
|
+
// throw Error('Invalid external config.');
|
|
18
|
+
// const configFirebase = configExternal.firebase;
|
|
19
|
+
// if (!configFirebase)
|
|
20
|
+
// throw Error('Invalid firebase config.');
|
|
21
|
+
// initializeApp(configFirebase);
|
|
22
|
+
// if (configFirebase.measurementId)
|
|
23
|
+
// getAnalytics();
|
|
24
|
+
|
|
25
|
+
// let outsideResolve;
|
|
26
|
+
// let outsideReject;
|
|
27
|
+
// const promiseAuth = new Promise(function(resolve, reject) {
|
|
28
|
+
// outsideResolve = resolve;
|
|
29
|
+
// outsideReject = reject;
|
|
30
|
+
// });
|
|
31
|
+
|
|
32
|
+
// const firebaseAuth = getAuth();
|
|
33
|
+
// // eslint-disable-next-line
|
|
34
|
+
// let init = false;
|
|
35
|
+
// firebaseAuth.onAuthStateChanged(async function(user) {
|
|
36
|
+
// const auth = LibrartyClientUtility.$injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_AUTH);
|
|
37
|
+
// await auth.onAuthStateChanged(user);
|
|
38
|
+
// if (!init) {
|
|
39
|
+
// init = true;
|
|
40
|
+
// outsideResolve(true);
|
|
41
|
+
// return;
|
|
42
|
+
// }
|
|
43
|
+
|
|
44
|
+
// outsideReject();
|
|
45
|
+
// });
|
|
46
|
+
|
|
47
|
+
// setup();
|
|
48
|
+
|
|
49
|
+
// return promiseAuth;
|
|
48
50
|
};
|
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.8",
|
|
4
4
|
"version_major": 0,
|
|
5
5
|
"version_minor": 17,
|
|
6
|
-
"version_patch":
|
|
7
|
-
"version_date": "
|
|
6
|
+
"version_patch": 8,
|
|
7
|
+
"version_date": "04/20/2023",
|
|
8
8
|
"author": "thZero",
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"repository": {
|
package/service/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
// import
|
|
1
|
+
import { initializeApp } from 'firebase/app';
|
|
2
|
+
// import { getAuth } from "firebase/auth";
|
|
3
|
+
import { getAnalytics } from "firebase/analytics";
|
|
3
4
|
import { getAuth, signInWithPopup, GoogleAuthProvider } from "firebase/auth";
|
|
4
5
|
|
|
5
6
|
import LibraryClientConstants from '@thzero/library_client/constants';
|
|
@@ -18,6 +19,12 @@ class FirebaseAuthService extends UserAuthService {
|
|
|
18
19
|
this._serviceRouter = null;
|
|
19
20
|
}
|
|
20
21
|
|
|
22
|
+
async init(injector) {
|
|
23
|
+
await super.init(injector);
|
|
24
|
+
|
|
25
|
+
this._serviceRouter = this._injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_ROUTER);
|
|
26
|
+
}
|
|
27
|
+
|
|
21
28
|
async deleteUser(correlationId) {
|
|
22
29
|
try {
|
|
23
30
|
const user = await getAuth().currentUser;
|
|
@@ -33,28 +40,56 @@ class FirebaseAuthService extends UserAuthService {
|
|
|
33
40
|
}
|
|
34
41
|
}
|
|
35
42
|
|
|
36
|
-
async init(injector) {
|
|
37
|
-
await super.init(injector);
|
|
38
|
-
|
|
39
|
-
this._serviceRouter = this._injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_ROUTER);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
43
|
get externalUser() {
|
|
43
44
|
const user = getAuth().currentUser;
|
|
44
45
|
this._logger.debug('FirebaseAuthService', 'tokenUser', 'user', user, LibraryCommonUtility.generateId());
|
|
45
46
|
return user;
|
|
46
47
|
}
|
|
47
48
|
|
|
49
|
+
async initialize(router) {
|
|
50
|
+
const configExternal = this._config.getExternal();
|
|
51
|
+
if (!configExternal)
|
|
52
|
+
throw Error('Invalid external config.');
|
|
53
|
+
const configFirebase = configExternal.firebase;
|
|
54
|
+
if (!configFirebase)
|
|
55
|
+
throw Error('Invalid firebase config.');
|
|
56
|
+
initializeApp(configFirebase);
|
|
57
|
+
if (configFirebase.measurementId)
|
|
58
|
+
getAnalytics();
|
|
59
|
+
|
|
60
|
+
let outsideResolve;
|
|
61
|
+
let outsideReject;
|
|
62
|
+
const promiseAuth = new Promise(function(resolve, reject) {
|
|
63
|
+
outsideResolve = resolve;
|
|
64
|
+
outsideReject = reject;
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const self = this;
|
|
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
|
+
}
|
|
80
|
+
|
|
81
|
+
outsideReject();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
return promiseAuth;
|
|
85
|
+
}
|
|
86
|
+
|
|
48
87
|
get isAuthenticated() {
|
|
49
88
|
const user = getAuth().currentUser;
|
|
50
89
|
this._logger.debug('FirebaseAuthService', 'isAuthenticated', 'user', user);
|
|
51
90
|
return user !== null;
|
|
52
91
|
}
|
|
53
92
|
|
|
54
|
-
get token() {
|
|
55
|
-
return this._serviceUser.token;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
93
|
async onAuthStateChanged(user) {
|
|
59
94
|
const correlationId = LibraryCommonUtility.correlationId();
|
|
60
95
|
try {
|
|
@@ -86,6 +121,149 @@ class FirebaseAuthService extends UserAuthService {
|
|
|
86
121
|
// }
|
|
87
122
|
}
|
|
88
123
|
|
|
124
|
+
async refreshToken(correlationId, user, forceRefresh) {
|
|
125
|
+
forceRefresh = forceRefresh !== null ? forceRefresh : false;
|
|
126
|
+
|
|
127
|
+
try {
|
|
128
|
+
this._logger.debug('FirebaseAuthService', 'refreshToken', 'user', user, correlationId);
|
|
129
|
+
if (!user) {
|
|
130
|
+
await this._serviceUser.setTokenResult(correlationId, null);
|
|
131
|
+
await this._serviceUser.setClaims(correlationId, null);
|
|
132
|
+
this.announceToken(correlationId, user, null);
|
|
133
|
+
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
this._logger.debug('FirebaseAuthService', 'refreshToken', 'forceRefresh', forceRefresh, correlationId);
|
|
138
|
+
const currentUser = await getAuth().currentUser;
|
|
139
|
+
this._logger.debug('FirebaseAuthService', 'refreshToken', 'currentUser', currentUser, correlationId);
|
|
140
|
+
if (!currentUser)
|
|
141
|
+
return;
|
|
142
|
+
|
|
143
|
+
const tokenResult = await currentUser.getIdTokenResult(forceRefresh);
|
|
144
|
+
if (tokenResult) {
|
|
145
|
+
await this._serviceUser.setTokenResult(correlationId, tokenResult);
|
|
146
|
+
const token = tokenResult.token;
|
|
147
|
+
let claims = token != null ? tokenResult.claims : null;
|
|
148
|
+
this._logger.debug('FirebaseAuthService', 'refreshToken', 'claims', claims, correlationId);
|
|
149
|
+
claims = claims != null ? claims.custom : null;
|
|
150
|
+
this._logger.debug('FirebaseAuthService', 'refreshToken', 'claims.custom', claims, correlationId);
|
|
151
|
+
await this._serviceUser.setClaims(correlationId, claims);
|
|
152
|
+
|
|
153
|
+
this.announceToken(correlationId, user, token);
|
|
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)
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
await this._serviceUser.setTokenResult(correlationId, null);
|
|
174
|
+
await this._serviceUser.setClaims(correlationId, null);
|
|
175
|
+
|
|
176
|
+
this.announceToken(correlationId, user, token);
|
|
177
|
+
|
|
178
|
+
if (this._polling)
|
|
179
|
+
clearInterval(this._polling);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
catch (err) {
|
|
183
|
+
this._logger.exception('FirebaseAuthService', 'refreshToken', err, correlationId);
|
|
184
|
+
throw err;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async resolveAuthorization(correlationId, requiresAuthRoles, requiresAuthLogical) {
|
|
189
|
+
// const serviceAuth = LibraryClientUtility.$injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_AUTH);
|
|
190
|
+
// const serviceLogger = LibraryClientUtility.$injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_LOGGER);
|
|
191
|
+
// const serviceSecurity = LibraryClientUtility.$injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_SECURITY);
|
|
192
|
+
// const serviceStore = LibraryClientUtility.$injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_STORE);
|
|
193
|
+
this._serviceLogger.info2('requiresAuth');
|
|
194
|
+
let isLoggedIn = this.isAuthenticated;
|
|
195
|
+
this._serviceLogger.info2('authorization.isLoggedIn', isLoggedIn);
|
|
196
|
+
console.log('authorization.isLoggedIn', isLoggedIn);
|
|
197
|
+
if (!isLoggedIn) {
|
|
198
|
+
// Briefly wait for authentication to settle...
|
|
199
|
+
let i = 0;
|
|
200
|
+
while (await this.sleep(150)) {
|
|
201
|
+
if (this._serviceStore.userAuthCompleted) {
|
|
202
|
+
this._serviceLogger.info2('authorization.userAuthCompleted', userAuthCompleted);
|
|
203
|
+
console.log('authorization.userAuthCompleted', userAuthCompleted);
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
i++;
|
|
207
|
+
this._serviceLogger.info2('waiting... ' + i);
|
|
208
|
+
if (i > 5) {
|
|
209
|
+
this._serviceLogger.warn2('authorization.userAuthCompleted failed');
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
const isLoggedInAuthCompleted = this.isAuthenticated;
|
|
214
|
+
this._serviceLogger.info2('authorization.isLoggedIn.userAuthCompleted', isLoggedInAuthCompleted);
|
|
215
|
+
console.log('authorization.isLoggedIn.userAuthCompleted', isLoggedInAuthCompleted);
|
|
216
|
+
isLoggedIn = isLoggedInAuthCompleted;
|
|
217
|
+
}
|
|
218
|
+
this._serviceLogger.info2('authorization.isLoggedIn.final', isLoggedIn);
|
|
219
|
+
console.log('authorization.isLoggedIn.final', isLoggedIn);
|
|
220
|
+
if (!isLoggedIn) {
|
|
221
|
+
this._serviceLogger.warn2('authorization.isLoggedIn - failed');
|
|
222
|
+
console.log('authorization.isLoggedIn - failed');
|
|
223
|
+
// LibraryClientUtility.$EventBus.on('auth-refresh', (user) => {
|
|
224
|
+
// this._serviceLogger.debug('auth-refresh', user)
|
|
225
|
+
// this._serviceLogger.debug('middleware', 'auth-refresh', null, user, correlationId);
|
|
226
|
+
// next()
|
|
227
|
+
// })
|
|
228
|
+
// return
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
this._serviceLogger.info2('authorization.isLoggedIn - success');
|
|
233
|
+
console.log('authorization.isLoggedIn - success');
|
|
234
|
+
|
|
235
|
+
const user = this._serviceStore.user;
|
|
236
|
+
let success = true;
|
|
237
|
+
this._serviceLogger.info2('authorization.requiresAuthRoles', requiresAuthRoles);
|
|
238
|
+
console.log('authorization.requiresAuthRoles', requiresAuthRoles);
|
|
239
|
+
this._serviceLogger.info2('authorization.requiresAuthLogical', requiresAuthLogical);
|
|
240
|
+
console.log('authorization.requiresAuthLogical', requiresAuthLogical);
|
|
241
|
+
|
|
242
|
+
if (requiresAuthRoles) {
|
|
243
|
+
success = await this._serviceSecurity.authorizationCheckRoles(correlationId, user, roles, record.meta.requiresAuthLogical);
|
|
244
|
+
this._serviceLogger.info2('authorization.roles.success', success);
|
|
245
|
+
console.log('authorization.roles.success', success);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
this._serviceLogger.debug('middleware', 'authorization', 'success', success, correlationId);
|
|
249
|
+
console.log('authorization.roles.success', success);
|
|
250
|
+
this._serviceLogger.info2('authorization.roles.success', success);
|
|
251
|
+
if (!success) {
|
|
252
|
+
this._serviceLogger.warn2('authorization.roles - failed');
|
|
253
|
+
console.log('authorization.roles - failed');
|
|
254
|
+
LibraryClientUtility.$navRouter.push('/', null, () => {
|
|
255
|
+
// LibraryClientUtility.$navRouter.push('/')
|
|
256
|
+
// window.location.href = '/'
|
|
257
|
+
});
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
this._serviceLogger.info2('authorization.roles - success');
|
|
262
|
+
console.log('authorization.roles - success');
|
|
263
|
+
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
|
|
89
267
|
async signIn(correlationId) {
|
|
90
268
|
if (this.isAuthenticated)
|
|
91
269
|
return false;
|
|
@@ -168,6 +346,16 @@ class FirebaseAuthService extends UserAuthService {
|
|
|
168
346
|
}
|
|
169
347
|
}
|
|
170
348
|
|
|
349
|
+
sleep(ms) {
|
|
350
|
+
return new Promise((resolve) => {
|
|
351
|
+
setTimeout(resolve, ms);
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
get token() {
|
|
356
|
+
return this._serviceUser.token;
|
|
357
|
+
}
|
|
358
|
+
|
|
171
359
|
// async token(forceRefresh) {
|
|
172
360
|
// if (!forceRefresh)
|
|
173
361
|
// forceRefresh = false
|
|
@@ -181,70 +369,6 @@ class FirebaseAuthService extends UserAuthService {
|
|
|
181
369
|
// return this.refreshToken'FirebaseAuthService', 'token', user, forceRefresh)
|
|
182
370
|
// }
|
|
183
371
|
|
|
184
|
-
async refreshToken(correlationId, user, forceRefresh) {
|
|
185
|
-
forceRefresh = forceRefresh !== null ? forceRefresh : false;
|
|
186
|
-
|
|
187
|
-
try {
|
|
188
|
-
this._logger.debug('FirebaseAuthService', 'refreshToken', 'user', user, correlationId);
|
|
189
|
-
if (!user) {
|
|
190
|
-
await this._serviceUser.setTokenResult(correlationId, null);
|
|
191
|
-
await this._serviceUser.setClaims(correlationId, null);
|
|
192
|
-
this.announceToken(correlationId, user, null);
|
|
193
|
-
|
|
194
|
-
return;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
this._logger.debug('FirebaseAuthService', 'refreshToken', 'forceRefresh', forceRefresh, correlationId);
|
|
198
|
-
const currentUser = await getAuth().currentUser;
|
|
199
|
-
this._logger.debug('FirebaseAuthService', 'refreshToken', 'currentUser', currentUser, correlationId);
|
|
200
|
-
if (!currentUser)
|
|
201
|
-
return;
|
|
202
|
-
|
|
203
|
-
const tokenResult = await currentUser.getIdTokenResult(forceRefresh);
|
|
204
|
-
if (tokenResult) {
|
|
205
|
-
await this._serviceUser.setTokenResult(correlationId, tokenResult);
|
|
206
|
-
const token = tokenResult.token;
|
|
207
|
-
let claims = token != null ? tokenResult.claims : null;
|
|
208
|
-
this._logger.debug('FirebaseAuthService', 'refreshToken', 'claims', claims, correlationId);
|
|
209
|
-
claims = claims != null ? claims.custom : null;
|
|
210
|
-
this._logger.debug('FirebaseAuthService', 'refreshToken', 'claims.custom', claims, correlationId);
|
|
211
|
-
await this._serviceUser.setClaims(correlationId, claims);
|
|
212
|
-
|
|
213
|
-
this.announceToken(correlationId, user, token);
|
|
214
|
-
|
|
215
|
-
const expired = LibraryCommonUtility.getDateParse(tokenResult.expirationTime);
|
|
216
|
-
const now = LibraryCommonUtility.getDate();
|
|
217
|
-
const diff = expired.diff(now);
|
|
218
|
-
const min = 5 * 60 * 1000;
|
|
219
|
-
if (diff <= min) {
|
|
220
|
-
await this.refreshToken(correlationId, getAuth().currentUser, true).then();
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
if (this._polling)
|
|
225
|
-
clearInterval(this._polling);
|
|
226
|
-
|
|
227
|
-
const self = this;
|
|
228
|
-
this._polling = setInterval(async () => {
|
|
229
|
-
await self.refreshToken(correlationId, self.user, true).then();
|
|
230
|
-
}, diff); // 60 * 1000)
|
|
231
|
-
}
|
|
232
|
-
else {
|
|
233
|
-
await this._serviceUser.setTokenResult(correlationId, null);
|
|
234
|
-
await this._serviceUser.setClaims(correlationId, null);
|
|
235
|
-
|
|
236
|
-
this.announceToken(correlationId, user, token);
|
|
237
|
-
|
|
238
|
-
if (this._polling)
|
|
239
|
-
clearInterval(this._polling);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
catch (err) {
|
|
243
|
-
this._logger.exception('FirebaseAuthService', 'refreshToken', err, correlationId);
|
|
244
|
-
throw err;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
372
|
async updateExternalUser(correlationId, user) {
|
|
249
373
|
// if (this._lock)
|
|
250
374
|
// return
|