@steroidsjs/core 2.2.85 → 2.2.87
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/actions/auth.js
CHANGED
|
@@ -55,7 +55,7 @@ var logout = function (routeId, routeParams) {
|
|
|
55
55
|
if (routeParams === void 0) { routeParams = null; }
|
|
56
56
|
return function (dispatch, getState, _a) {
|
|
57
57
|
var http = _a.http;
|
|
58
|
-
http.
|
|
58
|
+
http.onLogout();
|
|
59
59
|
return dispatch([
|
|
60
60
|
exports.setUser(null),
|
|
61
61
|
routeId && router_1.goToRoute(routeId, routeParams),
|
|
@@ -173,6 +173,9 @@ var HttpComponent = /** @class */ (function () {
|
|
|
173
173
|
HttpComponent.prototype.resetConfig = function () {
|
|
174
174
|
this._axios = null;
|
|
175
175
|
};
|
|
176
|
+
HttpComponent.prototype.onLogout = function () {
|
|
177
|
+
this.removeAccessToken();
|
|
178
|
+
};
|
|
176
179
|
HttpComponent.prototype.getAxiosInstance = function () {
|
|
177
180
|
return __awaiter(this, void 0, void 0, function () {
|
|
178
181
|
var _a, _b, _c;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import BaseHttpComponent from '@steroidsjs/core/components/HttpComponent';
|
|
2
|
+
export default class JwtHttpComponent extends BaseHttpComponent {
|
|
3
|
+
refreshTokenRequest: {
|
|
4
|
+
url: string;
|
|
5
|
+
method: string;
|
|
6
|
+
};
|
|
7
|
+
refreshTokenKey: string;
|
|
8
|
+
_refreshToken: any;
|
|
9
|
+
constructor(components: any, config?: any);
|
|
10
|
+
getAxiosConfig(): Promise<{
|
|
11
|
+
withCredentials: boolean;
|
|
12
|
+
headers: any;
|
|
13
|
+
}>;
|
|
14
|
+
private setRefreshTokenInterceptor;
|
|
15
|
+
getAxiosInstance(): Promise<any>;
|
|
16
|
+
removeRefreshToken(): void;
|
|
17
|
+
setRefreshToken(value: any): void;
|
|
18
|
+
getRefreshToken(): Promise<any>;
|
|
19
|
+
onLogout(): void;
|
|
20
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
18
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
19
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
20
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
21
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
22
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
23
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
27
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
28
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
29
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
30
|
+
function step(op) {
|
|
31
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
32
|
+
while (_) try {
|
|
33
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
34
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
35
|
+
switch (op[0]) {
|
|
36
|
+
case 0: case 1: t = op; break;
|
|
37
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
38
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
39
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
40
|
+
default:
|
|
41
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
42
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
43
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
44
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
45
|
+
if (t[2]) _.ops.pop();
|
|
46
|
+
_.trys.pop(); continue;
|
|
47
|
+
}
|
|
48
|
+
op = body.call(thisArg, _);
|
|
49
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
50
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
54
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
55
|
+
};
|
|
56
|
+
exports.__esModule = true;
|
|
57
|
+
var axios_1 = __importDefault(require("axios"));
|
|
58
|
+
var HttpComponent_1 = __importDefault(require("@steroidsjs/core/components/HttpComponent"));
|
|
59
|
+
var auth_1 = require("@steroidsjs/core/actions/auth");
|
|
60
|
+
var JwtHttpComponent = /** @class */ (function (_super) {
|
|
61
|
+
__extends(JwtHttpComponent, _super);
|
|
62
|
+
function JwtHttpComponent(components, config) {
|
|
63
|
+
if (config === void 0) { config = {}; }
|
|
64
|
+
var _this = _super.call(this, components, config) || this;
|
|
65
|
+
_this.refreshTokenKey = 'refreshToken';
|
|
66
|
+
_this.refreshTokenKey = config.refreshTokenKey || 'refreshToken';
|
|
67
|
+
_this.refreshTokenRequest = config.refreshTokenRequest;
|
|
68
|
+
return _this;
|
|
69
|
+
}
|
|
70
|
+
JwtHttpComponent.prototype.getAxiosConfig = function () {
|
|
71
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
72
|
+
var config, clientStorage, tokenValue, _a, _b;
|
|
73
|
+
return __generator(this, function (_c) {
|
|
74
|
+
switch (_c.label) {
|
|
75
|
+
case 0:
|
|
76
|
+
config = _super.prototype.getAxiosConfig.call(this);
|
|
77
|
+
if (!!this._refreshToken) return [3 /*break*/, 4];
|
|
78
|
+
clientStorage = this._components.clientStorage;
|
|
79
|
+
tokenValue = clientStorage.get(this.refreshTokenKey, clientStorage.STORAGE_COOKIE)
|
|
80
|
+
|| clientStorage.get(this.refreshTokenKey)
|
|
81
|
+
|| null;
|
|
82
|
+
_a = this;
|
|
83
|
+
if (!(tokenValue instanceof Promise)) return [3 /*break*/, 2];
|
|
84
|
+
return [4 /*yield*/, tokenValue];
|
|
85
|
+
case 1:
|
|
86
|
+
_b = _c.sent();
|
|
87
|
+
return [3 /*break*/, 3];
|
|
88
|
+
case 2:
|
|
89
|
+
_b = tokenValue;
|
|
90
|
+
_c.label = 3;
|
|
91
|
+
case 3:
|
|
92
|
+
_a._refreshToken = _b;
|
|
93
|
+
if (this._refreshToken) {
|
|
94
|
+
clientStorage.set(this.refreshTokenKey, this._refreshToken, clientStorage.STORAGE_COOKIE, 180);
|
|
95
|
+
}
|
|
96
|
+
_c.label = 4;
|
|
97
|
+
case 4: return [2 /*return*/, config];
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
JwtHttpComponent.prototype.setRefreshTokenInterceptor = function (axiosInstance) {
|
|
103
|
+
var _this = this;
|
|
104
|
+
axiosInstance.interceptors.response.use(function (config) { return config; }, function (error) { return __awaiter(_this, void 0, void 0, function () {
|
|
105
|
+
var originalRequest, store, response, accessToken;
|
|
106
|
+
var _a;
|
|
107
|
+
var _b;
|
|
108
|
+
return __generator(this, function (_c) {
|
|
109
|
+
switch (_c.label) {
|
|
110
|
+
case 0:
|
|
111
|
+
originalRequest = error.config;
|
|
112
|
+
if (!(error.response.status === 401 && error.config && !error.config._isRetry)) return [3 /*break*/, 2];
|
|
113
|
+
store = this._components.store;
|
|
114
|
+
return [4 /*yield*/, this.send(this.refreshTokenRequest.method, this.refreshTokenRequest.url, (_a = {},
|
|
115
|
+
_a[this.refreshTokenKey] = this._refreshToken,
|
|
116
|
+
_a))];
|
|
117
|
+
case 1:
|
|
118
|
+
response = _c.sent();
|
|
119
|
+
accessToken = (_b = response === null || response === void 0 ? void 0 : response.data) === null || _b === void 0 ? void 0 : _b[this.accessTokenKey];
|
|
120
|
+
if (accessToken) {
|
|
121
|
+
store.dispatch(auth_1.login(accessToken));
|
|
122
|
+
originalRequest._isRetry = true;
|
|
123
|
+
originalRequest.headers.Authorization = 'Bearer ' + accessToken;
|
|
124
|
+
return [2 /*return*/, axiosInstance.request(originalRequest)];
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
store.dispatch(auth_1.logout());
|
|
128
|
+
}
|
|
129
|
+
_c.label = 2;
|
|
130
|
+
case 2: throw error;
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
}); });
|
|
134
|
+
};
|
|
135
|
+
JwtHttpComponent.prototype.getAxiosInstance = function () {
|
|
136
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
137
|
+
var _a, _b, _c;
|
|
138
|
+
return __generator(this, function (_d) {
|
|
139
|
+
switch (_d.label) {
|
|
140
|
+
case 0:
|
|
141
|
+
if (!!this._axios) return [3 /*break*/, 2];
|
|
142
|
+
_a = this;
|
|
143
|
+
_c = (_b = axios_1["default"]).create;
|
|
144
|
+
return [4 /*yield*/, this.getAxiosConfig()];
|
|
145
|
+
case 1:
|
|
146
|
+
_a._axios = _c.apply(_b, [_d.sent()]);
|
|
147
|
+
this.setRefreshTokenInterceptor(this._axios);
|
|
148
|
+
_d.label = 2;
|
|
149
|
+
case 2: return [2 /*return*/, this._axios];
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
};
|
|
154
|
+
JwtHttpComponent.prototype.removeRefreshToken = function () {
|
|
155
|
+
this._refreshToken = null;
|
|
156
|
+
this.resetConfig();
|
|
157
|
+
this._components.clientStorage.remove(this.refreshTokenKey, this._components.clientStorage.STORAGE_COOKIE);
|
|
158
|
+
};
|
|
159
|
+
JwtHttpComponent.prototype.setRefreshToken = function (value) {
|
|
160
|
+
this._refreshToken = value;
|
|
161
|
+
this.resetConfig();
|
|
162
|
+
this._components.clientStorage.set(this.refreshTokenKey, value, this._components.clientStorage.STORAGE_COOKIE, 180);
|
|
163
|
+
};
|
|
164
|
+
JwtHttpComponent.prototype.getRefreshToken = function () {
|
|
165
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
166
|
+
var _a;
|
|
167
|
+
return __generator(this, function (_b) {
|
|
168
|
+
switch (_b.label) {
|
|
169
|
+
case 0:
|
|
170
|
+
if (!(this._refreshToken === false)) return [3 /*break*/, 2];
|
|
171
|
+
_a = this;
|
|
172
|
+
return [4 /*yield*/, this._components.clientStorage.get(this.refreshTokenKey)];
|
|
173
|
+
case 1:
|
|
174
|
+
_a._refreshToken = (_b.sent()) || null;
|
|
175
|
+
_b.label = 2;
|
|
176
|
+
case 2: return [2 /*return*/, this._refreshToken];
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
JwtHttpComponent.prototype.onLogout = function () {
|
|
182
|
+
this.removeAccessToken();
|
|
183
|
+
this.removeRefreshToken();
|
|
184
|
+
};
|
|
185
|
+
return JwtHttpComponent;
|
|
186
|
+
}(HttpComponent_1["default"]));
|
|
187
|
+
exports["default"] = JwtHttpComponent;
|
|
@@ -8,6 +8,7 @@ import { IComponents } from '../providers/ComponentsProvider';
|
|
|
8
8
|
import { IRouteItem } from '../ui/nav/Router/Router';
|
|
9
9
|
import MetricsComponent from '../components/MetricsComponent';
|
|
10
10
|
import { IScreenProviderProps } from '../providers/ScreenProvider';
|
|
11
|
+
import { IFetchConfig } from '../hooks/useFetch';
|
|
11
12
|
/**
|
|
12
13
|
* Application HOC
|
|
13
14
|
* Обертка над корневым компонентом приложения, используется только в `Application.tsx`. Добавляет через React Context
|
|
@@ -27,6 +28,10 @@ export interface IApplicationHookConfig {
|
|
|
27
28
|
layoutView?: () => CustomView;
|
|
28
29
|
layoutProps?: Record<string, unknown>;
|
|
29
30
|
screen?: boolean | Omit<IScreenProviderProps, 'children'>;
|
|
31
|
+
/**
|
|
32
|
+
* These fetch configurations will be used to preload and store for no matter what route matched in case of SSR
|
|
33
|
+
*/
|
|
34
|
+
defaultFetches?: IFetchConfig[];
|
|
30
35
|
}
|
|
31
36
|
export interface IApplicationHookResult {
|
|
32
37
|
renderApplication: (children?: any) => JSX.Element;
|