@osimatic/helpers-js 1.0.92 → 1.0.94

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.
Files changed (2) hide show
  1. package/http_client.js +47 -2
  2. package/package.json +1 -2
package/http_client.js CHANGED
@@ -3,11 +3,17 @@ class HTTPClient {
3
3
  static setRefreshTokenUrl(url) {
4
4
  HTTPClient.refreshTokenUrl = url;
5
5
  }
6
-
7
6
  static setRefreshTokenCallback(callback) {
8
7
  HTTPClient.refreshTokenCallback = callback;
9
8
  }
10
9
 
10
+ static setOnInvalidTokenRedirectUrl(url) {
11
+ HTTPClient.onInvalidTokenRedirectUrl = url;
12
+ }
13
+ static setOnInvalidTokenCallback(callback) {
14
+ HTTPClient.onInvalidTokenCallback = callback;
15
+ }
16
+
11
17
  static getHeaders(asObject) {
12
18
  HTTPClient.setAuthorizationToken(JwtSession.getToken());
13
19
 
@@ -48,6 +54,10 @@ class HTTPClient {
48
54
  HTTPClient.headers = {};
49
55
  }
50
56
 
57
+ if (typeof authorizationToken == 'undefined' || null == authorizationToken) {
58
+ return;
59
+ }
60
+
51
61
  HTTPClient.headers['Authorization'] = 'Bearer ' + authorizationToken;
52
62
  }
53
63
 
@@ -126,12 +136,37 @@ class HTTPClient {
126
136
  response.statusText === 'Expired JWT Token'
127
137
  || (typeof json['message'] != 'undefined' && json['message'] === 'Expired JWT Token')
128
138
  || (typeof json['error'] != 'undefined' && json['error'] === 'expired_token')
129
- || (typeof json['error'] != 'undefined' && json['error'] === 'authentification_failure')
130
139
  || (json === 'expired_token')
140
+ );
141
+ }
142
+
143
+ static isInvalidToken(response, json) {
144
+ if (response.status !== 401) {
145
+ return false;
146
+ }
147
+
148
+ return (
149
+ response.statusText === 'Invalid JWT Token'
150
+ || (typeof json['message'] != 'undefined' && json['message'] === 'Invalid JWT Token')
151
+ || (typeof json['error'] != 'undefined' && json['error'] === 'invalid_token')
152
+ || (typeof json['error'] != 'undefined' && json['error'] === 'authentification_failure')
153
+ || (json === 'invalid_token')
131
154
  || (json === 'authentification_failure')
132
155
  );
133
156
  }
134
157
 
158
+ static onInvalidToken() {
159
+ if (typeof HTTPClient.onInvalidTokenCallback == 'function') {
160
+ HTTPClient.onInvalidTokenCallback();
161
+ return;
162
+ }
163
+
164
+ JwtSession.logout();
165
+ if (typeof HTTPClient.onInvalidTokenRedirectUrl != 'undefined') {
166
+ window.location.href = HTTPClient.onInvalidTokenRedirectUrl;
167
+ }
168
+ }
169
+
135
170
  static async request(method, url, data, successCallback, errorCallback, formErrorCallback) {
136
171
  method = method.toUpperCase();
137
172
 
@@ -169,6 +204,11 @@ class HTTPClient {
169
204
  return;
170
205
  }
171
206
 
207
+ if (HTTPClient.isInvalidToken(response, jsonData)) {
208
+ HTTPClient.onInvalidToken();
209
+ return;
210
+ }
211
+
172
212
  if (response.ok) {
173
213
  if (typeof successCallback != 'undefined' && successCallback != null) {
174
214
  successCallback(jsonData, response);
@@ -225,6 +265,11 @@ class HTTPClient {
225
265
  return;
226
266
  }
227
267
 
268
+ if (response.status === 401 && response.statusText === 'Invalid JWT Token') {
269
+ HTTPClient.onInvalidToken();
270
+ return;
271
+ }
272
+
228
273
  if (response.ok) {
229
274
  const blobData = await response.blob();
230
275
  File.download(blobData, response.headers.get('content-type'), response.headers.get('content-disposition'));
package/package.json CHANGED
@@ -1,7 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.0.92",
4
- "main": "index.js",
3
+ "version": "1.0.94",
5
4
  "scripts": {
6
5
  "test": "echo \"Error: no test specified\" && exit 1"
7
6
  },