dauth-context-react 0.1.0 → 0.1.1

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,9 +1,717 @@
1
- import { createElement } from 'react';
1
+ import { createElement, useReducer, useEffect, useCallback, useMemo, createContext, useContext } from 'react';
2
+
3
+ function _regeneratorRuntime() {
4
+ _regeneratorRuntime = function () {
5
+ return e;
6
+ };
7
+ var t,
8
+ e = {},
9
+ r = Object.prototype,
10
+ n = r.hasOwnProperty,
11
+ o = Object.defineProperty || function (t, e, r) {
12
+ t[e] = r.value;
13
+ },
14
+ i = "function" == typeof Symbol ? Symbol : {},
15
+ a = i.iterator || "@@iterator",
16
+ c = i.asyncIterator || "@@asyncIterator",
17
+ u = i.toStringTag || "@@toStringTag";
18
+ function define(t, e, r) {
19
+ return Object.defineProperty(t, e, {
20
+ value: r,
21
+ enumerable: !0,
22
+ configurable: !0,
23
+ writable: !0
24
+ }), t[e];
25
+ }
26
+ try {
27
+ define({}, "");
28
+ } catch (t) {
29
+ define = function (t, e, r) {
30
+ return t[e] = r;
31
+ };
32
+ }
33
+ function wrap(t, e, r, n) {
34
+ var i = e && e.prototype instanceof Generator ? e : Generator,
35
+ a = Object.create(i.prototype),
36
+ c = new Context(n || []);
37
+ return o(a, "_invoke", {
38
+ value: makeInvokeMethod(t, r, c)
39
+ }), a;
40
+ }
41
+ function tryCatch(t, e, r) {
42
+ try {
43
+ return {
44
+ type: "normal",
45
+ arg: t.call(e, r)
46
+ };
47
+ } catch (t) {
48
+ return {
49
+ type: "throw",
50
+ arg: t
51
+ };
52
+ }
53
+ }
54
+ e.wrap = wrap;
55
+ var h = "suspendedStart",
56
+ l = "suspendedYield",
57
+ f = "executing",
58
+ s = "completed",
59
+ y = {};
60
+ function Generator() {}
61
+ function GeneratorFunction() {}
62
+ function GeneratorFunctionPrototype() {}
63
+ var p = {};
64
+ define(p, a, function () {
65
+ return this;
66
+ });
67
+ var d = Object.getPrototypeOf,
68
+ v = d && d(d(values([])));
69
+ v && v !== r && n.call(v, a) && (p = v);
70
+ var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p);
71
+ function defineIteratorMethods(t) {
72
+ ["next", "throw", "return"].forEach(function (e) {
73
+ define(t, e, function (t) {
74
+ return this._invoke(e, t);
75
+ });
76
+ });
77
+ }
78
+ function AsyncIterator(t, e) {
79
+ function invoke(r, o, i, a) {
80
+ var c = tryCatch(t[r], t, o);
81
+ if ("throw" !== c.type) {
82
+ var u = c.arg,
83
+ h = u.value;
84
+ return h && "object" == typeof h && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) {
85
+ invoke("next", t, i, a);
86
+ }, function (t) {
87
+ invoke("throw", t, i, a);
88
+ }) : e.resolve(h).then(function (t) {
89
+ u.value = t, i(u);
90
+ }, function (t) {
91
+ return invoke("throw", t, i, a);
92
+ });
93
+ }
94
+ a(c.arg);
95
+ }
96
+ var r;
97
+ o(this, "_invoke", {
98
+ value: function (t, n) {
99
+ function callInvokeWithMethodAndArg() {
100
+ return new e(function (e, r) {
101
+ invoke(t, n, e, r);
102
+ });
103
+ }
104
+ return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
105
+ }
106
+ });
107
+ }
108
+ function makeInvokeMethod(e, r, n) {
109
+ var o = h;
110
+ return function (i, a) {
111
+ if (o === f) throw new Error("Generator is already running");
112
+ if (o === s) {
113
+ if ("throw" === i) throw a;
114
+ return {
115
+ value: t,
116
+ done: !0
117
+ };
118
+ }
119
+ for (n.method = i, n.arg = a;;) {
120
+ var c = n.delegate;
121
+ if (c) {
122
+ var u = maybeInvokeDelegate(c, n);
123
+ if (u) {
124
+ if (u === y) continue;
125
+ return u;
126
+ }
127
+ }
128
+ if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) {
129
+ if (o === h) throw o = s, n.arg;
130
+ n.dispatchException(n.arg);
131
+ } else "return" === n.method && n.abrupt("return", n.arg);
132
+ o = f;
133
+ var p = tryCatch(e, r, n);
134
+ if ("normal" === p.type) {
135
+ if (o = n.done ? s : l, p.arg === y) continue;
136
+ return {
137
+ value: p.arg,
138
+ done: n.done
139
+ };
140
+ }
141
+ "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg);
142
+ }
143
+ };
144
+ }
145
+ function maybeInvokeDelegate(e, r) {
146
+ var n = r.method,
147
+ o = e.iterator[n];
148
+ if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y;
149
+ var i = tryCatch(o, e.iterator, r.arg);
150
+ if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y;
151
+ var a = i.arg;
152
+ return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y);
153
+ }
154
+ function pushTryEntry(t) {
155
+ var e = {
156
+ tryLoc: t[0]
157
+ };
158
+ 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e);
159
+ }
160
+ function resetTryEntry(t) {
161
+ var e = t.completion || {};
162
+ e.type = "normal", delete e.arg, t.completion = e;
163
+ }
164
+ function Context(t) {
165
+ this.tryEntries = [{
166
+ tryLoc: "root"
167
+ }], t.forEach(pushTryEntry, this), this.reset(!0);
168
+ }
169
+ function values(e) {
170
+ if (e || "" === e) {
171
+ var r = e[a];
172
+ if (r) return r.call(e);
173
+ if ("function" == typeof e.next) return e;
174
+ if (!isNaN(e.length)) {
175
+ var o = -1,
176
+ i = function next() {
177
+ for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next;
178
+ return next.value = t, next.done = !0, next;
179
+ };
180
+ return i.next = i;
181
+ }
182
+ }
183
+ throw new TypeError(typeof e + " is not iterable");
184
+ }
185
+ return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", {
186
+ value: GeneratorFunctionPrototype,
187
+ configurable: !0
188
+ }), o(GeneratorFunctionPrototype, "constructor", {
189
+ value: GeneratorFunction,
190
+ configurable: !0
191
+ }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) {
192
+ var e = "function" == typeof t && t.constructor;
193
+ return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name));
194
+ }, e.mark = function (t) {
195
+ return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t;
196
+ }, e.awrap = function (t) {
197
+ return {
198
+ __await: t
199
+ };
200
+ }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () {
201
+ return this;
202
+ }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) {
203
+ void 0 === i && (i = Promise);
204
+ var a = new AsyncIterator(wrap(t, r, n, o), i);
205
+ return e.isGeneratorFunction(r) ? a : a.next().then(function (t) {
206
+ return t.done ? t.value : a.next();
207
+ });
208
+ }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () {
209
+ return this;
210
+ }), define(g, "toString", function () {
211
+ return "[object Generator]";
212
+ }), e.keys = function (t) {
213
+ var e = Object(t),
214
+ r = [];
215
+ for (var n in e) r.push(n);
216
+ return r.reverse(), function next() {
217
+ for (; r.length;) {
218
+ var t = r.pop();
219
+ if (t in e) return next.value = t, next.done = !1, next;
220
+ }
221
+ return next.done = !0, next;
222
+ };
223
+ }, e.values = values, Context.prototype = {
224
+ constructor: Context,
225
+ reset: function (e) {
226
+ if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t);
227
+ },
228
+ stop: function () {
229
+ this.done = !0;
230
+ var t = this.tryEntries[0].completion;
231
+ if ("throw" === t.type) throw t.arg;
232
+ return this.rval;
233
+ },
234
+ dispatchException: function (e) {
235
+ if (this.done) throw e;
236
+ var r = this;
237
+ function handle(n, o) {
238
+ return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o;
239
+ }
240
+ for (var o = this.tryEntries.length - 1; o >= 0; --o) {
241
+ var i = this.tryEntries[o],
242
+ a = i.completion;
243
+ if ("root" === i.tryLoc) return handle("end");
244
+ if (i.tryLoc <= this.prev) {
245
+ var c = n.call(i, "catchLoc"),
246
+ u = n.call(i, "finallyLoc");
247
+ if (c && u) {
248
+ if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
249
+ if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
250
+ } else if (c) {
251
+ if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
252
+ } else {
253
+ if (!u) throw new Error("try statement without catch or finally");
254
+ if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
255
+ }
256
+ }
257
+ }
258
+ },
259
+ abrupt: function (t, e) {
260
+ for (var r = this.tryEntries.length - 1; r >= 0; --r) {
261
+ var o = this.tryEntries[r];
262
+ if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) {
263
+ var i = o;
264
+ break;
265
+ }
266
+ }
267
+ i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null);
268
+ var a = i ? i.completion : {};
269
+ return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a);
270
+ },
271
+ complete: function (t, e) {
272
+ if ("throw" === t.type) throw t.arg;
273
+ return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y;
274
+ },
275
+ finish: function (t) {
276
+ for (var e = this.tryEntries.length - 1; e >= 0; --e) {
277
+ var r = this.tryEntries[e];
278
+ if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y;
279
+ }
280
+ },
281
+ catch: function (t) {
282
+ for (var e = this.tryEntries.length - 1; e >= 0; --e) {
283
+ var r = this.tryEntries[e];
284
+ if (r.tryLoc === t) {
285
+ var n = r.completion;
286
+ if ("throw" === n.type) {
287
+ var o = n.arg;
288
+ resetTryEntry(r);
289
+ }
290
+ return o;
291
+ }
292
+ }
293
+ throw new Error("illegal catch attempt");
294
+ },
295
+ delegateYield: function (e, r, n) {
296
+ return this.delegate = {
297
+ iterator: values(e),
298
+ resultName: r,
299
+ nextLoc: n
300
+ }, "next" === this.method && (this.arg = t), y;
301
+ }
302
+ }, e;
303
+ }
304
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
305
+ try {
306
+ var info = gen[key](arg);
307
+ var value = info.value;
308
+ } catch (error) {
309
+ reject(error);
310
+ return;
311
+ }
312
+ if (info.done) {
313
+ resolve(value);
314
+ } else {
315
+ Promise.resolve(value).then(_next, _throw);
316
+ }
317
+ }
318
+ function _asyncToGenerator(fn) {
319
+ return function () {
320
+ var self = this,
321
+ args = arguments;
322
+ return new Promise(function (resolve, reject) {
323
+ var gen = fn.apply(self, args);
324
+ function _next(value) {
325
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
326
+ }
327
+ function _throw(err) {
328
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
329
+ }
330
+ _next(undefined);
331
+ });
332
+ };
333
+ }
334
+ function _extends() {
335
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
336
+ for (var i = 1; i < arguments.length; i++) {
337
+ var source = arguments[i];
338
+ for (var key in source) {
339
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
340
+ target[key] = source[key];
341
+ }
342
+ }
343
+ }
344
+ return target;
345
+ };
346
+ return _extends.apply(this, arguments);
347
+ }
348
+
349
+ // export interface IDauthUser {
350
+ // _id: string
351
+ // ssid: string
352
+ // name: string
353
+ // lastname: string
354
+ // nickname: string
355
+ // email: string
356
+ // is_verified: boolean
357
+ // language: string
358
+ // avatar: string
359
+ // createdAt: Date
360
+ // updatedAt: Date
361
+ // last_login: Date
362
+ // }
363
+ // export interface IDauthDomain {
364
+ // name: string
365
+ // loginRedirect: string
366
+ // allowedOrigins: string[]
367
+ // }
368
+ // export interface IDauthState {
369
+ // user: IDauthUser
370
+ // domain: IDauthDomain
371
+ // isLoading: boolean
372
+ // isAuthenticated: boolean
373
+ // loginWithRedirect: () => void
374
+ // logout: () => void
375
+ // getAccessToken: () => void
376
+ // }
377
+ var userState = {
378
+ _id: "",
379
+ ssid: "",
380
+ name: "",
381
+ lastname: "",
382
+ nickname: "",
383
+ email: "",
384
+ is_verified: false,
385
+ language: "",
386
+ avatar: "",
387
+ createdAt: /*#__PURE__*/new Date(),
388
+ updatedAt: /*#__PURE__*/new Date(),
389
+ last_login: /*#__PURE__*/new Date()
390
+ };
391
+ var domainState = {
392
+ name: "",
393
+ loginRedirect: "",
394
+ allowedOrigins: [""]
395
+ };
396
+ var initialDauthState = {
397
+ user: userState,
398
+ domain: domainState,
399
+ isLoading: true,
400
+ isAuthenticated: false,
401
+ loginWithRedirect: function loginWithRedirect() {},
402
+ logout: function logout() {},
403
+ getAccessToken: function getAccessToken() {}
404
+ };
405
+
406
+ var LOGIN = 'LOGIN';
407
+ var SET_IS_LOADING = 'SET_IS_LOADING';
408
+
409
+ function userReducer(state, action) {
410
+ var type = action.type,
411
+ payload = action.payload;
412
+ switch (type) {
413
+ case LOGIN:
414
+ return _extends({}, state, {
415
+ user: payload.user,
416
+ domain: payload.domain,
417
+ isAuthenticated: payload.isAuthenticated
418
+ });
419
+ case SET_IS_LOADING:
420
+ return _extends({}, state, {
421
+ isLoading: payload.isLoading
422
+ });
423
+ default:
424
+ return state;
425
+ }
426
+ }
427
+
428
+ var isLocalhost = /*#__PURE__*/Boolean(window.location.hostname === "localhost" || window.location.hostname === "[::1]" || /*#__PURE__*/window.location.hostname.match(/(192)\.(168)\.(1)\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/gm) || /*#__PURE__*/window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/));
429
+ var apiVersion = "v1";
430
+ var serverDomain = "dauth.ovh";
431
+ function getServerBasePath(_ref) {
432
+ var domainName = _ref.domainName;
433
+ var serverPort = 4012;
434
+ var serverLocalUrl = window.location.protocol + "//" + window.location.hostname + ":" + serverPort + "/api/" + apiVersion;
435
+ var serverProdUrl = "https://" + domainName + "." + serverDomain + "/api/" + apiVersion;
436
+ var serverBasePath = isLocalhost ? serverLocalUrl : serverProdUrl;
437
+ return serverBasePath;
438
+ }
439
+ function getClientBasePath(_ref2) {
440
+ var domainName = _ref2.domainName;
441
+ var clientPort = 5185;
442
+ var clientLocalUrl = window.location.protocol + "//" + window.location.hostname + ":" + clientPort;
443
+ var clientProdUrl = "https://" + domainName + "." + serverDomain;
444
+ var clientBasePath = isLocalhost ? clientLocalUrl : clientProdUrl;
445
+ return clientBasePath;
446
+ }
447
+
448
+ var getTenantUserAPI = /*#__PURE__*/function () {
449
+ var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(domainName, ssid, token) {
450
+ var params, response, data;
451
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
452
+ while (1) switch (_context.prev = _context.next) {
453
+ case 0:
454
+ params = {
455
+ method: "GET",
456
+ headers: {
457
+ Authorization: "" + token,
458
+ "Content-Type": "application/json"
459
+ }
460
+ };
461
+ _context.next = 3;
462
+ return fetch(getServerBasePath({
463
+ domainName: domainName
464
+ }) + "/get-tenant-user/" + ssid, params);
465
+ case 3:
466
+ response = _context.sent;
467
+ _context.next = 6;
468
+ return response.json();
469
+ case 6:
470
+ data = _context.sent;
471
+ return _context.abrupt("return", {
472
+ response: response,
473
+ data: data
474
+ });
475
+ case 8:
476
+ case "end":
477
+ return _context.stop();
478
+ }
479
+ }, _callee);
480
+ }));
481
+ return function getTenantUserAPI(_x, _x2, _x3) {
482
+ return _ref.apply(this, arguments);
483
+ };
484
+ }();
485
+
486
+ var DAUTH_STATE = 'dauth_state';
487
+
488
+ function setDauthStateAction(_x) {
489
+ return _setDauthStateAction.apply(this, arguments);
490
+ }
491
+ function _setDauthStateAction() {
492
+ _setDauthStateAction = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
493
+ var dispatch, dauth_state, domainName, ssid, getUserFetch;
494
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
495
+ while (1) switch (_context.prev = _context.next) {
496
+ case 0:
497
+ dispatch = _ref.dispatch, dauth_state = _ref.dauth_state, domainName = _ref.domainName, ssid = _ref.ssid;
498
+ dispatch({
499
+ type: SET_IS_LOADING,
500
+ payload: {
501
+ isLoading: true
502
+ }
503
+ });
504
+ _context.prev = 2;
505
+ _context.next = 5;
506
+ return getTenantUserAPI(domainName, ssid, dauth_state);
507
+ case 5:
508
+ getUserFetch = _context.sent;
509
+ if (!(getUserFetch.response.status === 200)) {
510
+ _context.next = 12;
511
+ break;
512
+ }
513
+ dispatch({
514
+ type: LOGIN,
515
+ payload: {
516
+ user: getUserFetch.data.user,
517
+ domain: getUserFetch.data.domain,
518
+ isAuthenticated: true
519
+ }
520
+ });
521
+ window.history.replaceState({}, document.title, getUserFetch.data.domain.loginRedirect);
522
+ return _context.abrupt("return", localStorage.setItem(DAUTH_STATE, dauth_state));
523
+ case 12:
524
+ return _context.abrupt("return", localStorage.removeItem(DAUTH_STATE));
525
+ case 13:
526
+ _context.next = 19;
527
+ break;
528
+ case 15:
529
+ _context.prev = 15;
530
+ _context.t0 = _context["catch"](2);
531
+ localStorage.removeItem(DAUTH_STATE);
532
+ console.log(_context.t0);
533
+ case 19:
534
+ _context.prev = 19;
535
+ dispatch({
536
+ type: SET_IS_LOADING,
537
+ payload: {
538
+ isLoading: false
539
+ }
540
+ });
541
+ return _context.finish(19);
542
+ case 22:
543
+ case "end":
544
+ return _context.stop();
545
+ }
546
+ }, _callee, null, [[2, 15, 19, 22]]);
547
+ }));
548
+ return _setDauthStateAction.apply(this, arguments);
549
+ }
550
+ function setAutoLoginAction(_x2) {
551
+ return _setAutoLoginAction.apply(this, arguments);
552
+ }
553
+ function _setAutoLoginAction() {
554
+ _setAutoLoginAction = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(_ref2) {
555
+ var dispatch, dauth_state_ls, domainName, ssid, getUserFetch;
556
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
557
+ while (1) switch (_context2.prev = _context2.next) {
558
+ case 0:
559
+ dispatch = _ref2.dispatch, dauth_state_ls = _ref2.dauth_state_ls, domainName = _ref2.domainName, ssid = _ref2.ssid;
560
+ dispatch({
561
+ type: SET_IS_LOADING,
562
+ payload: {
563
+ isLoading: true
564
+ }
565
+ });
566
+ _context2.prev = 2;
567
+ _context2.next = 5;
568
+ return getTenantUserAPI(domainName, ssid, dauth_state_ls);
569
+ case 5:
570
+ getUserFetch = _context2.sent;
571
+ if (getUserFetch.response.status === 200) {
572
+ dispatch({
573
+ type: LOGIN,
574
+ payload: {
575
+ user: getUserFetch.data.user,
576
+ domain: getUserFetch.data.domain,
577
+ isAuthenticated: true
578
+ }
579
+ });
580
+ localStorage.setItem(DAUTH_STATE, dauth_state_ls);
581
+ } else {
582
+ localStorage.removeItem(DAUTH_STATE);
583
+ }
584
+ _context2.next = 13;
585
+ break;
586
+ case 9:
587
+ _context2.prev = 9;
588
+ _context2.t0 = _context2["catch"](2);
589
+ localStorage.removeItem(DAUTH_STATE);
590
+ console.log(_context2.t0);
591
+ case 13:
592
+ _context2.prev = 13;
593
+ dispatch({
594
+ type: SET_IS_LOADING,
595
+ payload: {
596
+ isLoading: false
597
+ }
598
+ });
599
+ return _context2.finish(13);
600
+ case 16:
601
+ case "end":
602
+ return _context2.stop();
603
+ }
604
+ }, _callee2, null, [[2, 9, 13, 16]]);
605
+ }));
606
+ return _setAutoLoginAction.apply(this, arguments);
607
+ }
608
+ function setLogoutAction(_x3) {
609
+ return _setLogoutAction.apply(this, arguments);
610
+ }
611
+ function _setLogoutAction() {
612
+ _setLogoutAction = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(_ref3) {
613
+ var dispatch;
614
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
615
+ while (1) switch (_context3.prev = _context3.next) {
616
+ case 0:
617
+ dispatch = _ref3.dispatch;
618
+ dispatch({
619
+ type: SET_IS_LOADING,
620
+ payload: {
621
+ isLoading: true
622
+ }
623
+ });
624
+ dispatch({
625
+ type: LOGIN,
626
+ payload: {
627
+ user: {},
628
+ domain: {},
629
+ isAuthenticated: false
630
+ }
631
+ });
632
+ localStorage.removeItem(DAUTH_STATE);
633
+ return _context3.abrupt("return", dispatch({
634
+ type: SET_IS_LOADING,
635
+ payload: {
636
+ isLoading: false
637
+ }
638
+ }));
639
+ case 5:
640
+ case "end":
641
+ return _context3.stop();
642
+ }
643
+ }, _callee3);
644
+ }));
645
+ return _setLogoutAction.apply(this, arguments);
646
+ }
2
647
 
