@thzero/library_client_firebase 0.17.8 → 0.17.9
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/README.md +117 -117
- package/boot/starter.js +50 -50
- package/license.md +8 -8
- package/openSource.js +31 -31
- package/package.json +28 -28
- package/service/index.js +415 -415
package/README.md
CHANGED
|
@@ -1,117 +1,117 @@
|
|
|
1
|
-

|
|
2
|
-

|
|
3
|
-
[](https://opensource.org/licenses/MIT)
|
|
4
|
-
|
|
5
|
-
# library_client_firebase
|
|
6
|
-
|
|
7
|
-
## Requirements
|
|
8
|
-
|
|
9
|
-
### NodeJs
|
|
10
|
-
|
|
11
|
-
[NodeJs](https://nodejs.org) version 18+
|
|
12
|
-
|
|
13
|
-
## Installation
|
|
14
|
-
|
|
15
|
-
[](https://npmjs.org/package/@thzero/library_client_firebase)
|
|
16
|
-
|
|
17
|
-
## Configuration
|
|
18
|
-
|
|
19
|
-
### Firebase
|
|
20
|
-
|
|
21
|
-
Google Firebase (https://firebase.google.com) provides the social based authentication; currently only Google social accounts are supported.
|
|
22
|
-
|
|
23
|
-
* Add a new project
|
|
24
|
-
* If not already completed when setting up the server application
|
|
25
|
-
* Setup **Authentication**, enabled Google in the **Sign-in method**.
|
|
26
|
-
* If not already completed when setting up the server application
|
|
27
|
-
* Get the Firebase SDK configuration
|
|
28
|
-
* Go to Project Overview->Settings->General
|
|
29
|
-
* Click **Add App** and select **Web**
|
|
30
|
-
* Click *Firebase SDK snippet*, select **Config*
|
|
31
|
-
* Select the JSON object and store it
|
|
32
|
-
* The contents of the JSON object will be stored as key/value pairs in the external/firebase confib object (below)
|
|
33
|
-
* Supports Firebase Analytics.
|
|
34
|
-
* Go to Project Overview->Settings->Integrations
|
|
35
|
-
* Enable the Google Analytics.
|
|
36
|
-
* Copy the 'measurementId' key//value pair into the external/firebase config object (below)
|
|
37
|
-
|
|
38
|
-
### Application Configuration
|
|
39
|
-
|
|
40
|
-
* In the configuration files (development.json and production.json) of the application
|
|
41
|
-
* Add the following onfiguration block to contain the firebase key.
|
|
42
|
-
|
|
43
|
-
```
|
|
44
|
-
,
|
|
45
|
-
"external": {
|
|
46
|
-
"firebase": <firebase key JSON object from above goes here>
|
|
47
|
-
}
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
### Locales
|
|
51
|
-
|
|
52
|
-
Merge the following to the 'src/locals/en/index.json' file:
|
|
53
|
-
|
|
54
|
-
```
|
|
55
|
-
{
|
|
56
|
-
"admin": {
|
|
57
|
-
"users": "Users"
|
|
58
|
-
},
|
|
59
|
-
"forms": {
|
|
60
|
-
"externalId": "External Id",
|
|
61
|
-
"news": {
|
|
62
|
-
"requiresAuth": "Requires Authentication",
|
|
63
|
-
},
|
|
64
|
-
"role": "Role",
|
|
65
|
-
"roles": "Roles",
|
|
66
|
-
},
|
|
67
|
-
"news": {
|
|
68
|
-
"requiresAuth": "Authenticated",
|
|
69
|
-
},
|
|
70
|
-
"users": {
|
|
71
|
-
"actions": "Actions",
|
|
72
|
-
"externalId": "External Id",
|
|
73
|
-
"id": "Id",
|
|
74
|
-
"name": "Name",
|
|
75
|
-
"role": "Role",
|
|
76
|
-
"roles": "Roles"
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
### Main.js
|
|
82
|
-
|
|
83
|
-
* Add the following import statement to the 'main.js' file.
|
|
84
|
-
|
|
85
|
-
```
|
|
86
|
-
import bootStarter from '@thzero/library_client_firebase/boot/starter';
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
* Adjust the start method of the 'main.js' file to iclude 'bootAuth' as the last parameter.
|
|
90
|
-
|
|
91
|
-
```
|
|
92
|
-
start(app, router, store, vuetify, [ ... ], bootStarter);
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
### Route.js
|
|
96
|
-
|
|
97
|
-
Routes can be denoted as not requiring authentication. To do so, tag on the route the following in the 'meta node'.
|
|
98
|
-
|
|
99
|
-
```
|
|
100
|
-
requiresAuth: false
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
It is advised that the following routes should have authentication turned off.
|
|
104
|
-
|
|
105
|
-
* Home
|
|
106
|
-
* About
|
|
107
|
-
* Open Source
|
|
108
|
-
* Auth
|
|
109
|
-
* Not Found
|
|
110
|
-
* Blank
|
|
111
|
-
|
|
112
|
-
It is advised that the following routes should have authentication turned on.
|
|
113
|
-
|
|
114
|
-
* Admin
|
|
115
|
-
* Settings
|
|
116
|
-
* Support
|
|
117
|
-
* Any application routes that require authenticated users.
|
|
1
|
+

|
|
2
|
+

|
|
3
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
|
+
|
|
5
|
+
# library_client_firebase
|
|
6
|
+
|
|
7
|
+
## Requirements
|
|
8
|
+
|
|
9
|
+
### NodeJs
|
|
10
|
+
|
|
11
|
+
[NodeJs](https://nodejs.org) version 18+
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
[](https://npmjs.org/package/@thzero/library_client_firebase)
|
|
16
|
+
|
|
17
|
+
## Configuration
|
|
18
|
+
|
|
19
|
+
### Firebase
|
|
20
|
+
|
|
21
|
+
Google Firebase (https://firebase.google.com) provides the social based authentication; currently only Google social accounts are supported.
|
|
22
|
+
|
|
23
|
+
* Add a new project
|
|
24
|
+
* If not already completed when setting up the server application
|
|
25
|
+
* Setup **Authentication**, enabled Google in the **Sign-in method**.
|
|
26
|
+
* If not already completed when setting up the server application
|
|
27
|
+
* Get the Firebase SDK configuration
|
|
28
|
+
* Go to Project Overview->Settings->General
|
|
29
|
+
* Click **Add App** and select **Web**
|
|
30
|
+
* Click *Firebase SDK snippet*, select **Config*
|
|
31
|
+
* Select the JSON object and store it
|
|
32
|
+
* The contents of the JSON object will be stored as key/value pairs in the external/firebase confib object (below)
|
|
33
|
+
* Supports Firebase Analytics.
|
|
34
|
+
* Go to Project Overview->Settings->Integrations
|
|
35
|
+
* Enable the Google Analytics.
|
|
36
|
+
* Copy the 'measurementId' key//value pair into the external/firebase config object (below)
|
|
37
|
+
|
|
38
|
+
### Application Configuration
|
|
39
|
+
|
|
40
|
+
* In the configuration files (development.json and production.json) of the application
|
|
41
|
+
* Add the following onfiguration block to contain the firebase key.
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
,
|
|
45
|
+
"external": {
|
|
46
|
+
"firebase": <firebase key JSON object from above goes here>
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Locales
|
|
51
|
+
|
|
52
|
+
Merge the following to the 'src/locals/en/index.json' file:
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
{
|
|
56
|
+
"admin": {
|
|
57
|
+
"users": "Users"
|
|
58
|
+
},
|
|
59
|
+
"forms": {
|
|
60
|
+
"externalId": "External Id",
|
|
61
|
+
"news": {
|
|
62
|
+
"requiresAuth": "Requires Authentication",
|
|
63
|
+
},
|
|
64
|
+
"role": "Role",
|
|
65
|
+
"roles": "Roles",
|
|
66
|
+
},
|
|
67
|
+
"news": {
|
|
68
|
+
"requiresAuth": "Authenticated",
|
|
69
|
+
},
|
|
70
|
+
"users": {
|
|
71
|
+
"actions": "Actions",
|
|
72
|
+
"externalId": "External Id",
|
|
73
|
+
"id": "Id",
|
|
74
|
+
"name": "Name",
|
|
75
|
+
"role": "Role",
|
|
76
|
+
"roles": "Roles"
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Main.js
|
|
82
|
+
|
|
83
|
+
* Add the following import statement to the 'main.js' file.
|
|
84
|
+
|
|
85
|
+
```
|
|
86
|
+
import bootStarter from '@thzero/library_client_firebase/boot/starter';
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
* Adjust the start method of the 'main.js' file to iclude 'bootAuth' as the last parameter.
|
|
90
|
+
|
|
91
|
+
```
|
|
92
|
+
start(app, router, store, vuetify, [ ... ], bootStarter);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Route.js
|
|
96
|
+
|
|
97
|
+
Routes can be denoted as not requiring authentication. To do so, tag on the route the following in the 'meta node'.
|
|
98
|
+
|
|
99
|
+
```
|
|
100
|
+
requiresAuth: false
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
It is advised that the following routes should have authentication turned off.
|
|
104
|
+
|
|
105
|
+
* Home
|
|
106
|
+
* About
|
|
107
|
+
* Open Source
|
|
108
|
+
* Auth
|
|
109
|
+
* Not Found
|
|
110
|
+
* Blank
|
|
111
|
+
|
|
112
|
+
It is advised that the following routes should have authentication turned on.
|
|
113
|
+
|
|
114
|
+
* Admin
|
|
115
|
+
* Settings
|
|
116
|
+
* Support
|
|
117
|
+
* Any application routes that require authenticated users.
|
package/boot/starter.js
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
// import { initializeApp } from 'firebase/app';
|
|
2
|
-
// import { getAuth } from "firebase/auth";
|
|
3
|
-
// import { getAnalytics } from "firebase/analytics";
|
|
4
|
-
|
|
5
|
-
import LibraryClientConstants from '@thzero/library_client/constants';
|
|
6
|
-
|
|
7
|
-
import LibrartyClientUtility from '@thzero/library_client/utility/index';
|
|
8
|
-
|
|
9
|
-
// import config from 'local-config';
|
|
10
|
-
|
|
11
|
-
// export default async ({
|
|
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;
|
|
50
|
-
};
|
|
1
|
+
// import { initializeApp } from 'firebase/app';
|
|
2
|
+
// import { getAuth } from "firebase/auth";
|
|
3
|
+
// import { getAnalytics } from "firebase/analytics";
|
|
4
|
+
|
|
5
|
+
import LibraryClientConstants from '@thzero/library_client/constants';
|
|
6
|
+
|
|
7
|
+
import LibrartyClientUtility from '@thzero/library_client/utility/index';
|
|
8
|
+
|
|
9
|
+
// import config from 'local-config';
|
|
10
|
+
|
|
11
|
+
// export default async ({
|
|
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;
|
|
50
|
+
};
|
package/license.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2020-2023 thZero.com
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
-
|
|
7
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
-
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020-2023 thZero.com
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
9
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/openSource.js
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
export default () => {
|
|
2
|
-
return [
|
|
3
|
-
{
|
|
4
|
-
category: 'client',
|
|
5
|
-
name: '@thzero/library_client',
|
|
6
|
-
url: 'https://github.com/thzero/library_client',
|
|
7
|
-
licenseName: 'MIT',
|
|
8
|
-
licenseUrl: 'https://github.com/thzero/library_client/blob/master/license.md'
|
|
9
|
-
},
|
|
10
|
-
{
|
|
11
|
-
category: 'client',
|
|
12
|
-
name: '@thzero/library_client_firebase',
|
|
13
|
-
url: 'https://github.com/thzero/library_client_firebase',
|
|
14
|
-
licenseName: 'MIT',
|
|
15
|
-
licenseUrl: 'https://github.com/thzero/library_client_firebase/blob/master/license.md'
|
|
16
|
-
},
|
|
17
|
-
{
|
|
18
|
-
category: 'client',
|
|
19
|
-
name: '@thzero/library_common',
|
|
20
|
-
url: 'https://github.com/thzero/library_common',
|
|
21
|
-
licenseName: 'MIT',
|
|
22
|
-
licenseUrl: 'https://github.com/thzero/library_common/blob/master/license.md'
|
|
23
|
-
},
|
|
24
|
-
{
|
|
25
|
-
category: 'client',
|
|
26
|
-
name: 'firebase',
|
|
27
|
-
url: 'https://github.com/firebase/firebase-js-sdk',
|
|
28
|
-
licenseName: 'MIT',
|
|
29
|
-
licenseUrl: 'https://github.com/firebase/firebase-js-sdk/blob/master/LICENSE'
|
|
30
|
-
}
|
|
31
|
-
];
|
|
1
|
+
export default () => {
|
|
2
|
+
return [
|
|
3
|
+
{
|
|
4
|
+
category: 'client',
|
|
5
|
+
name: '@thzero/library_client',
|
|
6
|
+
url: 'https://github.com/thzero/library_client',
|
|
7
|
+
licenseName: 'MIT',
|
|
8
|
+
licenseUrl: 'https://github.com/thzero/library_client/blob/master/license.md'
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
category: 'client',
|
|
12
|
+
name: '@thzero/library_client_firebase',
|
|
13
|
+
url: 'https://github.com/thzero/library_client_firebase',
|
|
14
|
+
licenseName: 'MIT',
|
|
15
|
+
licenseUrl: 'https://github.com/thzero/library_client_firebase/blob/master/license.md'
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
category: 'client',
|
|
19
|
+
name: '@thzero/library_common',
|
|
20
|
+
url: 'https://github.com/thzero/library_common',
|
|
21
|
+
licenseName: 'MIT',
|
|
22
|
+
licenseUrl: 'https://github.com/thzero/library_common/blob/master/license.md'
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
category: 'client',
|
|
26
|
+
name: 'firebase',
|
|
27
|
+
url: 'https://github.com/firebase/firebase-js-sdk',
|
|
28
|
+
licenseName: 'MIT',
|
|
29
|
+
licenseUrl: 'https://github.com/firebase/firebase-js-sdk/blob/master/LICENSE'
|
|
30
|
+
}
|
|
31
|
+
];
|
|
32
32
|
}
|
package/package.json
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@thzero/library_client_firebase",
|
|
3
|
-
"version": "0.17.
|
|
4
|
-
"version_major": 0,
|
|
5
|
-
"version_minor": 17,
|
|
6
|
-
"version_patch":
|
|
7
|
-
"version_date": "
|
|
8
|
-
"author": "thZero",
|
|
9
|
-
"license": "MIT",
|
|
10
|
-
"repository": {
|
|
11
|
-
"type": "git",
|
|
12
|
-
"url": "git+https://github.com/thzero/library_client_firebase.git"
|
|
13
|
-
},
|
|
14
|
-
"bugs": {
|
|
15
|
-
"url": "https://github.com/thzero/library_client_firebase/issues"
|
|
16
|
-
},
|
|
17
|
-
"homepage": "https://github.com/thzero/library_client_firebase#readme",
|
|
18
|
-
"scripts": {
|
|
19
|
-
"cli-update": "library-cli --updateversion --pi",
|
|
20
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
21
|
-
},
|
|
22
|
-
"dependencies": {
|
|
23
|
-
"firebase": "^9.
|
|
24
|
-
},
|
|
25
|
-
"peerDependencies": {
|
|
26
|
-
"@thzero/library_client": "^0.17",
|
|
27
|
-
"@thzero/library_common": "^0.17"
|
|
28
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@thzero/library_client_firebase",
|
|
3
|
+
"version": "0.17.9",
|
|
4
|
+
"version_major": 0,
|
|
5
|
+
"version_minor": 17,
|
|
6
|
+
"version_patch": 9,
|
|
7
|
+
"version_date": "05/10/2023",
|
|
8
|
+
"author": "thZero",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/thzero/library_client_firebase.git"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/thzero/library_client_firebase/issues"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://github.com/thzero/library_client_firebase#readme",
|
|
18
|
+
"scripts": {
|
|
19
|
+
"cli-update": "library-cli --updateversion --pi",
|
|
20
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"firebase": "^9.21.0"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"@thzero/library_client": "^0.17",
|
|
27
|
+
"@thzero/library_common": "^0.17"
|
|
28
|
+
}
|
|
29
29
|
}
|
package/service/index.js
CHANGED
|
@@ -1,415 +1,415 @@
|
|
|
1
|
-
import { initializeApp } from 'firebase/app';
|
|
2
|
-
// import { getAuth } from "firebase/auth";
|
|
3
|
-
import { getAnalytics } from "firebase/analytics";
|
|
4
|
-
import { getAuth, signInWithPopup, GoogleAuthProvider } from "firebase/auth";
|
|
5
|
-
|
|
6
|
-
import LibraryClientConstants from '@thzero/library_client/constants';
|
|
7
|
-
|
|
8
|
-
import LibraryCommonUtility from '@thzero/library_common/utility';
|
|
9
|
-
|
|
10
|
-
import UserAuthService from '@thzero/library_client/service/auth/user';
|
|
11
|
-
|
|
12
|
-
class FirebaseAuthService extends UserAuthService {
|
|
13
|
-
constructor() {
|
|
14
|
-
super();
|
|
15
|
-
|
|
16
|
-
// this._lock = false
|
|
17
|
-
this._polling = null;
|
|
18
|
-
|
|
19
|
-
this._serviceRouter = null;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
async init(injector) {
|
|
23
|
-
await super.init(injector);
|
|
24
|
-
|
|
25
|
-
this._serviceRouter = this._injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_ROUTER);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
async deleteUser(correlationId) {
|
|
29
|
-
try {
|
|
30
|
-
const user = await getAuth().currentUser;
|
|
31
|
-
if (!user)
|
|
32
|
-
return;
|
|
33
|
-
|
|
34
|
-
await user.delete();
|
|
35
|
-
await this._serviceUser.resetUser(correlationId);
|
|
36
|
-
}
|
|
37
|
-
catch (err) {
|
|
38
|
-
this._logger.exception('FirebaseAuthService', 'deleteUser', err, correlationId);
|
|
39
|
-
throw err;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
get externalUser() {
|
|
44
|
-
const user = getAuth().currentUser;
|
|
45
|
-
this._logger.debug('FirebaseAuthService', 'tokenUser', 'user', user, LibraryCommonUtility.generateId());
|
|
46
|
-
return user;
|
|
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
|
-
|
|
87
|
-
get isAuthenticated() {
|
|
88
|
-
const user = getAuth().currentUser;
|
|
89
|
-
this._logger.debug('FirebaseAuthService', 'isAuthenticated', 'user', user);
|
|
90
|
-
return user !== null;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
async onAuthStateChanged(user) {
|
|
94
|
-
const correlationId = LibraryCommonUtility.correlationId();
|
|
95
|
-
try {
|
|
96
|
-
await this.updateExternalUser(correlationId, user, true);
|
|
97
|
-
// if (!user)
|
|
98
|
-
// return
|
|
99
|
-
|
|
100
|
-
await this._serviceUser.setAuthCompleted(correlationId, true);
|
|
101
|
-
this._serviceEvent.emit(LibraryClientConstants.EventKeys.Auth.Refresh, user);
|
|
102
|
-
}
|
|
103
|
-
catch (err) {
|
|
104
|
-
this._logger.exception('FirebaseAuthService', 'onAuthStateChanged', err, correlationId);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// try {
|
|
108
|
-
// if (!user) {
|
|
109
|
-
// if (this._polling)
|
|
110
|
-
// clearInterval(this._polling)
|
|
111
|
-
// return
|
|
112
|
-
// }
|
|
113
|
-
|
|
114
|
-
// const self = this
|
|
115
|
-
// this._polling = setInterval(async () => {
|
|
116
|
-
// await self.refreshToken(self.user, true).then()
|
|
117
|
-
// }, 60 * 1000)
|
|
118
|
-
// }
|
|
119
|
-
// catch (err) {
|
|
120
|
-
// this._logger.exception('FirebaseAuthService', 'onAuthStateChanged', err, correlationId)
|
|
121
|
-
// }
|
|
122
|
-
}
|
|
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
|
-
|
|
267
|
-
async signIn(correlationId) {
|
|
268
|
-
if (this.isAuthenticated)
|
|
269
|
-
return false;
|
|
270
|
-
|
|
271
|
-
try {
|
|
272
|
-
const provider = new GoogleAuthProvider();
|
|
273
|
-
const result = await signInWithPopup(getAuth(), provider);
|
|
274
|
-
if (result && result.user) {
|
|
275
|
-
//const credential = GoogleAuthProvider.credentialFromResult(result);
|
|
276
|
-
// const token = credential.accessToken;
|
|
277
|
-
this.updateExternalUser(correlationId, result.user);
|
|
278
|
-
// this._serviceRouter.route('/')
|
|
279
|
-
window.location.href = '/';
|
|
280
|
-
return true;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
this.updateExternalUser(correlationId, null);
|
|
284
|
-
}
|
|
285
|
-
catch (err) {
|
|
286
|
-
this._logger.exception('FirebaseAuthService', 'signIn', err, correlationId);
|
|
287
|
-
this.updateExternalUser(correlationId, null);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
return true;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
async signInCompleted(correlationId) {
|
|
294
|
-
// if (await auth.isAuthenticated())
|
|
295
|
-
// return
|
|
296
|
-
// getAuth().getRedirectResult().then(function (result) {
|
|
297
|
-
// if (result.credential) {
|
|
298
|
-
// // This gives you a Google Access Token. You can use it to access the Google API.
|
|
299
|
-
// // eslint-disable-next-line
|
|
300
|
-
// var token = result.credential.accessToken
|
|
301
|
-
// // ...
|
|
302
|
-
// }
|
|
303
|
-
// // The signed-in user info.
|
|
304
|
-
// // eslint-disable-next-line
|
|
305
|
-
// var user = result.user
|
|
306
|
-
// }).catch(function (error) {
|
|
307
|
-
// // Handle Errors here.
|
|
308
|
-
// // eslint-disable-next-line
|
|
309
|
-
// var errorCode = error.code
|
|
310
|
-
// // eslint-disable-next-line
|
|
311
|
-
// var errorMessage = error.message
|
|
312
|
-
// // The email of the user's account used.
|
|
313
|
-
// // eslint-disable-next-line
|
|
314
|
-
// var email = error.email
|
|
315
|
-
// // The firebase.auth.AuthCredential type that was used.
|
|
316
|
-
// // eslint-disable-next-line
|
|
317
|
-
// var credential = error.credential
|
|
318
|
-
// // ...
|
|
319
|
-
// })
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
async signOut(correlationId) {
|
|
323
|
-
try {
|
|
324
|
-
// await getAuth().signOut()
|
|
325
|
-
// await this._serviceUser.dispatcher.user.setTokenResult(correlationId, null)
|
|
326
|
-
// await this._serviceUser.dispatcher.user.setClaims(correlationId, null)
|
|
327
|
-
// await this._serviceUser.dispatcher.user.setUser(correlationId, null)
|
|
328
|
-
// await this._serviceUser.dispatcher.user.setLoggedIn(correlationId, false)
|
|
329
|
-
|
|
330
|
-
const list = [];
|
|
331
|
-
list.push(getAuth().signOut());
|
|
332
|
-
// list.push(this._serviceUser.dispatcher.user.setTokenResult(correlationId, null))
|
|
333
|
-
// list.push(this._serviceUser.dispatcher.user.setClaims(correlationId, null))
|
|
334
|
-
// list.push(this._serviceUser.dispatcher.user.setUser(correlationId, null))
|
|
335
|
-
// list.push(this._serviceUser.dispatcher.user.setLoggedIn(correlationId, false))
|
|
336
|
-
list.push(this._serviceUser.resetUser(correlationId));
|
|
337
|
-
list.push(this._serviceUser.setAuthCompleted(correlationId, false));
|
|
338
|
-
|
|
339
|
-
await Promise.all(list);
|
|
340
|
-
|
|
341
|
-
// this._serviceRouter.route('/')
|
|
342
|
-
window.location.href = '/';
|
|
343
|
-
}
|
|
344
|
-
catch (err) {
|
|
345
|
-
this._logger.exception('FirebaseAuthService', 'signOut', err, correlationId);
|
|
346
|
-
}
|
|
347
|
-
}
|
|
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
|
-
|
|
359
|
-
// async token(forceRefresh) {
|
|
360
|
-
// if (!forceRefresh)
|
|
361
|
-
// forceRefresh = false
|
|
362
|
-
|
|
363
|
-
// const user = this.user
|
|
364
|
-
// this._logger.debug('FirebaseAuthService', 'token', 'user', user, correlationId);
|
|
365
|
-
// if (!user)
|
|
366
|
-
// return null
|
|
367
|
-
|
|
368
|
-
// this._logger.debug('FirebaseAuthService', 'token', 'forceRefresh', forceRefresh, correlationId)
|
|
369
|
-
// return this.refreshToken'FirebaseAuthService', 'token', user, forceRefresh)
|
|
370
|
-
// }
|
|
371
|
-
|
|
372
|
-
async updateExternalUser(correlationId, user) {
|
|
373
|
-
// if (this._lock)
|
|
374
|
-
// return
|
|
375
|
-
|
|
376
|
-
try {
|
|
377
|
-
// if (this._lock)
|
|
378
|
-
// return
|
|
379
|
-
|
|
380
|
-
// this._lock = true
|
|
381
|
-
|
|
382
|
-
user = this._convert(correlationId, user);
|
|
383
|
-
if (!user) {
|
|
384
|
-
await this._serviceUser.setUser(correlationId, null);
|
|
385
|
-
await this._serviceUser.setLoggedIn(correlationId, false);
|
|
386
|
-
return;
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
await this.refreshToken(correlationId, user);
|
|
390
|
-
const response = await this._serviceUser.updateExternal(correlationId, user);
|
|
391
|
-
if (this._hasSucceeded(response)) {
|
|
392
|
-
await this._serviceUser.setUser(correlationId, response.results);
|
|
393
|
-
await this._serviceUser.setLoggedIn(correlationId, true);
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
finally {
|
|
397
|
-
// this._lock = false
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
_convert(correlationId, requestedUser) {
|
|
402
|
-
if (requestedUser) {
|
|
403
|
-
const user = {};
|
|
404
|
-
user.id = requestedUser.uid;
|
|
405
|
-
user.name = requestedUser.displayName;
|
|
406
|
-
user.picture = requestedUser.photoURL;
|
|
407
|
-
user.email = requestedUser.email;
|
|
408
|
-
return user;
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
return null;
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
export default FirebaseAuthService;
|
|
1
|
+
import { initializeApp } from 'firebase/app';
|
|
2
|
+
// import { getAuth } from "firebase/auth";
|
|
3
|
+
import { getAnalytics } from "firebase/analytics";
|
|
4
|
+
import { getAuth, signInWithPopup, GoogleAuthProvider } from "firebase/auth";
|
|
5
|
+
|
|
6
|
+
import LibraryClientConstants from '@thzero/library_client/constants';
|
|
7
|
+
|
|
8
|
+
import LibraryCommonUtility from '@thzero/library_common/utility';
|
|
9
|
+
|
|
10
|
+
import UserAuthService from '@thzero/library_client/service/auth/user';
|
|
11
|
+
|
|
12
|
+
class FirebaseAuthService extends UserAuthService {
|
|
13
|
+
constructor() {
|
|
14
|
+
super();
|
|
15
|
+
|
|
16
|
+
// this._lock = false
|
|
17
|
+
this._polling = null;
|
|
18
|
+
|
|
19
|
+
this._serviceRouter = null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async init(injector) {
|
|
23
|
+
await super.init(injector);
|
|
24
|
+
|
|
25
|
+
this._serviceRouter = this._injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_ROUTER);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async deleteUser(correlationId) {
|
|
29
|
+
try {
|
|
30
|
+
const user = await getAuth().currentUser;
|
|
31
|
+
if (!user)
|
|
32
|
+
return;
|
|
33
|
+
|
|
34
|
+
await user.delete();
|
|
35
|
+
await this._serviceUser.resetUser(correlationId);
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
this._logger.exception('FirebaseAuthService', 'deleteUser', err, correlationId);
|
|
39
|
+
throw err;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
get externalUser() {
|
|
44
|
+
const user = getAuth().currentUser;
|
|
45
|
+
this._logger.debug('FirebaseAuthService', 'tokenUser', 'user', user, LibraryCommonUtility.generateId());
|
|
46
|
+
return user;
|
|
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
|
+
|
|
87
|
+
get isAuthenticated() {
|
|
88
|
+
const user = getAuth().currentUser;
|
|
89
|
+
this._logger.debug('FirebaseAuthService', 'isAuthenticated', 'user', user);
|
|
90
|
+
return user !== null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async onAuthStateChanged(user) {
|
|
94
|
+
const correlationId = LibraryCommonUtility.correlationId();
|
|
95
|
+
try {
|
|
96
|
+
await this.updateExternalUser(correlationId, user, true);
|
|
97
|
+
// if (!user)
|
|
98
|
+
// return
|
|
99
|
+
|
|
100
|
+
await this._serviceUser.setAuthCompleted(correlationId, true);
|
|
101
|
+
this._serviceEvent.emit(LibraryClientConstants.EventKeys.Auth.Refresh, user);
|
|
102
|
+
}
|
|
103
|
+
catch (err) {
|
|
104
|
+
this._logger.exception('FirebaseAuthService', 'onAuthStateChanged', err, correlationId);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// try {
|
|
108
|
+
// if (!user) {
|
|
109
|
+
// if (this._polling)
|
|
110
|
+
// clearInterval(this._polling)
|
|
111
|
+
// return
|
|
112
|
+
// }
|
|
113
|
+
|
|
114
|
+
// const self = this
|
|
115
|
+
// this._polling = setInterval(async () => {
|
|
116
|
+
// await self.refreshToken(self.user, true).then()
|
|
117
|
+
// }, 60 * 1000)
|
|
118
|
+
// }
|
|
119
|
+
// catch (err) {
|
|
120
|
+
// this._logger.exception('FirebaseAuthService', 'onAuthStateChanged', err, correlationId)
|
|
121
|
+
// }
|
|
122
|
+
}
|
|
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
|
+
|
|
267
|
+
async signIn(correlationId) {
|
|
268
|
+
if (this.isAuthenticated)
|
|
269
|
+
return false;
|
|
270
|
+
|
|
271
|
+
try {
|
|
272
|
+
const provider = new GoogleAuthProvider();
|
|
273
|
+
const result = await signInWithPopup(getAuth(), provider);
|
|
274
|
+
if (result && result.user) {
|
|
275
|
+
//const credential = GoogleAuthProvider.credentialFromResult(result);
|
|
276
|
+
// const token = credential.accessToken;
|
|
277
|
+
this.updateExternalUser(correlationId, result.user);
|
|
278
|
+
// this._serviceRouter.route('/')
|
|
279
|
+
window.location.href = '/';
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
this.updateExternalUser(correlationId, null);
|
|
284
|
+
}
|
|
285
|
+
catch (err) {
|
|
286
|
+
this._logger.exception('FirebaseAuthService', 'signIn', err, correlationId);
|
|
287
|
+
this.updateExternalUser(correlationId, null);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return true;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
async signInCompleted(correlationId) {
|
|
294
|
+
// if (await auth.isAuthenticated())
|
|
295
|
+
// return
|
|
296
|
+
// getAuth().getRedirectResult().then(function (result) {
|
|
297
|
+
// if (result.credential) {
|
|
298
|
+
// // This gives you a Google Access Token. You can use it to access the Google API.
|
|
299
|
+
// // eslint-disable-next-line
|
|
300
|
+
// var token = result.credential.accessToken
|
|
301
|
+
// // ...
|
|
302
|
+
// }
|
|
303
|
+
// // The signed-in user info.
|
|
304
|
+
// // eslint-disable-next-line
|
|
305
|
+
// var user = result.user
|
|
306
|
+
// }).catch(function (error) {
|
|
307
|
+
// // Handle Errors here.
|
|
308
|
+
// // eslint-disable-next-line
|
|
309
|
+
// var errorCode = error.code
|
|
310
|
+
// // eslint-disable-next-line
|
|
311
|
+
// var errorMessage = error.message
|
|
312
|
+
// // The email of the user's account used.
|
|
313
|
+
// // eslint-disable-next-line
|
|
314
|
+
// var email = error.email
|
|
315
|
+
// // The firebase.auth.AuthCredential type that was used.
|
|
316
|
+
// // eslint-disable-next-line
|
|
317
|
+
// var credential = error.credential
|
|
318
|
+
// // ...
|
|
319
|
+
// })
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
async signOut(correlationId) {
|
|
323
|
+
try {
|
|
324
|
+
// await getAuth().signOut()
|
|
325
|
+
// await this._serviceUser.dispatcher.user.setTokenResult(correlationId, null)
|
|
326
|
+
// await this._serviceUser.dispatcher.user.setClaims(correlationId, null)
|
|
327
|
+
// await this._serviceUser.dispatcher.user.setUser(correlationId, null)
|
|
328
|
+
// await this._serviceUser.dispatcher.user.setLoggedIn(correlationId, false)
|
|
329
|
+
|
|
330
|
+
const list = [];
|
|
331
|
+
list.push(getAuth().signOut());
|
|
332
|
+
// list.push(this._serviceUser.dispatcher.user.setTokenResult(correlationId, null))
|
|
333
|
+
// list.push(this._serviceUser.dispatcher.user.setClaims(correlationId, null))
|
|
334
|
+
// list.push(this._serviceUser.dispatcher.user.setUser(correlationId, null))
|
|
335
|
+
// list.push(this._serviceUser.dispatcher.user.setLoggedIn(correlationId, false))
|
|
336
|
+
list.push(this._serviceUser.resetUser(correlationId));
|
|
337
|
+
list.push(this._serviceUser.setAuthCompleted(correlationId, false));
|
|
338
|
+
|
|
339
|
+
await Promise.all(list);
|
|
340
|
+
|
|
341
|
+
// this._serviceRouter.route('/')
|
|
342
|
+
window.location.href = '/';
|
|
343
|
+
}
|
|
344
|
+
catch (err) {
|
|
345
|
+
this._logger.exception('FirebaseAuthService', 'signOut', err, correlationId);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
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
|
+
|
|
359
|
+
// async token(forceRefresh) {
|
|
360
|
+
// if (!forceRefresh)
|
|
361
|
+
// forceRefresh = false
|
|
362
|
+
|
|
363
|
+
// const user = this.user
|
|
364
|
+
// this._logger.debug('FirebaseAuthService', 'token', 'user', user, correlationId);
|
|
365
|
+
// if (!user)
|
|
366
|
+
// return null
|
|
367
|
+
|
|
368
|
+
// this._logger.debug('FirebaseAuthService', 'token', 'forceRefresh', forceRefresh, correlationId)
|
|
369
|
+
// return this.refreshToken'FirebaseAuthService', 'token', user, forceRefresh)
|
|
370
|
+
// }
|
|
371
|
+
|
|
372
|
+
async updateExternalUser(correlationId, user) {
|
|
373
|
+
// if (this._lock)
|
|
374
|
+
// return
|
|
375
|
+
|
|
376
|
+
try {
|
|
377
|
+
// if (this._lock)
|
|
378
|
+
// return
|
|
379
|
+
|
|
380
|
+
// this._lock = true
|
|
381
|
+
|
|
382
|
+
user = this._convert(correlationId, user);
|
|
383
|
+
if (!user) {
|
|
384
|
+
await this._serviceUser.setUser(correlationId, null);
|
|
385
|
+
await this._serviceUser.setLoggedIn(correlationId, false);
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
await this.refreshToken(correlationId, user);
|
|
390
|
+
const response = await this._serviceUser.updateExternal(correlationId, user);
|
|
391
|
+
if (this._hasSucceeded(response)) {
|
|
392
|
+
await this._serviceUser.setUser(correlationId, response.results);
|
|
393
|
+
await this._serviceUser.setLoggedIn(correlationId, true);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
finally {
|
|
397
|
+
// this._lock = false
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
_convert(correlationId, requestedUser) {
|
|
402
|
+
if (requestedUser) {
|
|
403
|
+
const user = {};
|
|
404
|
+
user.id = requestedUser.uid;
|
|
405
|
+
user.name = requestedUser.displayName;
|
|
406
|
+
user.picture = requestedUser.photoURL;
|
|
407
|
+
user.email = requestedUser.email;
|
|
408
|
+
return user;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
return null;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
export default FirebaseAuthService;
|