dauth-context-react 2.2.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.
@@ -1,1203 +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
- deleteAccount: function deleteAccount() {
174
- return Promise.resolve(false);
175
- }
176
- };
177
-
178
- var LOGIN = 'LOGIN';
179
- var SET_IS_LOADING = 'SET_IS_LOADING';
180
- var UPDATE_USER = 'UPDATE_USER';
181
- var SET_SEND_EMAIL_VERIFICATION_IS_LOADING = 'SET_SEND_EMAIL_VERIFICATION_IS_LOADING';
182
- var SET_SEND_EMAIL_VERIFICATION_STATUS = 'SET_SEND_EMAIL_VERIFICATION_STATUS';
183
-
184
- function userReducer(state, action) {
185
- var type = action.type,
186
- payload = action.payload;
187
- switch (type) {
188
- case LOGIN:
189
- {
190
- var login = _extends({}, state, {
191
- user: payload.user,
192
- domain: payload.domain,
193
- isAuthenticated: payload.isAuthenticated
194
- });
195
- return login;
196
- }
197
- case SET_IS_LOADING:
198
- {
199
- var isLoading = _extends({}, state, {
200
- isLoading: payload.isLoading
201
- });
202
- return isLoading;
203
- }
204
- case UPDATE_USER:
205
- {
206
- var updateUser = _extends({}, state, {
207
- user: _extends({}, state.user, payload)
208
- });
209
- return updateUser;
210
- }
211
- case SET_SEND_EMAIL_VERIFICATION_STATUS:
212
- {
213
- var setSendEmailVerificationStatus = _extends({}, state, {
214
- sendEmailVerificationStatus: _extends({}, state.sendEmailVerificationStatus, {
215
- status: {
216
- type: payload.type,
217
- message: payload.message
218
- }
219
- })
220
- });
221
- return setSendEmailVerificationStatus;
222
- }
223
- case SET_SEND_EMAIL_VERIFICATION_IS_LOADING:
224
- {
225
- var setSendEmailVerificationIsLoading = _extends({}, state, {
226
- sendEmailVerificationStatus: _extends({}, state.sendEmailVerificationStatus, {
227
- isLoading: payload
228
- })
229
- });
230
- return setSendEmailVerificationIsLoading;
231
- }
232
- default:
233
- return state;
234
- }
235
- }
236
-
237
- var apiVersion = 'v1';
238
- var serverDomain = 'dauth.ovh';
239
- function checkIsLocalhost() {
240
- if (typeof window === 'undefined') return false;
241
- var hostname = window.location.hostname;
242
- 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}$/));
243
- }
244
- function getServerBasePath() {
245
- var isLocalhost = checkIsLocalhost();
246
- var serverPort = 4012;
247
- var serverLocalUrl = window.location.protocol + "//" + window.location.hostname + ":" + serverPort + "/api/" + apiVersion;
248
- var serverProdUrl = "https://" + serverDomain + "/api/" + apiVersion;
249
- return isLocalhost ? serverLocalUrl : serverProdUrl;
250
- }
251
- function getClientBasePath() {
252
- var isLocalhost = checkIsLocalhost();
253
- var clientPort = 5185;
254
- var clientLocalUrl = window.location.protocol + "//" + window.location.hostname + ":" + clientPort;
255
- var clientProdUrl = "https://" + serverDomain;
256
- return isLocalhost ? clientLocalUrl : clientProdUrl;
257
- }
258
-
259
- var getUserAPI = /*#__PURE__*/function () {
260
- var _ref = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(domainName, token) {
261
- var params, response, data;
262
- return _regenerator().w(function (_context) {
263
- while (1) switch (_context.n) {
264
- case 0:
265
- params = {
266
- method: 'GET',
267
- headers: {
268
- Authorization: token,
269
- 'Content-Type': 'application/json'
270
- }
271
- };
272
- _context.n = 1;
273
- return fetch(getServerBasePath() + "/app/" + domainName + "/user", params);
274
- case 1:
275
- response = _context.v;
276
- _context.n = 2;
277
- return response.json();
278
- case 2:
279
- data = _context.v;
280
- return _context.a(2, {
281
- response: response,
282
- data: data
283
- });
284
- }
285
- }, _callee);
286
- }));
287
- return function getUserAPI(_x, _x2) {
288
- return _ref.apply(this, arguments);
289
- };
290
- }();
291
- var updateUserAPI = /*#__PURE__*/function () {
292
- var _ref2 = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(domainName, user, token) {
293
- var params, response, data;
294
- return _regenerator().w(function (_context2) {
295
- while (1) switch (_context2.n) {
296
- case 0:
297
- params = {
298
- method: 'PATCH',
299
- headers: {
300
- Authorization: token,
301
- 'Content-Type': 'application/json'
302
- },
303
- body: JSON.stringify(user)
304
- };
305
- _context2.n = 1;
306
- return fetch(getServerBasePath() + "/app/" + domainName + "/user", params);
307
- case 1:
308
- response = _context2.v;
309
- _context2.n = 2;
310
- return response.json();
311
- case 2:
312
- data = _context2.v;
313
- return _context2.a(2, {
314
- response: response,
315
- data: data
316
- });
317
- }
318
- }, _callee2);
319
- }));
320
- return function updateUserAPI(_x3, _x4, _x5) {
321
- return _ref2.apply(this, arguments);
322
- };
323
- }();
324
- var sendEmailVerificationAPI = /*#__PURE__*/function () {
325
- var _ref3 = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(domainName, token) {
326
- var params, response, data;
327
- return _regenerator().w(function (_context3) {
328
- while (1) switch (_context3.n) {
329
- case 0:
330
- params = {
331
- method: 'GET',
332
- headers: {
333
- Authorization: token,
334
- 'Content-Type': 'application/json'
335
- }
336
- };
337
- _context3.n = 1;
338
- return fetch(getServerBasePath() + "/app/" + domainName + "/resend-email-verification", params);
339
- case 1:
340
- response = _context3.v;
341
- _context3.n = 2;
342
- return response.json();
343
- case 2:
344
- data = _context3.v;
345
- return _context3.a(2, {
346
- response: response,
347
- data: data
348
- });
349
- }
350
- }, _callee3);
351
- }));
352
- return function sendEmailVerificationAPI(_x6, _x7) {
353
- return _ref3.apply(this, arguments);
354
- };
355
- }();
356
- var refreshTokenAPI = /*#__PURE__*/function () {
357
- var _ref5 = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(domainName, refreshToken) {
358
- var params, response, data;
359
- return _regenerator().w(function (_context5) {
360
- while (1) switch (_context5.n) {
361
- case 0:
362
- params = {
363
- method: 'POST',
364
- headers: {
365
- 'Content-Type': 'application/json'
366
- },
367
- body: JSON.stringify({
368
- refreshToken: refreshToken
369
- })
370
- };
371
- _context5.n = 1;
372
- return fetch(getServerBasePath() + "/app/" + domainName + "/refresh-token", params);
373
- case 1:
374
- response = _context5.v;
375
- _context5.n = 2;
376
- return response.json();
377
- case 2:
378
- data = _context5.v;
379
- return _context5.a(2, {
380
- response: response,
381
- data: data
382
- });
383
- }
384
- }, _callee5);
385
- }));
386
- return function refreshTokenAPI(_x0, _x1) {
387
- return _ref5.apply(this, arguments);
388
- };
389
- }();
390
- var deleteAccountAPI = /*#__PURE__*/function () {
391
- var _ref6 = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(domainName, token) {
392
- var params, response, data;
393
- return _regenerator().w(function (_context6) {
394
- while (1) switch (_context6.n) {
395
- case 0:
396
- params = {
397
- method: 'DELETE',
398
- headers: {
399
- Authorization: token,
400
- 'Content-Type': 'application/json'
401
- }
402
- };
403
- _context6.n = 1;
404
- return fetch(getServerBasePath() + "/app/" + domainName + "/user", params);
405
- case 1:
406
- response = _context6.v;
407
- _context6.n = 2;
408
- return response.json();
409
- case 2:
410
- data = _context6.v;
411
- return _context6.a(2, {
412
- response: response,
413
- data: data
414
- });
415
- }
416
- }, _callee6);
417
- }));
418
- return function deleteAccountAPI(_x10, _x11) {
419
- return _ref6.apply(this, arguments);
420
- };
421
- }();
422
- var logoutAPI = /*#__PURE__*/function () {
423
- var _ref7 = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(domainName, refreshToken) {
424
- var params, response;
425
- return _regenerator().w(function (_context7) {
426
- while (1) switch (_context7.n) {
427
- case 0:
428
- params = {
429
- method: 'POST',
430
- headers: {
431
- 'Content-Type': 'application/json'
432
- },
433
- body: JSON.stringify({
434
- refreshToken: refreshToken
435
- })
436
- };
437
- _context7.n = 1;
438
- return fetch(getServerBasePath() + "/app/" + domainName + "/logout", params);
439
- case 1:
440
- response = _context7.v;
441
- return _context7.a(2, {
442
- response: response
443
- });
444
- }
445
- }, _callee7);
446
- }));
447
- return function logoutAPI(_x12, _x13) {
448
- return _ref7.apply(this, arguments);
449
- };
450
- }();
451
-
452
- var TOKEN_LS = 'dauth_state';
453
- var REFRESH_TOKEN_LS = 'dauth_refresh_token';
454
-
455
- function setDauthStateAction(_x) {
456
- return _setDauthStateAction.apply(this, arguments);
457
- }
458
- function _setDauthStateAction() {
459
- _setDauthStateAction = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(_ref) {
460
- var dispatch, token, refreshToken, domainName, getUserFetch, _t;
461
- return _regenerator().w(function (_context) {
462
- while (1) switch (_context.p = _context.n) {
463
- case 0:
464
- dispatch = _ref.dispatch, token = _ref.token, refreshToken = _ref.refreshToken, domainName = _ref.domainName;
465
- dispatch({
466
- type: SET_IS_LOADING,
467
- payload: {
468
- isLoading: true
469
- }
470
- });
471
- _context.p = 1;
472
- _context.n = 2;
473
- return getUserAPI(domainName, token);
474
- case 2:
475
- getUserFetch = _context.v;
476
- if (!(getUserFetch.response.status === 200)) {
477
- _context.n = 3;
478
- break;
479
- }
480
- dispatch({
481
- type: LOGIN,
482
- payload: {
483
- user: getUserFetch.data.user,
484
- domain: getUserFetch.data.domain,
485
- isAuthenticated: true
486
- }
487
- });
488
- window.history.replaceState({}, document.title, window.location.pathname);
489
- localStorage.setItem(TOKEN_LS, token);
490
- localStorage.setItem(REFRESH_TOKEN_LS, refreshToken);
491
- return _context.a(2);
492
- case 3:
493
- return _context.a(2, resetUser(dispatch));
494
- case 4:
495
- _context.n = 6;
496
- break;
497
- case 5:
498
- _context.p = 5;
499
- _t = _context.v;
500
- console.error(_t);
501
- return _context.a(2, resetUser(dispatch));
502
- case 6:
503
- _context.p = 6;
504
- dispatch({
505
- type: SET_IS_LOADING,
506
- payload: {
507
- isLoading: false
508
- }
509
- });
510
- return _context.f(6);
511
- case 7:
512
- return _context.a(2);
513
- }
514
- }, _callee, null, [[1, 5, 6, 7]]);
515
- }));
516
- return _setDauthStateAction.apply(this, arguments);
517
- }
518
- function setAutoLoginAction(_x2) {
519
- return _setAutoLoginAction.apply(this, arguments);
520
- }
521
- function _setAutoLoginAction() {
522
- _setAutoLoginAction = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(_ref2) {
523
- var dispatch, domainName, storedRefreshToken, refreshResult, newAccessToken, newRefreshToken, getUserFetch, _t2;
524
- return _regenerator().w(function (_context2) {
525
- while (1) switch (_context2.p = _context2.n) {
526
- case 0:
527
- dispatch = _ref2.dispatch, domainName = _ref2.domainName;
528
- dispatch({
529
- type: SET_IS_LOADING,
530
- payload: {
531
- isLoading: true
532
- }
533
- });
534
- storedRefreshToken = localStorage.getItem(REFRESH_TOKEN_LS);
535
- if (storedRefreshToken) {
536
- _context2.n = 1;
537
- break;
538
- }
539
- dispatch({
540
- type: SET_IS_LOADING,
541
- payload: {
542
- isLoading: false
543
- }
544
- });
545
- return _context2.a(2, resetUser(dispatch));
546
- case 1:
547
- _context2.p = 1;
548
- _context2.n = 2;
549
- return refreshTokenAPI(domainName, storedRefreshToken);
550
- case 2:
551
- refreshResult = _context2.v;
552
- if (!(refreshResult.response.status === 200)) {
553
- _context2.n = 4;
554
- break;
555
- }
556
- newAccessToken = refreshResult.data.accessToken;
557
- newRefreshToken = refreshResult.data.refreshToken;
558
- localStorage.setItem(TOKEN_LS, newAccessToken);
559
- localStorage.setItem(REFRESH_TOKEN_LS, newRefreshToken);
560
- _context2.n = 3;
561
- return getUserAPI(domainName, newAccessToken);
562
- case 3:
563
- getUserFetch = _context2.v;
564
- if (!(getUserFetch.response.status === 200)) {
565
- _context2.n = 4;
566
- break;
567
- }
568
- dispatch({
569
- type: LOGIN,
570
- payload: {
571
- user: getUserFetch.data.user,
572
- domain: getUserFetch.data.domain,
573
- isAuthenticated: true
574
- }
575
- });
576
- return _context2.a(2);
577
- case 4:
578
- // Refresh failed — session expired
579
- resetUser(dispatch);
580
- _context2.n = 6;
581
- break;
582
- case 5:
583
- _context2.p = 5;
584
- _t2 = _context2.v;
585
- console.error(_t2);
586
- resetUser(dispatch);
587
- case 6:
588
- _context2.p = 6;
589
- dispatch({
590
- type: SET_IS_LOADING,
591
- payload: {
592
- isLoading: false
593
- }
594
- });
595
- return _context2.f(6);
596
- case 7:
597
- return _context2.a(2);
598
- }
599
- }, _callee2, null, [[1, 5, 6, 7]]);
600
- }));
601
- return _setAutoLoginAction.apply(this, arguments);
602
- }
603
- function setLogoutAction(_x3) {
604
- return _setLogoutAction.apply(this, arguments);
605
- }
606
- function _setLogoutAction() {
607
- _setLogoutAction = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(_ref3) {
608
- var dispatch, domainName, storedRefreshToken;
609
- return _regenerator().w(function (_context3) {
610
- while (1) switch (_context3.p = _context3.n) {
611
- case 0:
612
- dispatch = _ref3.dispatch, domainName = _ref3.domainName;
613
- storedRefreshToken = localStorage.getItem(REFRESH_TOKEN_LS);
614
- if (!(storedRefreshToken && domainName)) {
615
- _context3.n = 4;
616
- break;
617
- }
618
- _context3.p = 1;
619
- _context3.n = 2;
620
- return logoutAPI(domainName, storedRefreshToken);
621
- case 2:
622
- _context3.n = 4;
623
- break;
624
- case 3:
625
- _context3.p = 3;
626
- case 4:
627
- dispatch({
628
- type: SET_IS_LOADING,
629
- payload: {
630
- isLoading: true
631
- }
632
- });
633
- dispatch({
634
- type: LOGIN,
635
- payload: {
636
- user: {
637
- language: window.document.documentElement.getAttribute('lang') || 'es'
638
- },
639
- domain: {},
640
- isAuthenticated: false
641
- }
642
- });
643
- localStorage.removeItem(TOKEN_LS);
644
- localStorage.removeItem(REFRESH_TOKEN_LS);
645
- return _context3.a(2, dispatch({
646
- type: SET_IS_LOADING,
647
- payload: {
648
- isLoading: false
649
- }
650
- }));
651
- }
652
- }, _callee3, null, [[1, 3]]);
653
- }));
654
- return _setLogoutAction.apply(this, arguments);
655
- }
656
- function refreshSessionAction(_x4) {
657
- return _refreshSessionAction.apply(this, arguments);
658
- }
659
- function _refreshSessionAction() {
660
- _refreshSessionAction = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(_ref4) {
661
- var dispatch, domainName, storedRefreshToken, refreshResult, _t4;
662
- return _regenerator().w(function (_context4) {
663
- while (1) switch (_context4.p = _context4.n) {
664
- case 0:
665
- dispatch = _ref4.dispatch, domainName = _ref4.domainName;
666
- storedRefreshToken = localStorage.getItem(REFRESH_TOKEN_LS);
667
- if (storedRefreshToken) {
668
- _context4.n = 1;
669
- break;
670
- }
671
- return _context4.a(2, resetUser(dispatch));
672
- case 1:
673
- _context4.p = 1;
674
- _context4.n = 2;
675
- return refreshTokenAPI(domainName, storedRefreshToken);
676
- case 2:
677
- refreshResult = _context4.v;
678
- if (!(refreshResult.response.status === 200)) {
679
- _context4.n = 3;
680
- break;
681
- }
682
- localStorage.setItem(TOKEN_LS, refreshResult.data.accessToken);
683
- localStorage.setItem(REFRESH_TOKEN_LS, refreshResult.data.refreshToken);
684
- return _context4.a(2);
685
- case 3:
686
- // Refresh failed — revoked or expired
687
- resetUser(dispatch);
688
- _context4.n = 5;
689
- break;
690
- case 4:
691
- _context4.p = 4;
692
- _t4 = _context4.v;
693
- console.error(_t4);
694
- resetUser(dispatch);
695
- case 5:
696
- return _context4.a(2);
697
- }
698
- }, _callee4, null, [[1, 4]]);
699
- }));
700
- return _refreshSessionAction.apply(this, arguments);
701
- }
702
- function setUpdateUserAction(_x5) {
703
- return _setUpdateUserAction.apply(this, arguments);
704
- }
705
- function _setUpdateUserAction() {
706
- _setUpdateUserAction = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(_ref5) {
707
- var dispatch, domainName, user, token, getUserFetch, _t5;
708
- return _regenerator().w(function (_context5) {
709
- while (1) switch (_context5.p = _context5.n) {
710
- case 0:
711
- dispatch = _ref5.dispatch, domainName = _ref5.domainName, user = _ref5.user, token = _ref5.token;
712
- if (user.language) {
713
- window.document.documentElement.setAttribute('lang', user.language);
714
- }
715
- if (token) {
716
- _context5.n = 1;
717
- break;
718
- }
719
- dispatch({
720
- type: UPDATE_USER,
721
- payload: user
722
- });
723
- return _context5.a(2, false);
724
- case 1:
725
- _context5.p = 1;
726
- _context5.n = 2;
727
- return updateUserAPI(domainName, user, token);
728
- case 2:
729
- getUserFetch = _context5.v;
730
- if (!(getUserFetch.response.status === 200)) {
731
- _context5.n = 3;
732
- break;
733
- }
734
- dispatch({
735
- type: UPDATE_USER,
736
- payload: getUserFetch.data.user
737
- });
738
- return _context5.a(2, true);
739
- case 3:
740
- console.error('Update user error', getUserFetch.data.message);
741
- return _context5.a(2, false);
742
- case 4:
743
- _context5.n = 6;
744
- break;
745
- case 5:
746
- _context5.p = 5;
747
- _t5 = _context5.v;
748
- console.error('Update user error', _t5);
749
- return _context5.a(2, false);
750
- case 6:
751
- return _context5.a(2);
752
- }
753
- }, _callee5, null, [[1, 5]]);
754
- }));
755
- return _setUpdateUserAction.apply(this, arguments);
756
- }
757
- function sendEmailVerificationAction(_x6) {
758
- return _sendEmailVerificationAction.apply(this, arguments);
759
- }
760
- function _sendEmailVerificationAction() {
761
- _sendEmailVerificationAction = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(_ref6) {
762
- var dispatch, domainName, token, sendEmailFetch;
763
- return _regenerator().w(function (_context6) {
764
- while (1) switch (_context6.p = _context6.n) {
765
- case 0:
766
- dispatch = _ref6.dispatch, domainName = _ref6.domainName, token = _ref6.token;
767
- dispatch({
768
- type: SET_SEND_EMAIL_VERIFICATION_IS_LOADING,
769
- payload: true
770
- });
771
- dispatch({
772
- type: SET_SEND_EMAIL_VERIFICATION_STATUS,
773
- payload: {
774
- type: 'info',
775
- message: 'Sending email verification...'
776
- }
777
- });
778
- _context6.p = 1;
779
- _context6.n = 2;
780
- return sendEmailVerificationAPI(domainName, token);
781
- case 2:
782
- sendEmailFetch = _context6.v;
783
- if (!(sendEmailFetch.response.status === 200)) {
784
- _context6.n = 3;
785
- break;
786
- }
787
- dispatch({
788
- type: SET_SEND_EMAIL_VERIFICATION_STATUS,
789
- payload: {
790
- type: 'success',
791
- message: sendEmailFetch.data.message
792
- }
793
- });
794
- dispatch({
795
- type: SET_SEND_EMAIL_VERIFICATION_IS_LOADING,
796
- payload: false
797
- });
798
- return _context6.a(2, true);
799
- case 3:
800
- dispatch({
801
- type: SET_SEND_EMAIL_VERIFICATION_STATUS,
802
- payload: {
803
- type: 'error',
804
- message: sendEmailFetch.data.message
805
- }
806
- });
807
- dispatch({
808
- type: SET_SEND_EMAIL_VERIFICATION_IS_LOADING,
809
- payload: false
810
- });
811
- return _context6.a(2, false);
812
- case 4:
813
- _context6.n = 6;
814
- break;
815
- case 5:
816
- _context6.p = 5;
817
- dispatch({
818
- type: SET_SEND_EMAIL_VERIFICATION_STATUS,
819
- payload: {
820
- type: 'error',
821
- message: 'Send email verification fetch error'
822
- }
823
- });
824
- dispatch({
825
- type: SET_SEND_EMAIL_VERIFICATION_IS_LOADING,
826
- payload: false
827
- });
828
- return _context6.a(2, false);
829
- case 6:
830
- return _context6.a(2);
831
- }
832
- }, _callee6, null, [[1, 5]]);
833
- }));
834
- return _sendEmailVerificationAction.apply(this, arguments);
835
- }
836
- function getAccessTokenAction(_x7) {
837
- return _getAccessTokenAction.apply(this, arguments);
838
- }
839
- function _getAccessTokenAction() {
840
- _getAccessTokenAction = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(_ref7) {
841
- var dispatch, domainName, token_ls, payloadB64, payload, expiresIn, refreshedToken;
842
- return _regenerator().w(function (_context7) {
843
- while (1) switch (_context7.p = _context7.n) {
844
- case 0:
845
- dispatch = _ref7.dispatch, domainName = _ref7.domainName;
846
- token_ls = localStorage.getItem(TOKEN_LS);
847
- if (token_ls) {
848
- _context7.n = 1;
849
- break;
850
- }
851
- return _context7.a(2, 'token-not-found');
852
- case 1:
853
- _context7.p = 1;
854
- payloadB64 = token_ls.split('.')[1];
855
- if (!payloadB64) {
856
- _context7.n = 3;
857
- break;
858
- }
859
- payload = JSON.parse(atob(payloadB64));
860
- expiresIn = (payload.exp || 0) * 1000 - Date.now(); // If token expires in less than 5 minutes, refresh proactively
861
- if (!(expiresIn < 5 * 60 * 1000)) {
862
- _context7.n = 3;
863
- break;
864
- }
865
- _context7.n = 2;
866
- return refreshSessionAction({
867
- dispatch: dispatch,
868
- domainName: domainName
869
- });
870
- case 2:
871
- refreshedToken = localStorage.getItem(TOKEN_LS);
872
- return _context7.a(2, refreshedToken || 'token-not-found');
873
- case 3:
874
- _context7.n = 5;
875
- break;
876
- case 4:
877
- _context7.p = 4;
878
- case 5:
879
- return _context7.a(2, token_ls);
880
- }
881
- }, _callee7, null, [[1, 4]]);
882
- }));
883
- return _getAccessTokenAction.apply(this, arguments);
884
- }
885
- function deleteAccountAction(_x8) {
886
- return _deleteAccountAction.apply(this, arguments);
887
- }
888
- ///////////////////////////////////////////
889
- //////////////////////////////////////////
890
- function _deleteAccountAction() {
891
- _deleteAccountAction = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(_ref8) {
892
- var dispatch, domainName, token, result, _t8;
893
- return _regenerator().w(function (_context8) {
894
- while (1) switch (_context8.p = _context8.n) {
895
- case 0:
896
- dispatch = _ref8.dispatch, domainName = _ref8.domainName, token = _ref8.token;
897
- _context8.p = 1;
898
- _context8.n = 2;
899
- return deleteAccountAPI(domainName, token);
900
- case 2:
901
- result = _context8.v;
902
- if (!(result.response.status === 200)) {
903
- _context8.n = 3;
904
- break;
905
- }
906
- resetUser(dispatch);
907
- return _context8.a(2, true);
908
- case 3:
909
- return _context8.a(2, false);
910
- case 4:
911
- _context8.p = 4;
912
- _t8 = _context8.v;
913
- console.error('Delete account error', _t8);
914
- return _context8.a(2, false);
915
- }
916
- }, _callee8, null, [[1, 4]]);
917
- }));
918
- return _deleteAccountAction.apply(this, arguments);
919
- }
920
- var resetUser = function resetUser(dispatch) {
921
- localStorage.removeItem(TOKEN_LS);
922
- localStorage.removeItem(REFRESH_TOKEN_LS);
923
- return dispatch({
924
- type: LOGIN,
925
- payload: {
926
- user: {},
927
- domain: {},
928
- isAuthenticated: false
929
- }
930
- });
931
- };
932
-
933
- var routes = {
934
- signin: 'signin',
935
- updateUser: 'update-user'
936
- };
937
-
938
- var DauthProvider = function DauthProvider(props) {
939
- var domainName = props.domainName,
940
- children = props.children;
941
- var _useReducer = useReducer(userReducer, initialDauthState),
942
- dauthState = _useReducer[0],
943
- dispatch = _useReducer[1];
944
- var refreshTimerRef = useRef(null);
945
- // Schedule next proactive refresh based on access token expiry
946
- var scheduleRefresh = useCallback(function () {
947
- if (refreshTimerRef.current) clearTimeout(refreshTimerRef.current);
948
- var token = localStorage.getItem(TOKEN_LS);
949
- if (!token) return;
950
- try {
951
- var payloadB64 = token.split('.')[1];
952
- if (!payloadB64) return;
953
- var payload = JSON.parse(atob(payloadB64));
954
- var expiresIn = (payload.exp || 0) * 1000 - Date.now();
955
- // Refresh 5 minutes before expiry, minimum 10 seconds
956
- var refreshIn = Math.max(expiresIn - 5 * 60 * 1000, 10000);
957
- refreshTimerRef.current = setTimeout(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
958
- return _regenerator().w(function (_context) {
959
- while (1) switch (_context.n) {
960
- case 0:
961
- _context.n = 1;
962
- return refreshSessionAction({
963
- dispatch: dispatch,
964
- domainName: domainName
965
- });
966
- case 1:
967
- scheduleRefresh();
968
- case 2:
969
- return _context.a(2);
970
- }
971
- }, _callee);
972
- })), refreshIn);
973
- } catch (_) {
974
- // If decode fails, retry in 5 minutes
975
- refreshTimerRef.current = setTimeout(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
976
- return _regenerator().w(function (_context2) {
977
- while (1) switch (_context2.n) {
978
- case 0:
979
- _context2.n = 1;
980
- return refreshSessionAction({
981
- dispatch: dispatch,
982
- domainName: domainName
983
- });
984
- case 1:
985
- scheduleRefresh();
986
- case 2:
987
- return _context2.a(2);
988
- }
989
- }, _callee2);
990
- })), 5 * 60 * 1000);
991
- }
992
- }, [domainName]);
993
- // Catch login redirect
994
- useEffect(function () {
995
- _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() {
996
- var queryString, urlParams, token_url, refresh_url;
997
- return _regenerator().w(function (_context3) {
998
- while (1) switch (_context3.n) {
999
- case 0:
1000
- queryString = window.location.search;
1001
- if (queryString) {
1002
- _context3.n = 1;
1003
- break;
1004
- }
1005
- return _context3.a(2);
1006
- case 1:
1007
- urlParams = new URLSearchParams(queryString);
1008
- token_url = urlParams.get(TOKEN_LS);
1009
- refresh_url = urlParams.get(REFRESH_TOKEN_LS);
1010
- if (!(token_url && refresh_url && !dauthState.isAuthenticated)) {
1011
- _context3.n = 2;
1012
- break;
1013
- }
1014
- return _context3.a(2, setDauthStateAction({
1015
- dispatch: dispatch,
1016
- token: token_url,
1017
- refreshToken: refresh_url,
1018
- domainName: domainName
1019
- }));
1020
- case 2:
1021
- return _context3.a(2);
1022
- }
1023
- }, _callee3);
1024
- }))();
1025
- }, []);
1026
- // Auto Login
1027
- useEffect(function () {
1028
- _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4() {
1029
- var refreshToken;
1030
- return _regenerator().w(function (_context4) {
1031
- while (1) switch (_context4.n) {
1032
- case 0:
1033
- refreshToken = localStorage.getItem(REFRESH_TOKEN_LS);
1034
- if (!(refreshToken && !dauthState.isAuthenticated)) {
1035
- _context4.n = 1;
1036
- break;
1037
- }
1038
- return _context4.a(2, setAutoLoginAction({
1039
- dispatch: dispatch,
1040
- domainName: domainName
1041
- }));
1042
- case 1:
1043
- return _context4.a(2, dispatch({
1044
- type: SET_IS_LOADING,
1045
- payload: {
1046
- isLoading: false
1047
- }
1048
- }));
1049
- case 2:
1050
- return _context4.a(2);
1051
- }
1052
- }, _callee4);
1053
- }))();
1054
- }, []);
1055
- // Schedule proactive refresh when authenticated
1056
- useEffect(function () {
1057
- if (dauthState.isAuthenticated) {
1058
- scheduleRefresh();
1059
- }
1060
- return function () {
1061
- if (refreshTimerRef.current) clearTimeout(refreshTimerRef.current);
1062
- };
1063
- }, [dauthState.isAuthenticated, scheduleRefresh]);
1064
- var loginWithRedirect = useCallback(function () {
1065
- return window.location.replace(getClientBasePath() + "/" + domainName + "/" + routes.signin);
1066
- }, [domainName]);
1067
- var logout = useCallback(function () {
1068
- if (refreshTimerRef.current) clearTimeout(refreshTimerRef.current);
1069
- return setLogoutAction({
1070
- dispatch: dispatch,
1071
- domainName: domainName
1072
- });
1073
- }, [domainName]);
1074
- var getAccessToken = useCallback(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5() {
1075
- var token;
1076
- return _regenerator().w(function (_context5) {
1077
- while (1) switch (_context5.n) {
1078
- case 0:
1079
- _context5.n = 1;
1080
- return getAccessTokenAction({
1081
- dispatch: dispatch,
1082
- domainName: domainName
1083
- });
1084
- case 1:
1085
- token = _context5.v;
1086
- return _context5.a(2, token);
1087
- }
1088
- }, _callee5);
1089
- })), [domainName]);
1090
- var updateUser = useCallback(/*#__PURE__*/function () {
1091
- var _ref6 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(fields) {
1092
- var token_ls, name, lastname, nickname, telPrefix, telSuffix, language, avatar, birthDate, country, metadata, user;
1093
- return _regenerator().w(function (_context6) {
1094
- while (1) switch (_context6.n) {
1095
- case 0:
1096
- token_ls = localStorage.getItem(TOKEN_LS);
1097
- 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;
1098
- user = {
1099
- name: name,
1100
- lastname: lastname,
1101
- nickname: nickname,
1102
- telPrefix: telPrefix,
1103
- telSuffix: telSuffix,
1104
- language: language,
1105
- avatar: avatar,
1106
- birthDate: birthDate,
1107
- country: country,
1108
- metadata: metadata
1109
- };
1110
- _context6.n = 1;
1111
- return setUpdateUserAction({
1112
- dispatch: dispatch,
1113
- domainName: domainName,
1114
- user: user,
1115
- token: token_ls
1116
- });
1117
- case 1:
1118
- return _context6.a(2, _context6.v);
1119
- }
1120
- }, _callee6);
1121
- }));
1122
- return function (_x) {
1123
- return _ref6.apply(this, arguments);
1124
- };
1125
- }(), [domainName]);
1126
- var updateUserWithRedirect = useCallback(function () {
1127
- var token_ls = localStorage.getItem(TOKEN_LS);
1128
- if (!token_ls) return;
1129
- return window.location.replace(getClientBasePath() + "/" + domainName + "/" + routes.updateUser + "/" + token_ls);
1130
- }, [domainName]);
1131
- var sendEmailVerification = useCallback(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7() {
1132
- var token_ls;
1133
- return _regenerator().w(function (_context7) {
1134
- while (1) switch (_context7.n) {
1135
- case 0:
1136
- token_ls = localStorage.getItem(TOKEN_LS);
1137
- if (token_ls) {
1138
- _context7.n = 1;
1139
- break;
1140
- }
1141
- return _context7.a(2, false);
1142
- case 1:
1143
- _context7.n = 2;
1144
- return sendEmailVerificationAction({
1145
- dispatch: dispatch,
1146
- domainName: domainName,
1147
- token: token_ls
1148
- });
1149
- case 2:
1150
- return _context7.a(2, _context7.v);
1151
- }
1152
- }, _callee7);
1153
- })), [domainName]);
1154
- var deleteAccount = useCallback(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8() {
1155
- var token_ls;
1156
- return _regenerator().w(function (_context8) {
1157
- while (1) switch (_context8.n) {
1158
- case 0:
1159
- token_ls = localStorage.getItem(TOKEN_LS);
1160
- if (token_ls) {
1161
- _context8.n = 1;
1162
- break;
1163
- }
1164
- return _context8.a(2, false);
1165
- case 1:
1166
- if (refreshTimerRef.current) clearTimeout(refreshTimerRef.current);
1167
- _context8.n = 2;
1168
- return deleteAccountAction({
1169
- dispatch: dispatch,
1170
- domainName: domainName,
1171
- token: token_ls
1172
- });
1173
- case 2:
1174
- return _context8.a(2, _context8.v);
1175
- }
1176
- }, _callee8);
1177
- })), [domainName]);
1178
- var memoProvider = useMemo(function () {
1179
- return _extends({}, dauthState, {
1180
- loginWithRedirect: loginWithRedirect,
1181
- logout: logout,
1182
- getAccessToken: getAccessToken,
1183
- updateUser: updateUser,
1184
- updateUserWithRedirect: updateUserWithRedirect,
1185
- sendEmailVerification: sendEmailVerification,
1186
- deleteAccount: deleteAccount
1187
- });
1188
- }, [dauthState, loginWithRedirect, logout, getAccessToken, updateUser, updateUserWithRedirect, sendEmailVerification, deleteAccount]);
1189
- return React.createElement(DauthContext.Provider, {
1190
- value: memoProvider
1191
- }, children);
1192
- };
1193
- var DauthContext = /*#__PURE__*/createContext(initialDauthState);
1194
- var useDauth = function useDauth() {
1195
- var context = useContext(DauthContext);
1196
- if (!context) {
1197
- throw new Error('useDauth must be used inside DauthProvider');
1198
- }
1199
- return context;
1200
- };
1201
-
1202
- export { DauthProvider, useDauth };
1203
- //# sourceMappingURL=dauth-context-react.esm.js.map