@etsoo/react 1.3.38 → 1.3.42
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/lib/app/RefreshTokenRQ.d.ts +4 -0
- package/lib/app/ServiceApp.d.ts +12 -2
- package/lib/app/ServiceApp.js +93 -1
- package/package.json +3 -3
- package/src/app/RefreshTokenRQ.ts +5 -0
- package/src/app/ServiceApp.ts +120 -4
package/lib/app/ServiceApp.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { IApi } from '@etsoo/appscript';
|
|
2
|
-
import { ISmartERPUser } from '..';
|
|
1
|
+
import { IApi, RefreshTokenProps } from '@etsoo/appscript';
|
|
3
2
|
import { IServiceAppSettings } from './IServiceAppSettings';
|
|
4
3
|
import { IServicePageData } from './IServicePage';
|
|
5
4
|
import { IServiceUser } from './IServiceUser';
|
|
5
|
+
import { ISmartERPUser } from './ISmartERPUser';
|
|
6
6
|
import { ReactApp } from './ReactApp';
|
|
7
|
+
import { RefreshTokenRQ } from './RefreshTokenRQ';
|
|
7
8
|
/**
|
|
8
9
|
* Core Service App
|
|
9
10
|
* Service login to core system, get the refesh token and access token
|
|
@@ -29,6 +30,15 @@ export declare class ServiceApp<P extends IServicePageData = IServicePageData, U
|
|
|
29
30
|
* Go to the login page
|
|
30
31
|
*/
|
|
31
32
|
toLoginPage(): void;
|
|
33
|
+
/**
|
|
34
|
+
* Refresh token
|
|
35
|
+
* @param props Props
|
|
36
|
+
*/
|
|
37
|
+
refreshToken<D = RefreshTokenRQ>(props?: RefreshTokenProps<D>): Promise<boolean>;
|
|
38
|
+
/**
|
|
39
|
+
* Try login
|
|
40
|
+
*/
|
|
41
|
+
tryLogin(): Promise<boolean>;
|
|
32
42
|
/**
|
|
33
43
|
* User login extended
|
|
34
44
|
* @param user New user
|
package/lib/app/ServiceApp.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createClient } from '@etsoo/appscript';
|
|
2
2
|
import { DomUtils } from '@etsoo/shared';
|
|
3
|
+
import { CoreConstants } from './CoreConstants';
|
|
3
4
|
import { ReactApp } from './ReactApp';
|
|
4
5
|
/**
|
|
5
6
|
* Core Service App
|
|
@@ -29,7 +30,7 @@ export class ServiceApp extends ReactApp {
|
|
|
29
30
|
* Go to the login page
|
|
30
31
|
*/
|
|
31
32
|
toLoginPage() {
|
|
32
|
-
const coreUrl = this.settings.
|
|
33
|
+
const coreUrl = this.settings.coreUrl;
|
|
33
34
|
window.location.href =
|
|
34
35
|
coreUrl +
|
|
35
36
|
'?serviceId=' +
|
|
@@ -39,6 +40,97 @@ export class ServiceApp extends ReactApp {
|
|
|
39
40
|
'=' +
|
|
40
41
|
this.culture;
|
|
41
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Refresh token
|
|
45
|
+
* @param props Props
|
|
46
|
+
*/
|
|
47
|
+
async refreshToken(props) {
|
|
48
|
+
// Destruct
|
|
49
|
+
const { callback, data, showLoading = false } = props !== null && props !== void 0 ? props : {};
|
|
50
|
+
// Token
|
|
51
|
+
const token = this.getCacheToken();
|
|
52
|
+
if (token == null || token === '') {
|
|
53
|
+
if (callback)
|
|
54
|
+
callback(false);
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
// Reqest data
|
|
58
|
+
// Merge additional data passed
|
|
59
|
+
const rq = {
|
|
60
|
+
timezone: this.getTimeZone(),
|
|
61
|
+
...data
|
|
62
|
+
};
|
|
63
|
+
// Payload
|
|
64
|
+
const payload = {
|
|
65
|
+
showLoading,
|
|
66
|
+
config: { headers: { [CoreConstants.TokenHeaderRefresh]: token } },
|
|
67
|
+
onError: (error) => {
|
|
68
|
+
if (callback)
|
|
69
|
+
callback(error);
|
|
70
|
+
// Prevent further processing
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
// Call API
|
|
75
|
+
const result = await this.coreApi.put('Auth/RefreshToken', rq, payload);
|
|
76
|
+
if (result == null)
|
|
77
|
+
return false;
|
|
78
|
+
if (!result.ok) {
|
|
79
|
+
if (callback)
|
|
80
|
+
callback(result);
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
// Token
|
|
84
|
+
const refreshToken = this.getResponseToken(payload.response);
|
|
85
|
+
if (refreshToken == null || result.data == null) {
|
|
86
|
+
if (callback)
|
|
87
|
+
callback(this.get('noData'));
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
// User data
|
|
91
|
+
const userData = result.data;
|
|
92
|
+
// Use core system access token to service api to exchange service access token
|
|
93
|
+
const serviceResult = await this.api.put('Auth/ExchangeToken', {
|
|
94
|
+
token: userData.token
|
|
95
|
+
}, {
|
|
96
|
+
showLoading,
|
|
97
|
+
onError: (error) => {
|
|
98
|
+
if (callback)
|
|
99
|
+
callback(error);
|
|
100
|
+
// Prevent further processing
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
if (serviceResult == null)
|
|
105
|
+
return false;
|
|
106
|
+
if (!serviceResult.ok) {
|
|
107
|
+
if (callback)
|
|
108
|
+
callback(serviceResult);
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
if (serviceResult.data == null) {
|
|
112
|
+
if (callback)
|
|
113
|
+
callback(this.get('noData'));
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
this.userLoginEx(serviceResult.data, refreshToken, userData);
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Try login
|
|
121
|
+
*/
|
|
122
|
+
async tryLogin() {
|
|
123
|
+
// Reset user state
|
|
124
|
+
const result = await super.tryLogin();
|
|
125
|
+
if (!result)
|
|
126
|
+
return false;
|
|
127
|
+
// Refresh token
|
|
128
|
+
return await this.refreshToken({
|
|
129
|
+
callback: (_result) => {
|
|
130
|
+
this.toLoginPage();
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
}
|
|
42
134
|
/**
|
|
43
135
|
* User login extended
|
|
44
136
|
* @param user New user
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etsoo/react",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.42",
|
|
4
4
|
"description": "TypeScript ReactJs framework",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"@emotion/react": "^11.7.0",
|
|
51
51
|
"@emotion/style": "^0.8.0",
|
|
52
52
|
"@emotion/styled": "^11.6.0",
|
|
53
|
-
"@etsoo/appscript": "^1.1.
|
|
53
|
+
"@etsoo/appscript": "^1.1.56",
|
|
54
54
|
"@etsoo/notificationbase": "^1.0.94",
|
|
55
55
|
"@etsoo/shared": "^1.0.75",
|
|
56
56
|
"@mui/icons-material": "^5.2.0",
|
|
@@ -83,7 +83,7 @@
|
|
|
83
83
|
"@types/react-test-renderer": "^17.0.1",
|
|
84
84
|
"@typescript-eslint/eslint-plugin": "^5.5.0",
|
|
85
85
|
"@typescript-eslint/parser": "^5.5.0",
|
|
86
|
-
"eslint": "^8.
|
|
86
|
+
"eslint": "^8.4.0",
|
|
87
87
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
88
88
|
"eslint-plugin-import": "^2.25.3",
|
|
89
89
|
"eslint-plugin-react": "^7.27.1",
|
package/src/app/ServiceApp.ts
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
createClient,
|
|
3
|
+
IApi,
|
|
4
|
+
IApiPayload,
|
|
5
|
+
RefreshTokenProps
|
|
6
|
+
} from '@etsoo/appscript';
|
|
2
7
|
import { DomUtils } from '@etsoo/shared';
|
|
3
|
-
import {
|
|
8
|
+
import { CoreConstants } from './CoreConstants';
|
|
4
9
|
import { IServiceAppSettings } from './IServiceAppSettings';
|
|
5
10
|
import { IServicePageData } from './IServicePage';
|
|
6
|
-
import { IServiceUser } from './IServiceUser';
|
|
11
|
+
import { IServiceUser, ServiceLoginResult } from './IServiceUser';
|
|
12
|
+
import { ISmartERPUser, SmartERPLoginResult } from './ISmartERPUser';
|
|
7
13
|
import { ReactApp } from './ReactApp';
|
|
14
|
+
import { RefreshTokenRQ } from './RefreshTokenRQ';
|
|
8
15
|
|
|
9
16
|
/**
|
|
10
17
|
* Core Service App
|
|
@@ -52,7 +59,7 @@ export class ServiceApp<
|
|
|
52
59
|
* Go to the login page
|
|
53
60
|
*/
|
|
54
61
|
override toLoginPage() {
|
|
55
|
-
const coreUrl = this.settings.
|
|
62
|
+
const coreUrl = this.settings.coreUrl;
|
|
56
63
|
window.location.href =
|
|
57
64
|
coreUrl +
|
|
58
65
|
'?serviceId=' +
|
|
@@ -63,6 +70,115 @@ export class ServiceApp<
|
|
|
63
70
|
this.culture;
|
|
64
71
|
}
|
|
65
72
|
|
|
73
|
+
/**
|
|
74
|
+
* Refresh token
|
|
75
|
+
* @param props Props
|
|
76
|
+
*/
|
|
77
|
+
override async refreshToken<D = RefreshTokenRQ>(
|
|
78
|
+
props?: RefreshTokenProps<D>
|
|
79
|
+
) {
|
|
80
|
+
// Destruct
|
|
81
|
+
const { callback, data, showLoading = false } = props ?? {};
|
|
82
|
+
|
|
83
|
+
// Token
|
|
84
|
+
const token = this.getCacheToken();
|
|
85
|
+
if (token == null || token === '') {
|
|
86
|
+
if (callback) callback(false);
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Reqest data
|
|
91
|
+
// Merge additional data passed
|
|
92
|
+
const rq: RefreshTokenRQ = {
|
|
93
|
+
timezone: this.getTimeZone(),
|
|
94
|
+
...data
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// Payload
|
|
98
|
+
const payload: IApiPayload<SmartERPLoginResult, any> = {
|
|
99
|
+
showLoading,
|
|
100
|
+
config: { headers: { [CoreConstants.TokenHeaderRefresh]: token } },
|
|
101
|
+
onError: (error) => {
|
|
102
|
+
if (callback) callback(error);
|
|
103
|
+
|
|
104
|
+
// Prevent further processing
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// Call API
|
|
110
|
+
const result = await this.coreApi.put<SmartERPLoginResult>(
|
|
111
|
+
'Auth/RefreshToken',
|
|
112
|
+
rq,
|
|
113
|
+
payload
|
|
114
|
+
);
|
|
115
|
+
if (result == null) return false;
|
|
116
|
+
|
|
117
|
+
if (!result.ok) {
|
|
118
|
+
if (callback) callback(result);
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Token
|
|
123
|
+
const refreshToken = this.getResponseToken(payload.response);
|
|
124
|
+
if (refreshToken == null || result.data == null) {
|
|
125
|
+
if (callback) callback(this.get('noData')!);
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// User data
|
|
130
|
+
const userData = result.data;
|
|
131
|
+
|
|
132
|
+
// Use core system access token to service api to exchange service access token
|
|
133
|
+
const serviceResult = await this.api.put<ServiceLoginResult>(
|
|
134
|
+
'Auth/ExchangeToken',
|
|
135
|
+
{
|
|
136
|
+
token: userData.token
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
showLoading,
|
|
140
|
+
onError: (error) => {
|
|
141
|
+
if (callback) callback(error);
|
|
142
|
+
|
|
143
|
+
// Prevent further processing
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
if (serviceResult == null) return false;
|
|
150
|
+
|
|
151
|
+
if (!serviceResult.ok) {
|
|
152
|
+
if (callback) callback(serviceResult);
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (serviceResult.data == null) {
|
|
157
|
+
if (callback) callback(this.get('noData')!);
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
this.userLoginEx(serviceResult.data, refreshToken, userData);
|
|
162
|
+
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Try login
|
|
168
|
+
*/
|
|
169
|
+
override async tryLogin() {
|
|
170
|
+
// Reset user state
|
|
171
|
+
const result = await super.tryLogin();
|
|
172
|
+
if (!result) return false;
|
|
173
|
+
|
|
174
|
+
// Refresh token
|
|
175
|
+
return await this.refreshToken({
|
|
176
|
+
callback: (_result) => {
|
|
177
|
+
this.toLoginPage();
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
66
182
|
/**
|
|
67
183
|
* User login extended
|
|
68
184
|
* @param user New user
|