dauth-context-react 2.1.0 → 2.3.0
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 +15 -11
- package/dist/{interfaces.d.ts → index.d.mts} +65 -48
- package/dist/index.d.ts +65 -8
- package/dist/index.js +730 -5
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +715 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +26 -20
- package/src/api/dauth.api.ts +20 -0
- package/src/api/interfaces/dauth.api.responses.ts +8 -0
- package/src/index.tsx +67 -32
- package/src/initialDauthState.ts +1 -0
- package/src/interfaces.ts +16 -0
- package/src/reducer/dauth.actions.ts +99 -60
- package/dist/api/dauth.api.d.ts +0 -10
- package/dist/api/interfaces/dauth.api.responses.d.ts +0 -38
- package/dist/api/utils/config.d.ts +0 -4
- package/dist/api/utils/routes.d.ts +0 -4
- package/dist/constants.d.ts +0 -2
- package/dist/dauth-context-react.cjs.development.js +0 -1118
- package/dist/dauth-context-react.cjs.development.js.map +0 -1
- package/dist/dauth-context-react.cjs.production.min.js +0 -2
- package/dist/dauth-context-react.cjs.production.min.js.map +0 -1
- package/dist/dauth-context-react.esm.js +0 -1110
- package/dist/dauth-context-react.esm.js.map +0 -1
- package/dist/initialDauthState.d.ts +0 -3
- package/dist/reducer/dauth.actions.d.ts +0 -41
- package/dist/reducer/dauth.reducer.d.ts +0 -2
- package/dist/reducer/dauth.types.d.ts +0 -5
|
@@ -1,1110 +0,0 @@
|
|
|
1
|
-
import React, { useReducer, useRef, useCallback, useEffect, useMemo, createContext, useContext } from 'react';
|
|
2
|
-
|
|
3
|
-
function asyncGeneratorStep(n, t, e, r, o, a, c) {
|
|
4
|
-
try {
|
|
5
|
-
var i = n[a](c),
|
|
6
|
-
u = i.value;
|
|
7
|
-
} catch (n) {
|
|
8
|
-
return void e(n);
|
|
9
|
-
}
|
|
10
|
-
i.done ? t(u) : Promise.resolve(u).then(r, o);
|
|
11
|
-
}
|
|
12
|
-
function _asyncToGenerator(n) {
|
|
13
|
-
return function () {
|
|
14
|
-
var t = this,
|
|
15
|
-
e = arguments;
|
|
16
|
-
return new Promise(function (r, o) {
|
|
17
|
-
var a = n.apply(t, e);
|
|
18
|
-
function _next(n) {
|
|
19
|
-
asyncGeneratorStep(a, r, o, _next, _throw, "next", n);
|
|
20
|
-
}
|
|
21
|
-
function _throw(n) {
|
|
22
|
-
asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);
|
|
23
|
-
}
|
|
24
|
-
_next(void 0);
|
|
25
|
-
});
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
function _extends() {
|
|
29
|
-
return _extends = Object.assign ? Object.assign.bind() : function (n) {
|
|
30
|
-
for (var e = 1; e < arguments.length; e++) {
|
|
31
|
-
var t = arguments[e];
|
|
32
|
-
for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
|
|
33
|
-
}
|
|
34
|
-
return n;
|
|
35
|
-
}, _extends.apply(null, arguments);
|
|
36
|
-
}
|
|
37
|
-
function _regenerator() {
|
|
38
|
-
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */
|
|
39
|
-
var e,
|
|
40
|
-
t,
|
|
41
|
-
r = "function" == typeof Symbol ? Symbol : {},
|
|
42
|
-
n = r.iterator || "@@iterator",
|
|
43
|
-
o = r.toStringTag || "@@toStringTag";
|
|
44
|
-
function i(r, n, o, i) {
|
|
45
|
-
var c = n && n.prototype instanceof Generator ? n : Generator,
|
|
46
|
-
u = Object.create(c.prototype);
|
|
47
|
-
return _regeneratorDefine(u, "_invoke", function (r, n, o) {
|
|
48
|
-
var i,
|
|
49
|
-
c,
|
|
50
|
-
u,
|
|
51
|
-
f = 0,
|
|
52
|
-
p = o || [],
|
|
53
|
-
y = !1,
|
|
54
|
-
G = {
|
|
55
|
-
p: 0,
|
|
56
|
-
n: 0,
|
|
57
|
-
v: e,
|
|
58
|
-
a: d,
|
|
59
|
-
f: d.bind(e, 4),
|
|
60
|
-
d: function (t, r) {
|
|
61
|
-
return i = t, c = 0, u = e, G.n = r, a;
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
function d(r, n) {
|
|
65
|
-
for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) {
|
|
66
|
-
var o,
|
|
67
|
-
i = p[t],
|
|
68
|
-
d = G.p,
|
|
69
|
-
l = i[2];
|
|
70
|
-
r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0));
|
|
71
|
-
}
|
|
72
|
-
if (o || r > 1) return a;
|
|
73
|
-
throw y = !0, n;
|
|
74
|
-
}
|
|
75
|
-
return function (o, p, l) {
|
|
76
|
-
if (f > 1) throw TypeError("Generator is already running");
|
|
77
|
-
for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) {
|
|
78
|
-
i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u);
|
|
79
|
-
try {
|
|
80
|
-
if (f = 2, i) {
|
|
81
|
-
if (c || (o = "next"), t = i[o]) {
|
|
82
|
-
if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object");
|
|
83
|
-
if (!t.done) return t;
|
|
84
|
-
u = t.value, c < 2 && (c = 0);
|
|
85
|
-
} else 1 === c && (t = i.return) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1);
|
|
86
|
-
i = e;
|
|
87
|
-
} else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break;
|
|
88
|
-
} catch (t) {
|
|
89
|
-
i = e, c = 1, u = t;
|
|
90
|
-
} finally {
|
|
91
|
-
f = 1;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
return {
|
|
95
|
-
value: t,
|
|
96
|
-
done: y
|
|
97
|
-
};
|
|
98
|
-
};
|
|
99
|
-
}(r, o, i), !0), u;
|
|
100
|
-
}
|
|
101
|
-
var a = {};
|
|
102
|
-
function Generator() {}
|
|
103
|
-
function GeneratorFunction() {}
|
|
104
|
-
function GeneratorFunctionPrototype() {}
|
|
105
|
-
t = Object.getPrototypeOf;
|
|
106
|
-
var c = [][n] ? t(t([][n]())) : (_regeneratorDefine(t = {}, n, function () {
|
|
107
|
-
return this;
|
|
108
|
-
}), t),
|
|
109
|
-
u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c);
|
|
110
|
-
function f(e) {
|
|
111
|
-
return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e;
|
|
112
|
-
}
|
|
113
|
-
return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine(u), _regeneratorDefine(u, o, "Generator"), _regeneratorDefine(u, n, function () {
|
|
114
|
-
return this;
|
|
115
|
-
}), _regeneratorDefine(u, "toString", function () {
|
|
116
|
-
return "[object Generator]";
|
|
117
|
-
}), (_regenerator = function () {
|
|
118
|
-
return {
|
|
119
|
-
w: i,
|
|
120
|
-
m: f
|
|
121
|
-
};
|
|
122
|
-
})();
|
|
123
|
-
}
|
|
124
|
-
function _regeneratorDefine(e, r, n, t) {
|
|
125
|
-
var i = Object.defineProperty;
|
|
126
|
-
try {
|
|
127
|
-
i({}, "", {});
|
|
128
|
-
} catch (e) {
|
|
129
|
-
i = 0;
|
|
130
|
-
}
|
|
131
|
-
_regeneratorDefine = function (e, r, n, t) {
|
|
132
|
-
function o(r, n) {
|
|
133
|
-
_regeneratorDefine(e, r, function (e) {
|
|
134
|
-
return this._invoke(r, n, e);
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
r ? i ? i(e, r, {
|
|
138
|
-
value: n,
|
|
139
|
-
enumerable: !t,
|
|
140
|
-
configurable: !t,
|
|
141
|
-
writable: !t
|
|
142
|
-
}) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2));
|
|
143
|
-
}, _regeneratorDefine(e, r, n, t);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
var initialDauthState = {
|
|
147
|
-
user: {
|
|
148
|
-
language: (typeof window !== 'undefined' ? /*#__PURE__*/window.document.documentElement.getAttribute('lang') : null) || 'es'
|
|
149
|
-
},
|
|
150
|
-
domain: {},
|
|
151
|
-
isLoading: true,
|
|
152
|
-
isAuthenticated: false,
|
|
153
|
-
loginWithRedirect: function loginWithRedirect() {},
|
|
154
|
-
logout: function logout() {},
|
|
155
|
-
getAccessToken: function getAccessToken() {
|
|
156
|
-
return Promise.resolve('');
|
|
157
|
-
},
|
|
158
|
-
updateUser: function updateUser() {
|
|
159
|
-
return Promise.resolve(false);
|
|
160
|
-
},
|
|
161
|
-
updateUserWithRedirect: function updateUserWithRedirect() {},
|
|
162
|
-
// Send email verification
|
|
163
|
-
sendEmailVerificationStatus: {
|
|
164
|
-
status: {
|
|
165
|
-
type: 'info',
|
|
166
|
-
message: 'Sending email verification...'
|
|
167
|
-
},
|
|
168
|
-
isLoading: false
|
|
169
|
-
},
|
|
170
|
-
sendEmailVerification: function sendEmailVerification() {
|
|
171
|
-
return Promise.resolve(false);
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
var LOGIN = 'LOGIN';
|
|
176
|
-
var SET_IS_LOADING = 'SET_IS_LOADING';
|
|
177
|
-
var UPDATE_USER = 'UPDATE_USER';
|
|
178
|
-
var SET_SEND_EMAIL_VERIFICATION_IS_LOADING = 'SET_SEND_EMAIL_VERIFICATION_IS_LOADING';
|
|
179
|
-
var SET_SEND_EMAIL_VERIFICATION_STATUS = 'SET_SEND_EMAIL_VERIFICATION_STATUS';
|
|
180
|
-
|
|
181
|
-
function userReducer(state, action) {
|
|
182
|
-
var type = action.type,
|
|
183
|
-
payload = action.payload;
|
|
184
|
-
switch (type) {
|
|
185
|
-
case LOGIN:
|
|
186
|
-
{
|
|
187
|
-
var login = _extends({}, state, {
|
|
188
|
-
user: payload.user,
|
|
189
|
-
domain: payload.domain,
|
|
190
|
-
isAuthenticated: payload.isAuthenticated
|
|
191
|
-
});
|
|
192
|
-
return login;
|
|
193
|
-
}
|
|
194
|
-
case SET_IS_LOADING:
|
|
195
|
-
{
|
|
196
|
-
var isLoading = _extends({}, state, {
|
|
197
|
-
isLoading: payload.isLoading
|
|
198
|
-
});
|
|
199
|
-
return isLoading;
|
|
200
|
-
}
|
|
201
|
-
case UPDATE_USER:
|
|
202
|
-
{
|
|
203
|
-
var updateUser = _extends({}, state, {
|
|
204
|
-
user: _extends({}, state.user, payload)
|
|
205
|
-
});
|
|
206
|
-
return updateUser;
|
|
207
|
-
}
|
|
208
|
-
case SET_SEND_EMAIL_VERIFICATION_STATUS:
|
|
209
|
-
{
|
|
210
|
-
var setSendEmailVerificationStatus = _extends({}, state, {
|
|
211
|
-
sendEmailVerificationStatus: _extends({}, state.sendEmailVerificationStatus, {
|
|
212
|
-
status: {
|
|
213
|
-
type: payload.type,
|
|
214
|
-
message: payload.message
|
|
215
|
-
}
|
|
216
|
-
})
|
|
217
|
-
});
|
|
218
|
-
return setSendEmailVerificationStatus;
|
|
219
|
-
}
|
|
220
|
-
case SET_SEND_EMAIL_VERIFICATION_IS_LOADING:
|
|
221
|
-
{
|
|
222
|
-
var setSendEmailVerificationIsLoading = _extends({}, state, {
|
|
223
|
-
sendEmailVerificationStatus: _extends({}, state.sendEmailVerificationStatus, {
|
|
224
|
-
isLoading: payload
|
|
225
|
-
})
|
|
226
|
-
});
|
|
227
|
-
return setSendEmailVerificationIsLoading;
|
|
228
|
-
}
|
|
229
|
-
default:
|
|
230
|
-
return state;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
var apiVersion = 'v1';
|
|
235
|
-
var serverDomain = 'dauth.ovh';
|
|
236
|
-
function checkIsLocalhost() {
|
|
237
|
-
if (typeof window === 'undefined') return false;
|
|
238
|
-
var hostname = window.location.hostname;
|
|
239
|
-
return Boolean(hostname === 'localhost' || hostname === '[::1]' || hostname.match(/(192)\.(168)\.(1)\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/gm) || hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/));
|
|
240
|
-
}
|
|
241
|
-
function getServerBasePath() {
|
|
242
|
-
var isLocalhost = checkIsLocalhost();
|
|
243
|
-
var serverPort = 4012;
|
|
244
|
-
var serverLocalUrl = window.location.protocol + "//" + window.location.hostname + ":" + serverPort + "/api/" + apiVersion;
|
|
245
|
-
var serverProdUrl = "https://" + serverDomain + "/api/" + apiVersion;
|
|
246
|
-
return isLocalhost ? serverLocalUrl : serverProdUrl;
|
|
247
|
-
}
|
|
248
|
-
function getClientBasePath() {
|
|
249
|
-
var isLocalhost = checkIsLocalhost();
|
|
250
|
-
var clientPort = 5185;
|
|
251
|
-
var clientLocalUrl = window.location.protocol + "//" + window.location.hostname + ":" + clientPort;
|
|
252
|
-
var clientProdUrl = "https://" + serverDomain;
|
|
253
|
-
return isLocalhost ? clientLocalUrl : clientProdUrl;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
var getUserAPI = /*#__PURE__*/function () {
|
|
257
|
-
var _ref = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(domainName, token) {
|
|
258
|
-
var params, response, data;
|
|
259
|
-
return _regenerator().w(function (_context) {
|
|
260
|
-
while (1) switch (_context.n) {
|
|
261
|
-
case 0:
|
|
262
|
-
params = {
|
|
263
|
-
method: 'GET',
|
|
264
|
-
headers: {
|
|
265
|
-
Authorization: token,
|
|
266
|
-
'Content-Type': 'application/json'
|
|
267
|
-
}
|
|
268
|
-
};
|
|
269
|
-
_context.n = 1;
|
|
270
|
-
return fetch(getServerBasePath() + "/app/" + domainName + "/user", params);
|
|
271
|
-
case 1:
|
|
272
|
-
response = _context.v;
|
|
273
|
-
_context.n = 2;
|
|
274
|
-
return response.json();
|
|
275
|
-
case 2:
|
|
276
|
-
data = _context.v;
|
|
277
|
-
return _context.a(2, {
|
|
278
|
-
response: response,
|
|
279
|
-
data: data
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
}, _callee);
|
|
283
|
-
}));
|
|
284
|
-
return function getUserAPI(_x, _x2) {
|
|
285
|
-
return _ref.apply(this, arguments);
|
|
286
|
-
};
|
|
287
|
-
}();
|
|
288
|
-
var updateUserAPI = /*#__PURE__*/function () {
|
|
289
|
-
var _ref2 = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(domainName, user, token) {
|
|
290
|
-
var params, response, data;
|
|
291
|
-
return _regenerator().w(function (_context2) {
|
|
292
|
-
while (1) switch (_context2.n) {
|
|
293
|
-
case 0:
|
|
294
|
-
params = {
|
|
295
|
-
method: 'PATCH',
|
|
296
|
-
headers: {
|
|
297
|
-
Authorization: token,
|
|
298
|
-
'Content-Type': 'application/json'
|
|
299
|
-
},
|
|
300
|
-
body: JSON.stringify(user)
|
|
301
|
-
};
|
|
302
|
-
_context2.n = 1;
|
|
303
|
-
return fetch(getServerBasePath() + "/app/" + domainName + "/user", params);
|
|
304
|
-
case 1:
|
|
305
|
-
response = _context2.v;
|
|
306
|
-
_context2.n = 2;
|
|
307
|
-
return response.json();
|
|
308
|
-
case 2:
|
|
309
|
-
data = _context2.v;
|
|
310
|
-
return _context2.a(2, {
|
|
311
|
-
response: response,
|
|
312
|
-
data: data
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
}, _callee2);
|
|
316
|
-
}));
|
|
317
|
-
return function updateUserAPI(_x3, _x4, _x5) {
|
|
318
|
-
return _ref2.apply(this, arguments);
|
|
319
|
-
};
|
|
320
|
-
}();
|
|
321
|
-
var sendEmailVerificationAPI = /*#__PURE__*/function () {
|
|
322
|
-
var _ref3 = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(domainName, token) {
|
|
323
|
-
var params, response, data;
|
|
324
|
-
return _regenerator().w(function (_context3) {
|
|
325
|
-
while (1) switch (_context3.n) {
|
|
326
|
-
case 0:
|
|
327
|
-
params = {
|
|
328
|
-
method: 'GET',
|
|
329
|
-
headers: {
|
|
330
|
-
Authorization: token,
|
|
331
|
-
'Content-Type': 'application/json'
|
|
332
|
-
}
|
|
333
|
-
};
|
|
334
|
-
_context3.n = 1;
|
|
335
|
-
return fetch(getServerBasePath() + "/app/" + domainName + "/resend-email-verification", params);
|
|
336
|
-
case 1:
|
|
337
|
-
response = _context3.v;
|
|
338
|
-
_context3.n = 2;
|
|
339
|
-
return response.json();
|
|
340
|
-
case 2:
|
|
341
|
-
data = _context3.v;
|
|
342
|
-
return _context3.a(2, {
|
|
343
|
-
response: response,
|
|
344
|
-
data: data
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
}, _callee3);
|
|
348
|
-
}));
|
|
349
|
-
return function sendEmailVerificationAPI(_x6, _x7) {
|
|
350
|
-
return _ref3.apply(this, arguments);
|
|
351
|
-
};
|
|
352
|
-
}();
|
|
353
|
-
var refreshTokenAPI = /*#__PURE__*/function () {
|
|
354
|
-
var _ref5 = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(domainName, refreshToken) {
|
|
355
|
-
var params, response, data;
|
|
356
|
-
return _regenerator().w(function (_context5) {
|
|
357
|
-
while (1) switch (_context5.n) {
|
|
358
|
-
case 0:
|
|
359
|
-
params = {
|
|
360
|
-
method: 'POST',
|
|
361
|
-
headers: {
|
|
362
|
-
'Content-Type': 'application/json'
|
|
363
|
-
},
|
|
364
|
-
body: JSON.stringify({
|
|
365
|
-
refreshToken: refreshToken
|
|
366
|
-
})
|
|
367
|
-
};
|
|
368
|
-
_context5.n = 1;
|
|
369
|
-
return fetch(getServerBasePath() + "/app/" + domainName + "/refresh-token", params);
|
|
370
|
-
case 1:
|
|
371
|
-
response = _context5.v;
|
|
372
|
-
_context5.n = 2;
|
|
373
|
-
return response.json();
|
|
374
|
-
case 2:
|
|
375
|
-
data = _context5.v;
|
|
376
|
-
return _context5.a(2, {
|
|
377
|
-
response: response,
|
|
378
|
-
data: data
|
|
379
|
-
});
|
|
380
|
-
}
|
|
381
|
-
}, _callee5);
|
|
382
|
-
}));
|
|
383
|
-
return function refreshTokenAPI(_x0, _x1) {
|
|
384
|
-
return _ref5.apply(this, arguments);
|
|
385
|
-
};
|
|
386
|
-
}();
|
|
387
|
-
var logoutAPI = /*#__PURE__*/function () {
|
|
388
|
-
var _ref6 = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(domainName, refreshToken) {
|
|
389
|
-
var params, response;
|
|
390
|
-
return _regenerator().w(function (_context6) {
|
|
391
|
-
while (1) switch (_context6.n) {
|
|
392
|
-
case 0:
|
|
393
|
-
params = {
|
|
394
|
-
method: 'POST',
|
|
395
|
-
headers: {
|
|
396
|
-
'Content-Type': 'application/json'
|
|
397
|
-
},
|
|
398
|
-
body: JSON.stringify({
|
|
399
|
-
refreshToken: refreshToken
|
|
400
|
-
})
|
|
401
|
-
};
|
|
402
|
-
_context6.n = 1;
|
|
403
|
-
return fetch(getServerBasePath() + "/app/" + domainName + "/logout", params);
|
|
404
|
-
case 1:
|
|
405
|
-
response = _context6.v;
|
|
406
|
-
return _context6.a(2, {
|
|
407
|
-
response: response
|
|
408
|
-
});
|
|
409
|
-
}
|
|
410
|
-
}, _callee6);
|
|
411
|
-
}));
|
|
412
|
-
return function logoutAPI(_x10, _x11) {
|
|
413
|
-
return _ref6.apply(this, arguments);
|
|
414
|
-
};
|
|
415
|
-
}();
|
|
416
|
-
|
|
417
|
-
var TOKEN_LS = 'dauth_state';
|
|
418
|
-
var REFRESH_TOKEN_LS = 'dauth_refresh_token';
|
|
419
|
-
|
|
420
|
-
function setDauthStateAction(_x) {
|
|
421
|
-
return _setDauthStateAction.apply(this, arguments);
|
|
422
|
-
}
|
|
423
|
-
function _setDauthStateAction() {
|
|
424
|
-
_setDauthStateAction = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(_ref) {
|
|
425
|
-
var dispatch, token, refreshToken, domainName, getUserFetch, _t;
|
|
426
|
-
return _regenerator().w(function (_context) {
|
|
427
|
-
while (1) switch (_context.p = _context.n) {
|
|
428
|
-
case 0:
|
|
429
|
-
dispatch = _ref.dispatch, token = _ref.token, refreshToken = _ref.refreshToken, domainName = _ref.domainName;
|
|
430
|
-
dispatch({
|
|
431
|
-
type: SET_IS_LOADING,
|
|
432
|
-
payload: {
|
|
433
|
-
isLoading: true
|
|
434
|
-
}
|
|
435
|
-
});
|
|
436
|
-
_context.p = 1;
|
|
437
|
-
_context.n = 2;
|
|
438
|
-
return getUserAPI(domainName, token);
|
|
439
|
-
case 2:
|
|
440
|
-
getUserFetch = _context.v;
|
|
441
|
-
if (!(getUserFetch.response.status === 200)) {
|
|
442
|
-
_context.n = 3;
|
|
443
|
-
break;
|
|
444
|
-
}
|
|
445
|
-
dispatch({
|
|
446
|
-
type: LOGIN,
|
|
447
|
-
payload: {
|
|
448
|
-
user: getUserFetch.data.user,
|
|
449
|
-
domain: getUserFetch.data.domain,
|
|
450
|
-
isAuthenticated: true
|
|
451
|
-
}
|
|
452
|
-
});
|
|
453
|
-
window.history.replaceState({}, document.title, window.location.pathname);
|
|
454
|
-
localStorage.setItem(TOKEN_LS, token);
|
|
455
|
-
localStorage.setItem(REFRESH_TOKEN_LS, refreshToken);
|
|
456
|
-
return _context.a(2);
|
|
457
|
-
case 3:
|
|
458
|
-
return _context.a(2, resetUser(dispatch));
|
|
459
|
-
case 4:
|
|
460
|
-
_context.n = 6;
|
|
461
|
-
break;
|
|
462
|
-
case 5:
|
|
463
|
-
_context.p = 5;
|
|
464
|
-
_t = _context.v;
|
|
465
|
-
console.error(_t);
|
|
466
|
-
return _context.a(2, resetUser(dispatch));
|
|
467
|
-
case 6:
|
|
468
|
-
_context.p = 6;
|
|
469
|
-
dispatch({
|
|
470
|
-
type: SET_IS_LOADING,
|
|
471
|
-
payload: {
|
|
472
|
-
isLoading: false
|
|
473
|
-
}
|
|
474
|
-
});
|
|
475
|
-
return _context.f(6);
|
|
476
|
-
case 7:
|
|
477
|
-
return _context.a(2);
|
|
478
|
-
}
|
|
479
|
-
}, _callee, null, [[1, 5, 6, 7]]);
|
|
480
|
-
}));
|
|
481
|
-
return _setDauthStateAction.apply(this, arguments);
|
|
482
|
-
}
|
|
483
|
-
function setAutoLoginAction(_x2) {
|
|
484
|
-
return _setAutoLoginAction.apply(this, arguments);
|
|
485
|
-
}
|
|
486
|
-
function _setAutoLoginAction() {
|
|
487
|
-
_setAutoLoginAction = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(_ref2) {
|
|
488
|
-
var dispatch, domainName, storedRefreshToken, refreshResult, newAccessToken, newRefreshToken, getUserFetch, _t2;
|
|
489
|
-
return _regenerator().w(function (_context2) {
|
|
490
|
-
while (1) switch (_context2.p = _context2.n) {
|
|
491
|
-
case 0:
|
|
492
|
-
dispatch = _ref2.dispatch, domainName = _ref2.domainName;
|
|
493
|
-
dispatch({
|
|
494
|
-
type: SET_IS_LOADING,
|
|
495
|
-
payload: {
|
|
496
|
-
isLoading: true
|
|
497
|
-
}
|
|
498
|
-
});
|
|
499
|
-
storedRefreshToken = localStorage.getItem(REFRESH_TOKEN_LS);
|
|
500
|
-
if (storedRefreshToken) {
|
|
501
|
-
_context2.n = 1;
|
|
502
|
-
break;
|
|
503
|
-
}
|
|
504
|
-
dispatch({
|
|
505
|
-
type: SET_IS_LOADING,
|
|
506
|
-
payload: {
|
|
507
|
-
isLoading: false
|
|
508
|
-
}
|
|
509
|
-
});
|
|
510
|
-
return _context2.a(2, resetUser(dispatch));
|
|
511
|
-
case 1:
|
|
512
|
-
_context2.p = 1;
|
|
513
|
-
_context2.n = 2;
|
|
514
|
-
return refreshTokenAPI(domainName, storedRefreshToken);
|
|
515
|
-
case 2:
|
|
516
|
-
refreshResult = _context2.v;
|
|
517
|
-
if (!(refreshResult.response.status === 200)) {
|
|
518
|
-
_context2.n = 4;
|
|
519
|
-
break;
|
|
520
|
-
}
|
|
521
|
-
newAccessToken = refreshResult.data.accessToken;
|
|
522
|
-
newRefreshToken = refreshResult.data.refreshToken;
|
|
523
|
-
localStorage.setItem(TOKEN_LS, newAccessToken);
|
|
524
|
-
localStorage.setItem(REFRESH_TOKEN_LS, newRefreshToken);
|
|
525
|
-
_context2.n = 3;
|
|
526
|
-
return getUserAPI(domainName, newAccessToken);
|
|
527
|
-
case 3:
|
|
528
|
-
getUserFetch = _context2.v;
|
|
529
|
-
if (!(getUserFetch.response.status === 200)) {
|
|
530
|
-
_context2.n = 4;
|
|
531
|
-
break;
|
|
532
|
-
}
|
|
533
|
-
dispatch({
|
|
534
|
-
type: LOGIN,
|
|
535
|
-
payload: {
|
|
536
|
-
user: getUserFetch.data.user,
|
|
537
|
-
domain: getUserFetch.data.domain,
|
|
538
|
-
isAuthenticated: true
|
|
539
|
-
}
|
|
540
|
-
});
|
|
541
|
-
return _context2.a(2);
|
|
542
|
-
case 4:
|
|
543
|
-
// Refresh failed — session expired
|
|
544
|
-
resetUser(dispatch);
|
|
545
|
-
_context2.n = 6;
|
|
546
|
-
break;
|
|
547
|
-
case 5:
|
|
548
|
-
_context2.p = 5;
|
|
549
|
-
_t2 = _context2.v;
|
|
550
|
-
console.error(_t2);
|
|
551
|
-
resetUser(dispatch);
|
|
552
|
-
case 6:
|
|
553
|
-
_context2.p = 6;
|
|
554
|
-
dispatch({
|
|
555
|
-
type: SET_IS_LOADING,
|
|
556
|
-
payload: {
|
|
557
|
-
isLoading: false
|
|
558
|
-
}
|
|
559
|
-
});
|
|
560
|
-
return _context2.f(6);
|
|
561
|
-
case 7:
|
|
562
|
-
return _context2.a(2);
|
|
563
|
-
}
|
|
564
|
-
}, _callee2, null, [[1, 5, 6, 7]]);
|
|
565
|
-
}));
|
|
566
|
-
return _setAutoLoginAction.apply(this, arguments);
|
|
567
|
-
}
|
|
568
|
-
function setLogoutAction(_x3) {
|
|
569
|
-
return _setLogoutAction.apply(this, arguments);
|
|
570
|
-
}
|
|
571
|
-
function _setLogoutAction() {
|
|
572
|
-
_setLogoutAction = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(_ref3) {
|
|
573
|
-
var dispatch, domainName, storedRefreshToken;
|
|
574
|
-
return _regenerator().w(function (_context3) {
|
|
575
|
-
while (1) switch (_context3.p = _context3.n) {
|
|
576
|
-
case 0:
|
|
577
|
-
dispatch = _ref3.dispatch, domainName = _ref3.domainName;
|
|
578
|
-
storedRefreshToken = localStorage.getItem(REFRESH_TOKEN_LS);
|
|
579
|
-
if (!(storedRefreshToken && domainName)) {
|
|
580
|
-
_context3.n = 4;
|
|
581
|
-
break;
|
|
582
|
-
}
|
|
583
|
-
_context3.p = 1;
|
|
584
|
-
_context3.n = 2;
|
|
585
|
-
return logoutAPI(domainName, storedRefreshToken);
|
|
586
|
-
case 2:
|
|
587
|
-
_context3.n = 4;
|
|
588
|
-
break;
|
|
589
|
-
case 3:
|
|
590
|
-
_context3.p = 3;
|
|
591
|
-
case 4:
|
|
592
|
-
dispatch({
|
|
593
|
-
type: SET_IS_LOADING,
|
|
594
|
-
payload: {
|
|
595
|
-
isLoading: true
|
|
596
|
-
}
|
|
597
|
-
});
|
|
598
|
-
dispatch({
|
|
599
|
-
type: LOGIN,
|
|
600
|
-
payload: {
|
|
601
|
-
user: {
|
|
602
|
-
language: window.document.documentElement.getAttribute('lang') || 'es'
|
|
603
|
-
},
|
|
604
|
-
domain: {},
|
|
605
|
-
isAuthenticated: false
|
|
606
|
-
}
|
|
607
|
-
});
|
|
608
|
-
localStorage.removeItem(TOKEN_LS);
|
|
609
|
-
localStorage.removeItem(REFRESH_TOKEN_LS);
|
|
610
|
-
return _context3.a(2, dispatch({
|
|
611
|
-
type: SET_IS_LOADING,
|
|
612
|
-
payload: {
|
|
613
|
-
isLoading: false
|
|
614
|
-
}
|
|
615
|
-
}));
|
|
616
|
-
}
|
|
617
|
-
}, _callee3, null, [[1, 3]]);
|
|
618
|
-
}));
|
|
619
|
-
return _setLogoutAction.apply(this, arguments);
|
|
620
|
-
}
|
|
621
|
-
function refreshSessionAction(_x4) {
|
|
622
|
-
return _refreshSessionAction.apply(this, arguments);
|
|
623
|
-
}
|
|
624
|
-
function _refreshSessionAction() {
|
|
625
|
-
_refreshSessionAction = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(_ref4) {
|
|
626
|
-
var dispatch, domainName, storedRefreshToken, refreshResult, _t4;
|
|
627
|
-
return _regenerator().w(function (_context4) {
|
|
628
|
-
while (1) switch (_context4.p = _context4.n) {
|
|
629
|
-
case 0:
|
|
630
|
-
dispatch = _ref4.dispatch, domainName = _ref4.domainName;
|
|
631
|
-
storedRefreshToken = localStorage.getItem(REFRESH_TOKEN_LS);
|
|
632
|
-
if (storedRefreshToken) {
|
|
633
|
-
_context4.n = 1;
|
|
634
|
-
break;
|
|
635
|
-
}
|
|
636
|
-
return _context4.a(2, resetUser(dispatch));
|
|
637
|
-
case 1:
|
|
638
|
-
_context4.p = 1;
|
|
639
|
-
_context4.n = 2;
|
|
640
|
-
return refreshTokenAPI(domainName, storedRefreshToken);
|
|
641
|
-
case 2:
|
|
642
|
-
refreshResult = _context4.v;
|
|
643
|
-
if (!(refreshResult.response.status === 200)) {
|
|
644
|
-
_context4.n = 3;
|
|
645
|
-
break;
|
|
646
|
-
}
|
|
647
|
-
localStorage.setItem(TOKEN_LS, refreshResult.data.accessToken);
|
|
648
|
-
localStorage.setItem(REFRESH_TOKEN_LS, refreshResult.data.refreshToken);
|
|
649
|
-
return _context4.a(2);
|
|
650
|
-
case 3:
|
|
651
|
-
// Refresh failed — revoked or expired
|
|
652
|
-
resetUser(dispatch);
|
|
653
|
-
_context4.n = 5;
|
|
654
|
-
break;
|
|
655
|
-
case 4:
|
|
656
|
-
_context4.p = 4;
|
|
657
|
-
_t4 = _context4.v;
|
|
658
|
-
console.error(_t4);
|
|
659
|
-
resetUser(dispatch);
|
|
660
|
-
case 5:
|
|
661
|
-
return _context4.a(2);
|
|
662
|
-
}
|
|
663
|
-
}, _callee4, null, [[1, 4]]);
|
|
664
|
-
}));
|
|
665
|
-
return _refreshSessionAction.apply(this, arguments);
|
|
666
|
-
}
|
|
667
|
-
function setUpdateUserAction(_x5) {
|
|
668
|
-
return _setUpdateUserAction.apply(this, arguments);
|
|
669
|
-
}
|
|
670
|
-
function _setUpdateUserAction() {
|
|
671
|
-
_setUpdateUserAction = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(_ref5) {
|
|
672
|
-
var dispatch, domainName, user, token, getUserFetch, _t5;
|
|
673
|
-
return _regenerator().w(function (_context5) {
|
|
674
|
-
while (1) switch (_context5.p = _context5.n) {
|
|
675
|
-
case 0:
|
|
676
|
-
dispatch = _ref5.dispatch, domainName = _ref5.domainName, user = _ref5.user, token = _ref5.token;
|
|
677
|
-
if (user.language) {
|
|
678
|
-
window.document.documentElement.setAttribute('lang', user.language);
|
|
679
|
-
}
|
|
680
|
-
if (token) {
|
|
681
|
-
_context5.n = 1;
|
|
682
|
-
break;
|
|
683
|
-
}
|
|
684
|
-
dispatch({
|
|
685
|
-
type: UPDATE_USER,
|
|
686
|
-
payload: user
|
|
687
|
-
});
|
|
688
|
-
return _context5.a(2, false);
|
|
689
|
-
case 1:
|
|
690
|
-
_context5.p = 1;
|
|
691
|
-
_context5.n = 2;
|
|
692
|
-
return updateUserAPI(domainName, user, token);
|
|
693
|
-
case 2:
|
|
694
|
-
getUserFetch = _context5.v;
|
|
695
|
-
if (!(getUserFetch.response.status === 200)) {
|
|
696
|
-
_context5.n = 3;
|
|
697
|
-
break;
|
|
698
|
-
}
|
|
699
|
-
dispatch({
|
|
700
|
-
type: UPDATE_USER,
|
|
701
|
-
payload: getUserFetch.data.user
|
|
702
|
-
});
|
|
703
|
-
return _context5.a(2, true);
|
|
704
|
-
case 3:
|
|
705
|
-
console.error('Update user error', getUserFetch.data.message);
|
|
706
|
-
return _context5.a(2, false);
|
|
707
|
-
case 4:
|
|
708
|
-
_context5.n = 6;
|
|
709
|
-
break;
|
|
710
|
-
case 5:
|
|
711
|
-
_context5.p = 5;
|
|
712
|
-
_t5 = _context5.v;
|
|
713
|
-
console.error('Update user error', _t5);
|
|
714
|
-
return _context5.a(2, false);
|
|
715
|
-
case 6:
|
|
716
|
-
return _context5.a(2);
|
|
717
|
-
}
|
|
718
|
-
}, _callee5, null, [[1, 5]]);
|
|
719
|
-
}));
|
|
720
|
-
return _setUpdateUserAction.apply(this, arguments);
|
|
721
|
-
}
|
|
722
|
-
function sendEmailVerificationAction(_x6) {
|
|
723
|
-
return _sendEmailVerificationAction.apply(this, arguments);
|
|
724
|
-
}
|
|
725
|
-
function _sendEmailVerificationAction() {
|
|
726
|
-
_sendEmailVerificationAction = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(_ref6) {
|
|
727
|
-
var dispatch, domainName, token, sendEmailFetch;
|
|
728
|
-
return _regenerator().w(function (_context6) {
|
|
729
|
-
while (1) switch (_context6.p = _context6.n) {
|
|
730
|
-
case 0:
|
|
731
|
-
dispatch = _ref6.dispatch, domainName = _ref6.domainName, token = _ref6.token;
|
|
732
|
-
dispatch({
|
|
733
|
-
type: SET_SEND_EMAIL_VERIFICATION_IS_LOADING,
|
|
734
|
-
payload: true
|
|
735
|
-
});
|
|
736
|
-
dispatch({
|
|
737
|
-
type: SET_SEND_EMAIL_VERIFICATION_STATUS,
|
|
738
|
-
payload: {
|
|
739
|
-
type: 'info',
|
|
740
|
-
message: 'Sending email verification...'
|
|
741
|
-
}
|
|
742
|
-
});
|
|
743
|
-
_context6.p = 1;
|
|
744
|
-
_context6.n = 2;
|
|
745
|
-
return sendEmailVerificationAPI(domainName, token);
|
|
746
|
-
case 2:
|
|
747
|
-
sendEmailFetch = _context6.v;
|
|
748
|
-
if (!(sendEmailFetch.response.status === 200)) {
|
|
749
|
-
_context6.n = 3;
|
|
750
|
-
break;
|
|
751
|
-
}
|
|
752
|
-
dispatch({
|
|
753
|
-
type: SET_SEND_EMAIL_VERIFICATION_STATUS,
|
|
754
|
-
payload: {
|
|
755
|
-
type: 'success',
|
|
756
|
-
message: sendEmailFetch.data.message
|
|
757
|
-
}
|
|
758
|
-
});
|
|
759
|
-
dispatch({
|
|
760
|
-
type: SET_SEND_EMAIL_VERIFICATION_IS_LOADING,
|
|
761
|
-
payload: false
|
|
762
|
-
});
|
|
763
|
-
return _context6.a(2, true);
|
|
764
|
-
case 3:
|
|
765
|
-
dispatch({
|
|
766
|
-
type: SET_SEND_EMAIL_VERIFICATION_STATUS,
|
|
767
|
-
payload: {
|
|
768
|
-
type: 'error',
|
|
769
|
-
message: sendEmailFetch.data.message
|
|
770
|
-
}
|
|
771
|
-
});
|
|
772
|
-
dispatch({
|
|
773
|
-
type: SET_SEND_EMAIL_VERIFICATION_IS_LOADING,
|
|
774
|
-
payload: false
|
|
775
|
-
});
|
|
776
|
-
return _context6.a(2, false);
|
|
777
|
-
case 4:
|
|
778
|
-
_context6.n = 6;
|
|
779
|
-
break;
|
|
780
|
-
case 5:
|
|
781
|
-
_context6.p = 5;
|
|
782
|
-
dispatch({
|
|
783
|
-
type: SET_SEND_EMAIL_VERIFICATION_STATUS,
|
|
784
|
-
payload: {
|
|
785
|
-
type: 'error',
|
|
786
|
-
message: 'Send email verification fetch error'
|
|
787
|
-
}
|
|
788
|
-
});
|
|
789
|
-
dispatch({
|
|
790
|
-
type: SET_SEND_EMAIL_VERIFICATION_IS_LOADING,
|
|
791
|
-
payload: false
|
|
792
|
-
});
|
|
793
|
-
return _context6.a(2, false);
|
|
794
|
-
case 6:
|
|
795
|
-
return _context6.a(2);
|
|
796
|
-
}
|
|
797
|
-
}, _callee6, null, [[1, 5]]);
|
|
798
|
-
}));
|
|
799
|
-
return _sendEmailVerificationAction.apply(this, arguments);
|
|
800
|
-
}
|
|
801
|
-
function getAccessTokenAction(_x7) {
|
|
802
|
-
return _getAccessTokenAction.apply(this, arguments);
|
|
803
|
-
}
|
|
804
|
-
///////////////////////////////////////////
|
|
805
|
-
//////////////////////////////////////////
|
|
806
|
-
function _getAccessTokenAction() {
|
|
807
|
-
_getAccessTokenAction = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(_ref7) {
|
|
808
|
-
var dispatch, domainName, token_ls, payloadB64, payload, expiresIn, refreshedToken;
|
|
809
|
-
return _regenerator().w(function (_context7) {
|
|
810
|
-
while (1) switch (_context7.p = _context7.n) {
|
|
811
|
-
case 0:
|
|
812
|
-
dispatch = _ref7.dispatch, domainName = _ref7.domainName;
|
|
813
|
-
token_ls = localStorage.getItem(TOKEN_LS);
|
|
814
|
-
if (token_ls) {
|
|
815
|
-
_context7.n = 1;
|
|
816
|
-
break;
|
|
817
|
-
}
|
|
818
|
-
return _context7.a(2, 'token-not-found');
|
|
819
|
-
case 1:
|
|
820
|
-
_context7.p = 1;
|
|
821
|
-
payloadB64 = token_ls.split('.')[1];
|
|
822
|
-
if (!payloadB64) {
|
|
823
|
-
_context7.n = 3;
|
|
824
|
-
break;
|
|
825
|
-
}
|
|
826
|
-
payload = JSON.parse(atob(payloadB64));
|
|
827
|
-
expiresIn = (payload.exp || 0) * 1000 - Date.now(); // If token expires in less than 5 minutes, refresh proactively
|
|
828
|
-
if (!(expiresIn < 5 * 60 * 1000)) {
|
|
829
|
-
_context7.n = 3;
|
|
830
|
-
break;
|
|
831
|
-
}
|
|
832
|
-
_context7.n = 2;
|
|
833
|
-
return refreshSessionAction({
|
|
834
|
-
dispatch: dispatch,
|
|
835
|
-
domainName: domainName
|
|
836
|
-
});
|
|
837
|
-
case 2:
|
|
838
|
-
refreshedToken = localStorage.getItem(TOKEN_LS);
|
|
839
|
-
return _context7.a(2, refreshedToken || 'token-not-found');
|
|
840
|
-
case 3:
|
|
841
|
-
_context7.n = 5;
|
|
842
|
-
break;
|
|
843
|
-
case 4:
|
|
844
|
-
_context7.p = 4;
|
|
845
|
-
case 5:
|
|
846
|
-
return _context7.a(2, token_ls);
|
|
847
|
-
}
|
|
848
|
-
}, _callee7, null, [[1, 4]]);
|
|
849
|
-
}));
|
|
850
|
-
return _getAccessTokenAction.apply(this, arguments);
|
|
851
|
-
}
|
|
852
|
-
var resetUser = function resetUser(dispatch) {
|
|
853
|
-
localStorage.removeItem(TOKEN_LS);
|
|
854
|
-
localStorage.removeItem(REFRESH_TOKEN_LS);
|
|
855
|
-
return dispatch({
|
|
856
|
-
type: LOGIN,
|
|
857
|
-
payload: {
|
|
858
|
-
user: {},
|
|
859
|
-
domain: {},
|
|
860
|
-
isAuthenticated: false
|
|
861
|
-
}
|
|
862
|
-
});
|
|
863
|
-
};
|
|
864
|
-
|
|
865
|
-
var routes = {
|
|
866
|
-
signin: 'signin',
|
|
867
|
-
updateUser: 'update-user'
|
|
868
|
-
};
|
|
869
|
-
|
|
870
|
-
var DauthProvider = function DauthProvider(props) {
|
|
871
|
-
var domainName = props.domainName,
|
|
872
|
-
children = props.children;
|
|
873
|
-
var _useReducer = useReducer(userReducer, initialDauthState),
|
|
874
|
-
dauthState = _useReducer[0],
|
|
875
|
-
dispatch = _useReducer[1];
|
|
876
|
-
var refreshTimerRef = useRef(null);
|
|
877
|
-
// Schedule next proactive refresh based on access token expiry
|
|
878
|
-
var scheduleRefresh = useCallback(function () {
|
|
879
|
-
if (refreshTimerRef.current) clearTimeout(refreshTimerRef.current);
|
|
880
|
-
var token = localStorage.getItem(TOKEN_LS);
|
|
881
|
-
if (!token) return;
|
|
882
|
-
try {
|
|
883
|
-
var payloadB64 = token.split('.')[1];
|
|
884
|
-
if (!payloadB64) return;
|
|
885
|
-
var payload = JSON.parse(atob(payloadB64));
|
|
886
|
-
var expiresIn = (payload.exp || 0) * 1000 - Date.now();
|
|
887
|
-
// Refresh 5 minutes before expiry, minimum 10 seconds
|
|
888
|
-
var refreshIn = Math.max(expiresIn - 5 * 60 * 1000, 10000);
|
|
889
|
-
refreshTimerRef.current = setTimeout(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
|
|
890
|
-
return _regenerator().w(function (_context) {
|
|
891
|
-
while (1) switch (_context.n) {
|
|
892
|
-
case 0:
|
|
893
|
-
_context.n = 1;
|
|
894
|
-
return refreshSessionAction({
|
|
895
|
-
dispatch: dispatch,
|
|
896
|
-
domainName: domainName
|
|
897
|
-
});
|
|
898
|
-
case 1:
|
|
899
|
-
scheduleRefresh();
|
|
900
|
-
case 2:
|
|
901
|
-
return _context.a(2);
|
|
902
|
-
}
|
|
903
|
-
}, _callee);
|
|
904
|
-
})), refreshIn);
|
|
905
|
-
} catch (_) {
|
|
906
|
-
// If decode fails, retry in 5 minutes
|
|
907
|
-
refreshTimerRef.current = setTimeout(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
|
|
908
|
-
return _regenerator().w(function (_context2) {
|
|
909
|
-
while (1) switch (_context2.n) {
|
|
910
|
-
case 0:
|
|
911
|
-
_context2.n = 1;
|
|
912
|
-
return refreshSessionAction({
|
|
913
|
-
dispatch: dispatch,
|
|
914
|
-
domainName: domainName
|
|
915
|
-
});
|
|
916
|
-
case 1:
|
|
917
|
-
scheduleRefresh();
|
|
918
|
-
case 2:
|
|
919
|
-
return _context2.a(2);
|
|
920
|
-
}
|
|
921
|
-
}, _callee2);
|
|
922
|
-
})), 5 * 60 * 1000);
|
|
923
|
-
}
|
|
924
|
-
}, [domainName]);
|
|
925
|
-
// Catch login redirect
|
|
926
|
-
useEffect(function () {
|
|
927
|
-
_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() {
|
|
928
|
-
var queryString, urlParams, token_url, refresh_url;
|
|
929
|
-
return _regenerator().w(function (_context3) {
|
|
930
|
-
while (1) switch (_context3.n) {
|
|
931
|
-
case 0:
|
|
932
|
-
queryString = window.location.search;
|
|
933
|
-
if (queryString) {
|
|
934
|
-
_context3.n = 1;
|
|
935
|
-
break;
|
|
936
|
-
}
|
|
937
|
-
return _context3.a(2);
|
|
938
|
-
case 1:
|
|
939
|
-
urlParams = new URLSearchParams(queryString);
|
|
940
|
-
token_url = urlParams.get(TOKEN_LS);
|
|
941
|
-
refresh_url = urlParams.get(REFRESH_TOKEN_LS);
|
|
942
|
-
if (!(token_url && refresh_url && !dauthState.isAuthenticated)) {
|
|
943
|
-
_context3.n = 2;
|
|
944
|
-
break;
|
|
945
|
-
}
|
|
946
|
-
return _context3.a(2, setDauthStateAction({
|
|
947
|
-
dispatch: dispatch,
|
|
948
|
-
token: token_url,
|
|
949
|
-
refreshToken: refresh_url,
|
|
950
|
-
domainName: domainName
|
|
951
|
-
}));
|
|
952
|
-
case 2:
|
|
953
|
-
return _context3.a(2);
|
|
954
|
-
}
|
|
955
|
-
}, _callee3);
|
|
956
|
-
}))();
|
|
957
|
-
}, []);
|
|
958
|
-
// Auto Login
|
|
959
|
-
useEffect(function () {
|
|
960
|
-
_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4() {
|
|
961
|
-
var refreshToken;
|
|
962
|
-
return _regenerator().w(function (_context4) {
|
|
963
|
-
while (1) switch (_context4.n) {
|
|
964
|
-
case 0:
|
|
965
|
-
refreshToken = localStorage.getItem(REFRESH_TOKEN_LS);
|
|
966
|
-
if (!(refreshToken && !dauthState.isAuthenticated)) {
|
|
967
|
-
_context4.n = 1;
|
|
968
|
-
break;
|
|
969
|
-
}
|
|
970
|
-
return _context4.a(2, setAutoLoginAction({
|
|
971
|
-
dispatch: dispatch,
|
|
972
|
-
domainName: domainName
|
|
973
|
-
}));
|
|
974
|
-
case 1:
|
|
975
|
-
return _context4.a(2, dispatch({
|
|
976
|
-
type: SET_IS_LOADING,
|
|
977
|
-
payload: {
|
|
978
|
-
isLoading: false
|
|
979
|
-
}
|
|
980
|
-
}));
|
|
981
|
-
case 2:
|
|
982
|
-
return _context4.a(2);
|
|
983
|
-
}
|
|
984
|
-
}, _callee4);
|
|
985
|
-
}))();
|
|
986
|
-
}, []);
|
|
987
|
-
// Schedule proactive refresh when authenticated
|
|
988
|
-
useEffect(function () {
|
|
989
|
-
if (dauthState.isAuthenticated) {
|
|
990
|
-
scheduleRefresh();
|
|
991
|
-
}
|
|
992
|
-
return function () {
|
|
993
|
-
if (refreshTimerRef.current) clearTimeout(refreshTimerRef.current);
|
|
994
|
-
};
|
|
995
|
-
}, [dauthState.isAuthenticated, scheduleRefresh]);
|
|
996
|
-
var loginWithRedirect = useCallback(function () {
|
|
997
|
-
return window.location.replace(getClientBasePath() + "/" + domainName + "/" + routes.signin);
|
|
998
|
-
}, [domainName]);
|
|
999
|
-
var logout = useCallback(function () {
|
|
1000
|
-
if (refreshTimerRef.current) clearTimeout(refreshTimerRef.current);
|
|
1001
|
-
return setLogoutAction({
|
|
1002
|
-
dispatch: dispatch,
|
|
1003
|
-
domainName: domainName
|
|
1004
|
-
});
|
|
1005
|
-
}, [domainName]);
|
|
1006
|
-
var getAccessToken = useCallback(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5() {
|
|
1007
|
-
var token;
|
|
1008
|
-
return _regenerator().w(function (_context5) {
|
|
1009
|
-
while (1) switch (_context5.n) {
|
|
1010
|
-
case 0:
|
|
1011
|
-
_context5.n = 1;
|
|
1012
|
-
return getAccessTokenAction({
|
|
1013
|
-
dispatch: dispatch,
|
|
1014
|
-
domainName: domainName
|
|
1015
|
-
});
|
|
1016
|
-
case 1:
|
|
1017
|
-
token = _context5.v;
|
|
1018
|
-
return _context5.a(2, token);
|
|
1019
|
-
}
|
|
1020
|
-
}, _callee5);
|
|
1021
|
-
})), [domainName]);
|
|
1022
|
-
var updateUser = useCallback(/*#__PURE__*/function () {
|
|
1023
|
-
var _ref6 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(fields) {
|
|
1024
|
-
var token_ls, name, lastname, nickname, telPrefix, telSuffix, language, avatar, birthDate, country, metadata, user;
|
|
1025
|
-
return _regenerator().w(function (_context6) {
|
|
1026
|
-
while (1) switch (_context6.n) {
|
|
1027
|
-
case 0:
|
|
1028
|
-
token_ls = localStorage.getItem(TOKEN_LS);
|
|
1029
|
-
name = fields.name, lastname = fields.lastname, nickname = fields.nickname, telPrefix = fields.telPrefix, telSuffix = fields.telSuffix, language = fields.language, avatar = fields.avatar, birthDate = fields.birthDate, country = fields.country, metadata = fields.metadata;
|
|
1030
|
-
user = {
|
|
1031
|
-
name: name,
|
|
1032
|
-
lastname: lastname,
|
|
1033
|
-
nickname: nickname,
|
|
1034
|
-
telPrefix: telPrefix,
|
|
1035
|
-
telSuffix: telSuffix,
|
|
1036
|
-
language: language,
|
|
1037
|
-
avatar: avatar,
|
|
1038
|
-
birthDate: birthDate,
|
|
1039
|
-
country: country,
|
|
1040
|
-
metadata: metadata
|
|
1041
|
-
};
|
|
1042
|
-
_context6.n = 1;
|
|
1043
|
-
return setUpdateUserAction({
|
|
1044
|
-
dispatch: dispatch,
|
|
1045
|
-
domainName: domainName,
|
|
1046
|
-
user: user,
|
|
1047
|
-
token: token_ls
|
|
1048
|
-
});
|
|
1049
|
-
case 1:
|
|
1050
|
-
return _context6.a(2, _context6.v);
|
|
1051
|
-
}
|
|
1052
|
-
}, _callee6);
|
|
1053
|
-
}));
|
|
1054
|
-
return function (_x) {
|
|
1055
|
-
return _ref6.apply(this, arguments);
|
|
1056
|
-
};
|
|
1057
|
-
}(), [domainName]);
|
|
1058
|
-
var updateUserWithRedirect = useCallback(function () {
|
|
1059
|
-
var token_ls = localStorage.getItem(TOKEN_LS);
|
|
1060
|
-
if (!token_ls) return;
|
|
1061
|
-
return window.location.replace(getClientBasePath() + "/" + domainName + "/" + routes.updateUser + "/" + token_ls);
|
|
1062
|
-
}, [domainName]);
|
|
1063
|
-
var sendEmailVerification = useCallback(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7() {
|
|
1064
|
-
var token_ls;
|
|
1065
|
-
return _regenerator().w(function (_context7) {
|
|
1066
|
-
while (1) switch (_context7.n) {
|
|
1067
|
-
case 0:
|
|
1068
|
-
token_ls = localStorage.getItem(TOKEN_LS);
|
|
1069
|
-
if (token_ls) {
|
|
1070
|
-
_context7.n = 1;
|
|
1071
|
-
break;
|
|
1072
|
-
}
|
|
1073
|
-
return _context7.a(2, false);
|
|
1074
|
-
case 1:
|
|
1075
|
-
_context7.n = 2;
|
|
1076
|
-
return sendEmailVerificationAction({
|
|
1077
|
-
dispatch: dispatch,
|
|
1078
|
-
domainName: domainName,
|
|
1079
|
-
token: token_ls
|
|
1080
|
-
});
|
|
1081
|
-
case 2:
|
|
1082
|
-
return _context7.a(2, _context7.v);
|
|
1083
|
-
}
|
|
1084
|
-
}, _callee7);
|
|
1085
|
-
})), [domainName]);
|
|
1086
|
-
var memoProvider = useMemo(function () {
|
|
1087
|
-
return _extends({}, dauthState, {
|
|
1088
|
-
loginWithRedirect: loginWithRedirect,
|
|
1089
|
-
logout: logout,
|
|
1090
|
-
getAccessToken: getAccessToken,
|
|
1091
|
-
updateUser: updateUser,
|
|
1092
|
-
updateUserWithRedirect: updateUserWithRedirect,
|
|
1093
|
-
sendEmailVerification: sendEmailVerification
|
|
1094
|
-
});
|
|
1095
|
-
}, [dauthState, loginWithRedirect, logout, getAccessToken, updateUser, updateUserWithRedirect, sendEmailVerification]);
|
|
1096
|
-
return React.createElement(DauthContext.Provider, {
|
|
1097
|
-
value: memoProvider
|
|
1098
|
-
}, children);
|
|
1099
|
-
};
|
|
1100
|
-
var DauthContext = /*#__PURE__*/createContext(initialDauthState);
|
|
1101
|
-
var useDauth = function useDauth() {
|
|
1102
|
-
var context = useContext(DauthContext);
|
|
1103
|
-
if (!context) {
|
|
1104
|
-
throw new Error('useDauth must be used inside DauthProvider');
|
|
1105
|
-
}
|
|
1106
|
-
return context;
|
|
1107
|
-
};
|
|
1108
|
-
|
|
1109
|
-
export { DauthProvider, useDauth };
|
|
1110
|
-
//# sourceMappingURL=dauth-context-react.esm.js.map
|