@etsoo/react 1.3.38 → 1.3.39

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.
@@ -1,8 +1,9 @@
1
- import { IApi } from '@etsoo/appscript';
2
- import { ISmartERPUser } from '..';
1
+ import { IActionResult, IApi } from '@etsoo/appscript';
2
+ import { ApiDataError } from '@etsoo/restclient';
3
3
  import { IServiceAppSettings } from './IServiceAppSettings';
4
4
  import { IServicePageData } from './IServicePage';
5
5
  import { IServiceUser } from './IServiceUser';
6
+ import { ISmartERPUser } from './ISmartERPUser';
6
7
  import { ReactApp } from './ReactApp';
7
8
  /**
8
9
  * Core Service App
@@ -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
+ * Try login result processing
35
+ * @param result Result
36
+ */
37
+ protected tryLoginResult(result: string | ApiDataError | IActionResult): void;
38
+ /**
39
+ * Try login
40
+ */
41
+ tryLogin(): Promise<boolean>;
32
42
  /**
33
43
  * User login extended
34
44
  * @param user New user
@@ -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
@@ -39,6 +40,74 @@ export class ServiceApp extends ReactApp {
39
40
  '=' +
40
41
  this.culture;
41
42
  }
43
+ /**
44
+ * Try login result processing
45
+ * @param result Result
46
+ */
47
+ tryLoginResult(result) { }
48
+ /**
49
+ * Try login
50
+ */
51
+ async tryLogin() {
52
+ // Token
53
+ const token = this.getCacheToken();
54
+ if (token == null || token === '') {
55
+ this.tryLoginResult('');
56
+ return false;
57
+ }
58
+ // Reqest data
59
+ const data = {
60
+ timezone: this.getTimeZone()
61
+ };
62
+ // Payload
63
+ const payload = {
64
+ config: { headers: { [CoreConstants.TokenHeaderRefresh]: token } },
65
+ onError: (error) => {
66
+ this.tryLoginResult(error);
67
+ // Prevent further processing
68
+ return false;
69
+ }
70
+ };
71
+ // Call API
72
+ const result = await this.coreApi.put('Auth/RefreshToken', data, payload);
73
+ if (result == null)
74
+ return false;
75
+ if (!result.ok) {
76
+ this.tryLoginResult(result);
77
+ return false;
78
+ }
79
+ // Token
80
+ const refreshToken = this.getResponseToken(payload.response);
81
+ if (refreshToken == null || result.data == null) {
82
+ this.tryLoginResult(this.get('noData'));
83
+ return false;
84
+ }
85
+ // User data
86
+ const userData = result.data;
87
+ // Use core system access token to service api to exchange service access token
88
+ const serviceResult = await this.api.put('Auth/ExchangeToken', {
89
+ token: userData.token
90
+ }, {
91
+ onError: (error) => {
92
+ // Set message
93
+ this.tryLoginResult(error);
94
+ // Prevent further processing
95
+ return false;
96
+ }
97
+ });
98
+ if (serviceResult == null)
99
+ return false;
100
+ if (!serviceResult.ok) {
101
+ this.tryLoginResult(result);
102
+ return false;
103
+ }
104
+ if (serviceResult.data == null) {
105
+ this.tryLoginResult(this.get('noData'));
106
+ return false;
107
+ }
108
+ this.userLoginEx(serviceResult.data, refreshToken, userData);
109
+ return true;
110
+ }
42
111
  /**
43
112
  * User login extended
44
113
  * @param user New user
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/react",
3
- "version": "1.3.38",
3
+ "version": "1.3.39",
4
4
  "description": "TypeScript ReactJs framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -1,10 +1,18 @@
1
- import { createClient, IApi } from '@etsoo/appscript';
1
+ import {
2
+ createClient,
3
+ IActionResult,
4
+ IApi,
5
+ IApiPayload
6
+ } from '@etsoo/appscript';
7
+ import { ApiDataError } from '@etsoo/restclient';
2
8
  import { DomUtils } from '@etsoo/shared';
3
- import { ISmartERPUser } from '..';
9
+ import { CoreConstants } from './CoreConstants';
4
10
  import { IServiceAppSettings } from './IServiceAppSettings';
5
11
  import { IServicePageData } from './IServicePage';
6
- import { IServiceUser } from './IServiceUser';
12
+ import { IServiceUser, ServiceLoginResult } from './IServiceUser';
13
+ import { ISmartERPUser, SmartERPLoginResult } from './ISmartERPUser';
7
14
  import { ReactApp } from './ReactApp';
15
+ import { RefreshTokenRQ } from './RefreshTokenRQ';
8
16
 
9
17
  /**
10
18
  * Core Service App
@@ -63,6 +71,96 @@ export class ServiceApp<
63
71
  this.culture;
64
72
  }
65
73
 
74
+ /**
75
+ * Try login result processing
76
+ * @param result Result
77
+ */
78
+ protected tryLoginResult(result: string | ApiDataError | IActionResult) {}
79
+
80
+ /**
81
+ * Try login
82
+ */
83
+ override async tryLogin() {
84
+ // Token
85
+ const token = this.getCacheToken();
86
+ if (token == null || token === '') {
87
+ this.tryLoginResult('');
88
+ return false;
89
+ }
90
+
91
+ // Reqest data
92
+ const data: RefreshTokenRQ = {
93
+ timezone: this.getTimeZone()
94
+ };
95
+
96
+ // Payload
97
+ const payload: IApiPayload<SmartERPLoginResult, any> = {
98
+ config: { headers: { [CoreConstants.TokenHeaderRefresh]: token } },
99
+ onError: (error) => {
100
+ this.tryLoginResult(error);
101
+
102
+ // Prevent further processing
103
+ return false;
104
+ }
105
+ };
106
+
107
+ // Call API
108
+ const result = await this.coreApi.put<SmartERPLoginResult>(
109
+ 'Auth/RefreshToken',
110
+ data,
111
+ payload
112
+ );
113
+ if (result == null) return false;
114
+
115
+ if (!result.ok) {
116
+ this.tryLoginResult(result);
117
+ return false;
118
+ }
119
+
120
+ // Token
121
+ const refreshToken = this.getResponseToken(payload.response);
122
+ if (refreshToken == null || result.data == null) {
123
+ this.tryLoginResult(this.get('noData')!);
124
+ return false;
125
+ }
126
+
127
+ // User data
128
+ const userData = result.data;
129
+
130
+ // Use core system access token to service api to exchange service access token
131
+ const serviceResult = await this.api.put<ServiceLoginResult>(
132
+ 'Auth/ExchangeToken',
133
+ {
134
+ token: userData.token
135
+ },
136
+ {
137
+ onError: (error) => {
138
+ // Set message
139
+ this.tryLoginResult(error);
140
+
141
+ // Prevent further processing
142
+ return false;
143
+ }
144
+ }
145
+ );
146
+
147
+ if (serviceResult == null) return false;
148
+
149
+ if (!serviceResult.ok) {
150
+ this.tryLoginResult(result);
151
+ return false;
152
+ }
153
+
154
+ if (serviceResult.data == null) {
155
+ this.tryLoginResult(this.get('noData')!);
156
+ return false;
157
+ }
158
+
159
+ this.userLoginEx(serviceResult.data, refreshToken, userData);
160
+
161
+ return true;
162
+ }
163
+
66
164
  /**
67
165
  * User login extended
68
166
  * @param user New user