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