3
648
  // Delete me
4
649
  var Thing = function Thing() {
5
650
  return createElement("div", null, "the snozzberries taste like snozzberries");
6
651
  };
652
+ var DauthProvider = function DauthProvider(props) {
653
+ var domainName = props.domainName,
654
+ sid = props.sid,
655
+ ssid = props.ssid,
656
+ children = props.children;
657
+ var _useReducer = useReducer(userReducer, initialDauthState),
658
+ dauthState = _useReducer[0],
659
+ dispatch = _useReducer[1];
660
+ // Catch login redirect
661
+ useEffect(function () {
662
+ var queryString = window.location.search;
663
+ if (!queryString) return;
664
+ var urlParams = new URLSearchParams(queryString);
665
+ var dauth_state = urlParams.get(DAUTH_STATE);
666
+ if (dauth_state && !dauthState.isAuthenticated) {
667
+ setDauthStateAction({
668
+ dispatch: dispatch,
669
+ dauth_state: dauth_state,
670
+ domainName: domainName,
671
+ ssid: ssid
672
+ });
673
+ }
674
+ }, [dauthState.isAuthenticated, domainName, ssid]);
675
+ // Auto Login
676
+ useEffect(function () {
677
+ var dauth_state_ls = localStorage.getItem(DAUTH_STATE);
678
+ if (dauth_state_ls && !dauthState.isAuthenticated) {
679
+ setAutoLoginAction({
680
+ dispatch: dispatch,
681
+ dauth_state_ls: dauth_state_ls,
682
+ domainName: domainName,
683
+ ssid: ssid
684
+ });
685
+ }
686
+ }, [dauthState.isAuthenticated, domainName, ssid]);
687
+ var loginWithRedirect = useCallback(function () {
688
+ return window.location.replace(getClientBasePath({
689
+ domainName: domainName
690
+ }) + "/t-sign/" + sid);
691
+ }, [domainName, sid]);
692
+ var logout = useCallback(function () {
693
+ return setLogoutAction({
694
+ dispatch: dispatch
695
+ });
696
+ }, []);
697
+ var memoProvider = useMemo(function () {
698
+ return _extends({}, dauthState, {
699
+ loginWithRedirect: loginWithRedirect,
700
+ logout: logout
701
+ });
702
+ }, [dauthState, loginWithRedirect, logout]);
703
+ return createElement(DauthContext.Provider, {
704
+ value: memoProvider
705
+ }, children);
706
+ };
707
+ var DauthContext = /*#__PURE__*/createContext(initialDauthState);
708
+ var useDauthContext = function useDauthContext() {
709
+ var context = useContext(DauthContext);
710
+ if (!context) {
711
+ throw new Error('useMyContext debe ser utilizado dentro de un MyContextProvider');
712
+ }
713
+ return context;
714
+ };
7
715
 
8
- export { Thing };
716
+ export { DauthProvider, Thing, useDauthContext };
9
717
  //# sourceMappingURL=dauth-context-react.esm.js.map