@plasmicapp/auth-react 0.0.2

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.
@@ -0,0 +1,957 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
+
7
+ var authApi = require('@plasmicapp/auth-api');
8
+ var useSWR = _interopDefault(require('swr/immutable'));
9
+
10
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
11
+ try {
12
+ var info = gen[key](arg);
13
+ var value = info.value;
14
+ } catch (error) {
15
+ reject(error);
16
+ return;
17
+ }
18
+ if (info.done) {
19
+ resolve(value);
20
+ } else {
21
+ Promise.resolve(value).then(_next, _throw);
22
+ }
23
+ }
24
+ function _asyncToGenerator(fn) {
25
+ return function () {
26
+ var self = this,
27
+ args = arguments;
28
+ return new Promise(function (resolve, reject) {
29
+ var gen = fn.apply(self, args);
30
+ function _next(value) {
31
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
32
+ }
33
+ function _throw(err) {
34
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
35
+ }
36
+ _next(undefined);
37
+ });
38
+ };
39
+ }
40
+
41
+ function createCommonjsModule(fn, module) {
42
+ return module = { exports: {} }, fn(module, module.exports), module.exports;
43
+ }
44
+
45
+ var runtime_1 = /*#__PURE__*/createCommonjsModule(function (module) {
46
+ /**
47
+ * Copyright (c) 2014-present, Facebook, Inc.
48
+ *
49
+ * This source code is licensed under the MIT license found in the
50
+ * LICENSE file in the root directory of this source tree.
51
+ */
52
+
53
+ var runtime = function (exports) {
54
+
55
+ var Op = Object.prototype;
56
+ var hasOwn = Op.hasOwnProperty;
57
+ var undefined$1; // More compressible than void 0.
58
+ var $Symbol = typeof Symbol === "function" ? Symbol : {};
59
+ var iteratorSymbol = $Symbol.iterator || "@@iterator";
60
+ var asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator";
61
+ var toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
62
+ function define(obj, key, value) {
63
+ Object.defineProperty(obj, key, {
64
+ value: value,
65
+ enumerable: true,
66
+ configurable: true,
67
+ writable: true
68
+ });
69
+ return obj[key];
70
+ }
71
+ try {
72
+ // IE 8 has a broken Object.defineProperty that only works on DOM objects.
73
+ define({}, "");
74
+ } catch (err) {
75
+ define = function define(obj, key, value) {
76
+ return obj[key] = value;
77
+ };
78
+ }
79
+ function wrap(innerFn, outerFn, self, tryLocsList) {
80
+ // If outerFn provided and outerFn.prototype is a Generator, then outerFn.prototype instanceof Generator.
81
+ var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator;
82
+ var generator = Object.create(protoGenerator.prototype);
83
+ var context = new Context(tryLocsList || []);
84
+
85
+ // The ._invoke method unifies the implementations of the .next,
86
+ // .throw, and .return methods.
87
+ generator._invoke = makeInvokeMethod(innerFn, self, context);
88
+ return generator;
89
+ }
90
+ exports.wrap = wrap;
91
+
92
+ // Try/catch helper to minimize deoptimizations. Returns a completion
93
+ // record like context.tryEntries[i].completion. This interface could
94
+ // have been (and was previously) designed to take a closure to be
95
+ // invoked without arguments, but in all the cases we care about we
96
+ // already have an existing method we want to call, so there's no need
97
+ // to create a new function object. We can even get away with assuming
98
+ // the method takes exactly one argument, since that happens to be true
99
+ // in every case, so we don't have to touch the arguments object. The
100
+ // only additional allocation required is the completion record, which
101
+ // has a stable shape and so hopefully should be cheap to allocate.
102
+ function tryCatch(fn, obj, arg) {
103
+ try {
104
+ return {
105
+ type: "normal",
106
+ arg: fn.call(obj, arg)
107
+ };
108
+ } catch (err) {
109
+ return {
110
+ type: "throw",
111
+ arg: err
112
+ };
113
+ }
114
+ }
115
+ var GenStateSuspendedStart = "suspendedStart";
116
+ var GenStateSuspendedYield = "suspendedYield";
117
+ var GenStateExecuting = "executing";
118
+ var GenStateCompleted = "completed";
119
+
120
+ // Returning this object from the innerFn has the same effect as
121
+ // breaking out of the dispatch switch statement.
122
+ var ContinueSentinel = {};
123
+
124
+ // Dummy constructor functions that we use as the .constructor and
125
+ // .constructor.prototype properties for functions that return Generator
126
+ // objects. For full spec compliance, you may wish to configure your
127
+ // minifier not to mangle the names of these two functions.
128
+ function Generator() {}
129
+ function GeneratorFunction() {}
130
+ function GeneratorFunctionPrototype() {}
131
+
132
+ // This is a polyfill for %IteratorPrototype% for environments that
133
+ // don't natively support it.
134
+ var IteratorPrototype = {};
135
+ define(IteratorPrototype, iteratorSymbol, function () {
136
+ return this;
137
+ });
138
+ var getProto = Object.getPrototypeOf;
139
+ var NativeIteratorPrototype = getProto && getProto(getProto(values([])));
140
+ if (NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol)) {
141
+ // This environment has a native %IteratorPrototype%; use it instead
142
+ // of the polyfill.
143
+ IteratorPrototype = NativeIteratorPrototype;
144
+ }
145
+ var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype);
146
+ GeneratorFunction.prototype = GeneratorFunctionPrototype;
147
+ define(Gp, "constructor", GeneratorFunctionPrototype);
148
+ define(GeneratorFunctionPrototype, "constructor", GeneratorFunction);
149
+ GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction");
150
+
151
+ // Helper for defining the .next, .throw, and .return methods of the
152
+ // Iterator interface in terms of a single ._invoke method.
153
+ function defineIteratorMethods(prototype) {
154
+ ["next", "throw", "return"].forEach(function (method) {
155
+ define(prototype, method, function (arg) {
156
+ return this._invoke(method, arg);
157
+ });
158
+ });
159
+ }
160
+ exports.isGeneratorFunction = function (genFun) {
161
+ var ctor = typeof genFun === "function" && genFun.constructor;
162
+ return ctor ? ctor === GeneratorFunction ||
163
+ // For the native GeneratorFunction constructor, the best we can
164
+ // do is to check its .name property.
165
+ (ctor.displayName || ctor.name) === "GeneratorFunction" : false;
166
+ };
167
+ exports.mark = function (genFun) {
168
+ if (Object.setPrototypeOf) {
169
+ Object.setPrototypeOf(genFun, GeneratorFunctionPrototype);
170
+ } else {
171
+ genFun.__proto__ = GeneratorFunctionPrototype;
172
+ define(genFun, toStringTagSymbol, "GeneratorFunction");
173
+ }
174
+ genFun.prototype = Object.create(Gp);
175
+ return genFun;
176
+ };
177
+
178
+ // Within the body of any async function, `await x` is transformed to
179
+ // `yield regeneratorRuntime.awrap(x)`, so that the runtime can test
180
+ // `hasOwn.call(value, "__await")` to determine if the yielded value is
181
+ // meant to be awaited.
182
+ exports.awrap = function (arg) {
183
+ return {
184
+ __await: arg
185
+ };
186
+ };
187
+ function AsyncIterator(generator, PromiseImpl) {
188
+ function invoke(method, arg, resolve, reject) {
189
+ var record = tryCatch(generator[method], generator, arg);
190
+ if (record.type === "throw") {
191
+ reject(record.arg);
192
+ } else {
193
+ var result = record.arg;
194
+ var value = result.value;
195
+ if (value && typeof value === "object" && hasOwn.call(value, "__await")) {
196
+ return PromiseImpl.resolve(value.__await).then(function (value) {
197
+ invoke("next", value, resolve, reject);
198
+ }, function (err) {
199
+ invoke("throw", err, resolve, reject);
200
+ });
201
+ }
202
+ return PromiseImpl.resolve(value).then(function (unwrapped) {
203
+ // When a yielded Promise is resolved, its final value becomes
204
+ // the .value of the Promise<{value,done}> result for the
205
+ // current iteration.
206
+ result.value = unwrapped;
207
+ resolve(result);
208
+ }, function (error) {
209
+ // If a rejected Promise was yielded, throw the rejection back
210
+ // into the async generator function so it can be handled there.
211
+ return invoke("throw", error, resolve, reject);
212
+ });
213
+ }
214
+ }
215
+ var previousPromise;
216
+ function enqueue(method, arg) {
217
+ function callInvokeWithMethodAndArg() {
218
+ return new PromiseImpl(function (resolve, reject) {
219
+ invoke(method, arg, resolve, reject);
220
+ });
221
+ }
222
+ return previousPromise =
223
+ // If enqueue has been called before, then we want to wait until
224
+ // all previous Promises have been resolved before calling invoke,
225
+ // so that results are always delivered in the correct order. If
226
+ // enqueue has not been called before, then it is important to
227
+ // call invoke immediately, without waiting on a callback to fire,
228
+ // so that the async generator function has the opportunity to do
229
+ // any necessary setup in a predictable way. This predictability
230
+ // is why the Promise constructor synchronously invokes its
231
+ // executor callback, and why async functions synchronously
232
+ // execute code before the first await. Since we implement simple
233
+ // async functions in terms of async generators, it is especially
234
+ // important to get this right, even though it requires care.
235
+ previousPromise ? previousPromise.then(callInvokeWithMethodAndArg,
236
+ // Avoid propagating failures to Promises returned by later
237
+ // invocations of the iterator.
238
+ callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
239
+ }
240
+
241
+ // Define the unified helper method that is used to implement .next,
242
+ // .throw, and .return (see defineIteratorMethods).
243
+ this._invoke = enqueue;
244
+ }
245
+ defineIteratorMethods(AsyncIterator.prototype);
246
+ define(AsyncIterator.prototype, asyncIteratorSymbol, function () {
247
+ return this;
248
+ });
249
+ exports.AsyncIterator = AsyncIterator;
250
+
251
+ // Note that simple async functions are implemented on top of
252
+ // AsyncIterator objects; they just return a Promise for the value of
253
+ // the final result produced by the iterator.
254
+ exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) {
255
+ if (PromiseImpl === void 0) PromiseImpl = Promise;
256
+ var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl);
257
+ return exports.isGeneratorFunction(outerFn) ? iter // If outerFn is a generator, return the full iterator.
258
+ : iter.next().then(function (result) {
259
+ return result.done ? result.value : iter.next();
260
+ });
261
+ };
262
+ function makeInvokeMethod(innerFn, self, context) {
263
+ var state = GenStateSuspendedStart;
264
+ return function invoke(method, arg) {
265
+ if (state === GenStateExecuting) {
266
+ throw new Error("Generator is already running");
267
+ }
268
+ if (state === GenStateCompleted) {
269
+ if (method === "throw") {
270
+ throw arg;
271
+ }
272
+
273
+ // Be forgiving, per 25.3.3.3.3 of the spec:
274
+ // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorresume
275
+ return doneResult();
276
+ }
277
+ context.method = method;
278
+ context.arg = arg;
279
+ while (true) {
280
+ var delegate = context.delegate;
281
+ if (delegate) {
282
+ var delegateResult = maybeInvokeDelegate(delegate, context);
283
+ if (delegateResult) {
284
+ if (delegateResult === ContinueSentinel) continue;
285
+ return delegateResult;
286
+ }
287
+ }
288
+ if (context.method === "next") {
289
+ // Setting context._sent for legacy support of Babel's
290
+ // function.sent implementation.
291
+ context.sent = context._sent = context.arg;
292
+ } else if (context.method === "throw") {
293
+ if (state === GenStateSuspendedStart) {
294
+ state = GenStateCompleted;
295
+ throw context.arg;
296
+ }
297
+ context.dispatchException(context.arg);
298
+ } else if (context.method === "return") {
299
+ context.abrupt("return", context.arg);
300
+ }
301
+ state = GenStateExecuting;
302
+ var record = tryCatch(innerFn, self, context);
303
+ if (record.type === "normal") {
304
+ // If an exception is thrown from innerFn, we leave state ===
305
+ // GenStateExecuting and loop back for another invocation.
306
+ state = context.done ? GenStateCompleted : GenStateSuspendedYield;
307
+ if (record.arg === ContinueSentinel) {
308
+ continue;
309
+ }
310
+ return {
311
+ value: record.arg,
312
+ done: context.done
313
+ };
314
+ } else if (record.type === "throw") {
315
+ state = GenStateCompleted;
316
+ // Dispatch the exception by looping back around to the
317
+ // context.dispatchException(context.arg) call above.
318
+ context.method = "throw";
319
+ context.arg = record.arg;
320
+ }
321
+ }
322
+ };
323
+ }
324
+
325
+ // Call delegate.iterator[context.method](context.arg) and handle the
326
+ // result, either by returning a { value, done } result from the
327
+ // delegate iterator, or by modifying context.method and context.arg,
328
+ // setting context.delegate to null, and returning the ContinueSentinel.
329
+ function maybeInvokeDelegate(delegate, context) {
330
+ var method = delegate.iterator[context.method];
331
+ if (method === undefined$1) {
332
+ // A .throw or .return when the delegate iterator has no .throw
333
+ // method always terminates the yield* loop.
334
+ context.delegate = null;
335
+ if (context.method === "throw") {
336
+ // Note: ["return"] must be used for ES3 parsing compatibility.
337
+ if (delegate.iterator["return"]) {
338
+ // If the delegate iterator has a return method, give it a
339
+ // chance to clean up.
340
+ context.method = "return";
341
+ context.arg = undefined$1;
342
+ maybeInvokeDelegate(delegate, context);
343
+ if (context.method === "throw") {
344
+ // If maybeInvokeDelegate(context) changed context.method from
345
+ // "return" to "throw", let that override the TypeError below.
346
+ return ContinueSentinel;
347
+ }
348
+ }
349
+ context.method = "throw";
350
+ context.arg = new TypeError("The iterator does not provide a 'throw' method");
351
+ }
352
+ return ContinueSentinel;
353
+ }
354
+ var record = tryCatch(method, delegate.iterator, context.arg);
355
+ if (record.type === "throw") {
356
+ context.method = "throw";
357
+ context.arg = record.arg;
358
+ context.delegate = null;
359
+ return ContinueSentinel;
360
+ }
361
+ var info = record.arg;
362
+ if (!info) {
363
+ context.method = "throw";
364
+ context.arg = new TypeError("iterator result is not an object");
365
+ context.delegate = null;
366
+ return ContinueSentinel;
367
+ }
368
+ if (info.done) {
369
+ // Assign the result of the finished delegate to the temporary
370
+ // variable specified by delegate.resultName (see delegateYield).
371
+ context[delegate.resultName] = info.value;
372
+
373
+ // Resume execution at the desired location (see delegateYield).
374
+ context.next = delegate.nextLoc;
375
+
376
+ // If context.method was "throw" but the delegate handled the
377
+ // exception, let the outer generator proceed normally. If
378
+ // context.method was "next", forget context.arg since it has been
379
+ // "consumed" by the delegate iterator. If context.method was
380
+ // "return", allow the original .return call to continue in the
381
+ // outer generator.
382
+ if (context.method !== "return") {
383
+ context.method = "next";
384
+ context.arg = undefined$1;
385
+ }
386
+ } else {
387
+ // Re-yield the result returned by the delegate method.
388
+ return info;
389
+ }
390
+
391
+ // The delegate iterator is finished, so forget it and continue with
392
+ // the outer generator.
393
+ context.delegate = null;
394
+ return ContinueSentinel;
395
+ }
396
+
397
+ // Define Generator.prototype.{next,throw,return} in terms of the
398
+ // unified ._invoke helper method.
399
+ defineIteratorMethods(Gp);
400
+ define(Gp, toStringTagSymbol, "Generator");
401
+
402
+ // A Generator should always return itself as the iterator object when the
403
+ // @@iterator function is called on it. Some browsers' implementations of the
404
+ // iterator prototype chain incorrectly implement this, causing the Generator
405
+ // object to not be returned from this call. This ensures that doesn't happen.
406
+ // See https://github.com/facebook/regenerator/issues/274 for more details.
407
+ define(Gp, iteratorSymbol, function () {
408
+ return this;
409
+ });
410
+ define(Gp, "toString", function () {
411
+ return "[object Generator]";
412
+ });
413
+ function pushTryEntry(locs) {
414
+ var entry = {
415
+ tryLoc: locs[0]
416
+ };
417
+ if (1 in locs) {
418
+ entry.catchLoc = locs[1];
419
+ }
420
+ if (2 in locs) {
421
+ entry.finallyLoc = locs[2];
422
+ entry.afterLoc = locs[3];
423
+ }
424
+ this.tryEntries.push(entry);
425
+ }
426
+ function resetTryEntry(entry) {
427
+ var record = entry.completion || {};
428
+ record.type = "normal";
429
+ delete record.arg;
430
+ entry.completion = record;
431
+ }
432
+ function Context(tryLocsList) {
433
+ // The root entry object (effectively a try statement without a catch
434
+ // or a finally block) gives us a place to store values thrown from
435
+ // locations where there is no enclosing try statement.
436
+ this.tryEntries = [{
437
+ tryLoc: "root"
438
+ }];
439
+ tryLocsList.forEach(pushTryEntry, this);
440
+ this.reset(true);
441
+ }
442
+ exports.keys = function (object) {
443
+ var keys = [];
444
+ for (var key in object) {
445
+ keys.push(key);
446
+ }
447
+ keys.reverse();
448
+
449
+ // Rather than returning an object with a next method, we keep
450
+ // things simple and return the next function itself.
451
+ return function next() {
452
+ while (keys.length) {
453
+ var key = keys.pop();
454
+ if (key in object) {
455
+ next.value = key;
456
+ next.done = false;
457
+ return next;
458
+ }
459
+ }
460
+
461
+ // To avoid creating an additional object, we just hang the .value
462
+ // and .done properties off the next function object itself. This
463
+ // also ensures that the minifier will not anonymize the function.
464
+ next.done = true;
465
+ return next;
466
+ };
467
+ };
468
+ function values(iterable) {
469
+ if (iterable) {
470
+ var iteratorMethod = iterable[iteratorSymbol];
471
+ if (iteratorMethod) {
472
+ return iteratorMethod.call(iterable);
473
+ }
474
+ if (typeof iterable.next === "function") {
475
+ return iterable;
476
+ }
477
+ if (!isNaN(iterable.length)) {
478
+ var i = -1,
479
+ next = function next() {
480
+ while (++i < iterable.length) {
481
+ if (hasOwn.call(iterable, i)) {
482
+ next.value = iterable[i];
483
+ next.done = false;
484
+ return next;
485
+ }
486
+ }
487
+ next.value = undefined$1;
488
+ next.done = true;
489
+ return next;
490
+ };
491
+ return next.next = next;
492
+ }
493
+ }
494
+
495
+ // Return an iterator with no values.
496
+ return {
497
+ next: doneResult
498
+ };
499
+ }
500
+ exports.values = values;
501
+ function doneResult() {
502
+ return {
503
+ value: undefined$1,
504
+ done: true
505
+ };
506
+ }
507
+ Context.prototype = {
508
+ constructor: Context,
509
+ reset: function reset(skipTempReset) {
510
+ this.prev = 0;
511
+ this.next = 0;
512
+ // Resetting context._sent for legacy support of Babel's
513
+ // function.sent implementation.
514
+ this.sent = this._sent = undefined$1;
515
+ this.done = false;
516
+ this.delegate = null;
517
+ this.method = "next";
518
+ this.arg = undefined$1;
519
+ this.tryEntries.forEach(resetTryEntry);
520
+ if (!skipTempReset) {
521
+ for (var name in this) {
522
+ // Not sure about the optimal order of these conditions:
523
+ if (name.charAt(0) === "t" && hasOwn.call(this, name) && !isNaN(+name.slice(1))) {
524
+ this[name] = undefined$1;
525
+ }
526
+ }
527
+ }
528
+ },
529
+ stop: function stop() {
530
+ this.done = true;
531
+ var rootEntry = this.tryEntries[0];
532
+ var rootRecord = rootEntry.completion;
533
+ if (rootRecord.type === "throw") {
534
+ throw rootRecord.arg;
535
+ }
536
+ return this.rval;
537
+ },
538
+ dispatchException: function dispatchException(exception) {
539
+ if (this.done) {
540
+ throw exception;
541
+ }
542
+ var context = this;
543
+ function handle(loc, caught) {
544
+ record.type = "throw";
545
+ record.arg = exception;
546
+ context.next = loc;
547
+ if (caught) {
548
+ // If the dispatched exception was caught by a catch block,
549
+ // then let that catch block handle the exception normally.
550
+ context.method = "next";
551
+ context.arg = undefined$1;
552
+ }
553
+ return !!caught;
554
+ }
555
+ for (var i = this.tryEntries.length - 1; i >= 0; --i) {
556
+ var entry = this.tryEntries[i];
557
+ var record = entry.completion;
558
+ if (entry.tryLoc === "root") {
559
+ // Exception thrown outside of any try block that could handle
560
+ // it, so set the completion value of the entire function to
561
+ // throw the exception.
562
+ return handle("end");
563
+ }
564
+ if (entry.tryLoc <= this.prev) {
565
+ var hasCatch = hasOwn.call(entry, "catchLoc");
566
+ var hasFinally = hasOwn.call(entry, "finallyLoc");
567
+ if (hasCatch && hasFinally) {
568
+ if (this.prev < entry.catchLoc) {
569
+ return handle(entry.catchLoc, true);
570
+ } else if (this.prev < entry.finallyLoc) {
571
+ return handle(entry.finallyLoc);
572
+ }
573
+ } else if (hasCatch) {
574
+ if (this.prev < entry.catchLoc) {
575
+ return handle(entry.catchLoc, true);
576
+ }
577
+ } else if (hasFinally) {
578
+ if (this.prev < entry.finallyLoc) {
579
+ return handle(entry.finallyLoc);
580
+ }
581
+ } else {
582
+ throw new Error("try statement without catch or finally");
583
+ }
584
+ }
585
+ }
586
+ },
587
+ abrupt: function abrupt(type, arg) {
588
+ for (var i = this.tryEntries.length - 1; i >= 0; --i) {
589
+ var entry = this.tryEntries[i];
590
+ if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) {
591
+ var finallyEntry = entry;
592
+ break;
593
+ }
594
+ }
595
+ if (finallyEntry && (type === "break" || type === "continue") && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc) {
596
+ // Ignore the finally entry if control is not jumping to a
597
+ // location outside the try/catch block.
598
+ finallyEntry = null;
599
+ }
600
+ var record = finallyEntry ? finallyEntry.completion : {};
601
+ record.type = type;
602
+ record.arg = arg;
603
+ if (finallyEntry) {
604
+ this.method = "next";
605
+ this.next = finallyEntry.finallyLoc;
606
+ return ContinueSentinel;
607
+ }
608
+ return this.complete(record);
609
+ },
610
+ complete: function complete(record, afterLoc) {
611
+ if (record.type === "throw") {
612
+ throw record.arg;
613
+ }
614
+ if (record.type === "break" || record.type === "continue") {
615
+ this.next = record.arg;
616
+ } else if (record.type === "return") {
617
+ this.rval = this.arg = record.arg;
618
+ this.method = "return";
619
+ this.next = "end";
620
+ } else if (record.type === "normal" && afterLoc) {
621
+ this.next = afterLoc;
622
+ }
623
+ return ContinueSentinel;
624
+ },
625
+ finish: function finish(finallyLoc) {
626
+ for (var i = this.tryEntries.length - 1; i >= 0; --i) {
627
+ var entry = this.tryEntries[i];
628
+ if (entry.finallyLoc === finallyLoc) {
629
+ this.complete(entry.completion, entry.afterLoc);
630
+ resetTryEntry(entry);
631
+ return ContinueSentinel;
632
+ }
633
+ }
634
+ },
635
+ "catch": function _catch(tryLoc) {
636
+ for (var i = this.tryEntries.length - 1; i >= 0; --i) {
637
+ var entry = this.tryEntries[i];
638
+ if (entry.tryLoc === tryLoc) {
639
+ var record = entry.completion;
640
+ if (record.type === "throw") {
641
+ var thrown = record.arg;
642
+ resetTryEntry(entry);
643
+ }
644
+ return thrown;
645
+ }
646
+ }
647
+
648
+ // The context.catch method must only be called with a location
649
+ // argument that corresponds to a known catch block.
650
+ throw new Error("illegal catch attempt");
651
+ },
652
+ delegateYield: function delegateYield(iterable, resultName, nextLoc) {
653
+ this.delegate = {
654
+ iterator: values(iterable),
655
+ resultName: resultName,
656
+ nextLoc: nextLoc
657
+ };
658
+ if (this.method === "next") {
659
+ // Deliberately forget the last sent value so that we don't
660
+ // accidentally pass it on to the delegate.
661
+ this.arg = undefined$1;
662
+ }
663
+ return ContinueSentinel;
664
+ }
665
+ };
666
+
667
+ // Regardless of whether this script is executing as a CommonJS module
668
+ // or not, return the runtime object so that we can declare the variable
669
+ // regeneratorRuntime in the outer scope, which allows this module to be
670
+ // injected easily by `bin/regenerator --include-runtime script.js`.
671
+ return exports;
672
+ }(
673
+ // If this script is executing as a CommonJS module, use module.exports
674
+ // as the regeneratorRuntime namespace. Otherwise create a new empty
675
+ // object. Either way, the resulting object will be used to initialize
676
+ // the regeneratorRuntime variable at the top of this file.
677
+ module.exports );
678
+ try {
679
+ regeneratorRuntime = runtime;
680
+ } catch (accidentalStrictMode) {
681
+ // This module should not be running in strict mode, so the above
682
+ // assignment should always work unless something is misconfigured. Just
683
+ // in case runtime.js accidentally runs in strict mode, in modern engines
684
+ // we can explicitly access globalThis. In older engines we can escape
685
+ // strict mode using a global Function call. This could conceivably fail
686
+ // if a Content Security Policy forbids using Function, but in that case
687
+ // the proper solution is to fix the accidental strict mode problem. If
688
+ // you've misconfigured your bundler to force strict mode and applied a
689
+ // CSP to forbid Function, and you're not willing to fix either of those
690
+ // problems, please detail your unique predicament in a GitHub issue.
691
+ if (typeof globalThis === "object") {
692
+ globalThis.regeneratorRuntime = runtime;
693
+ } else {
694
+ Function("r", "regeneratorRuntime = r")(runtime);
695
+ }
696
+ }
697
+ });
698
+
699
+ var STORAGE_USER_KEY = 'plasmic_user';
700
+ function getCallbackParams() {
701
+ var params = new URLSearchParams(window.location.search);
702
+ var error = params.get('error');
703
+ var code = params.get('code');
704
+ var state = params.get('state');
705
+ return {
706
+ isCallbackError: !!error,
707
+ isCodeExchange: !!code && !!state,
708
+ error: error,
709
+ code: code,
710
+ state: state
711
+ };
712
+ }
713
+ function getCodeVerifier() {
714
+ try {
715
+ return localStorage.getItem('code_verifier');
716
+ } catch (err) {
717
+ return null;
718
+ }
719
+ }
720
+ function removeCallbackParams() {
721
+ try {
722
+ window.history.replaceState({}, '', location.pathname);
723
+ } catch (err) {}
724
+ }
725
+ function handleCallback(_x) {
726
+ return _handleCallback.apply(this, arguments);
727
+ }
728
+ function _handleCallback() {
729
+ _handleCallback = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(opts) {
730
+ var host, appId, code, state, codeVerifier, continueTo, parsedState, _yield$getPlasmicAppU, token, user, error;
731
+ return runtime_1.wrap(function _callee2$(_context2) {
732
+ while (1) {
733
+ switch (_context2.prev = _context2.next) {
734
+ case 0:
735
+ host = opts.host, appId = opts.appId, code = opts.code, state = opts.state, codeVerifier = opts.codeVerifier;
736
+ continueTo = undefined;
737
+ try {
738
+ if (state) {
739
+ parsedState = JSON.parse(state);
740
+ continueTo = parsedState.continueTo;
741
+ }
742
+ } catch (err) {}
743
+ _context2.next = 5;
744
+ return authApi.getPlasmicAppUser({
745
+ host: host,
746
+ appId: appId,
747
+ code: code,
748
+ codeVerifier: codeVerifier
749
+ });
750
+ case 5:
751
+ _yield$getPlasmicAppU = _context2.sent;
752
+ token = _yield$getPlasmicAppU.token;
753
+ user = _yield$getPlasmicAppU.user;
754
+ error = _yield$getPlasmicAppU.error;
755
+ if (!error) {
756
+ _context2.next = 12;
757
+ break;
758
+ }
759
+ console.log("Error while performing code exchange: " + error);
760
+ return _context2.abrupt("return", undefined);
761
+ case 12:
762
+ localStorage.setItem(STORAGE_USER_KEY, token);
763
+ if (continueTo) {
764
+ window.location.assign(continueTo);
765
+ } else {
766
+ window.location.assign('/');
767
+ }
768
+ return _context2.abrupt("return", {
769
+ token: token,
770
+ user: user
771
+ });
772
+ case 15:
773
+ case "end":
774
+ return _context2.stop();
775
+ }
776
+ }
777
+ }, _callee2);
778
+ }));
779
+ return _handleCallback.apply(this, arguments);
780
+ }
781
+ function checkAlreadyLoggedUser(_x2) {
782
+ return _checkAlreadyLoggedUser.apply(this, arguments);
783
+ }
784
+ /**
785
+ * Handles the authentication flow for Plasmic Auth and returns the user and token
786
+ */
787
+ function _checkAlreadyLoggedUser() {
788
+ _checkAlreadyLoggedUser = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(opts) {
789
+ var host, token, _yield$getPlasmicAppU2, user, error;
790
+ return runtime_1.wrap(function _callee3$(_context3) {
791
+ while (1) {
792
+ switch (_context3.prev = _context3.next) {
793
+ case 0:
794
+ host = opts.host;
795
+ token = localStorage.getItem(STORAGE_USER_KEY);
796
+ if (token) {
797
+ _context3.next = 4;
798
+ break;
799
+ }
800
+ return _context3.abrupt("return", {
801
+ user: null,
802
+ token: null
803
+ });
804
+ case 4:
805
+ _context3.next = 6;
806
+ return authApi.getPlasmicAppUserFromToken({
807
+ host: host,
808
+ token: token
809
+ });
810
+ case 6:
811
+ _yield$getPlasmicAppU2 = _context3.sent;
812
+ user = _yield$getPlasmicAppU2.user;
813
+ error = _yield$getPlasmicAppU2.error;
814
+ if (!error) {
815
+ _context3.next = 13;
816
+ break;
817
+ }
818
+ // If there is an error, we just remove the token
819
+ // But ideally we should check if the reason is token expired
820
+ localStorage.removeItem(STORAGE_USER_KEY);
821
+ console.log("Error while checking logged user");
822
+ return _context3.abrupt("return", {
823
+ user: null,
824
+ token: null
825
+ });
826
+ case 13:
827
+ return _context3.abrupt("return", {
828
+ user: user,
829
+ token: token
830
+ });
831
+ case 14:
832
+ case "end":
833
+ return _context3.stop();
834
+ }
835
+ }
836
+ }, _callee3);
837
+ }));
838
+ return _checkAlreadyLoggedUser.apply(this, arguments);
839
+ }
840
+ function usePlasmicAuth(opts) {
841
+ var host = opts.host,
842
+ appId = opts.appId;
843
+ var _useSWR = useSWR(['plasmic-auth', appId], /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee() {
844
+ var callbackParams, codeVerifier, result;
845
+ return runtime_1.wrap(function _callee$(_context) {
846
+ while (1) {
847
+ switch (_context.prev = _context.next) {
848
+ case 0:
849
+ if (appId) {
850
+ _context.next = 2;
851
+ break;
852
+ }
853
+ return _context.abrupt("return", {
854
+ user: null,
855
+ token: null
856
+ });
857
+ case 2:
858
+ _context.prev = 2;
859
+ // We first check if we are currently in the callback flow
860
+ callbackParams = getCallbackParams();
861
+ if (!(callbackParams.isCallbackError || callbackParams.isCodeExchange)) {
862
+ _context.next = 27;
863
+ break;
864
+ }
865
+ if (!callbackParams.isCallbackError) {
866
+ _context.next = 11;
867
+ break;
868
+ }
869
+ // If there is an error, we just remove the callback params
870
+ removeCallbackParams();
871
+ console.error("Error: " + callbackParams.error);
872
+ return _context.abrupt("return", {
873
+ user: null,
874
+ token: null
875
+ });
876
+ case 11:
877
+ codeVerifier = getCodeVerifier();
878
+ if (codeVerifier) {
879
+ _context.next = 18;
880
+ break;
881
+ }
882
+ // If there is no codeVerifier, we just remove the callback params
883
+ removeCallbackParams();
884
+ console.error('No code verifier found');
885
+ return _context.abrupt("return", {
886
+ user: null,
887
+ token: null
888
+ });
889
+ case 18:
890
+ _context.next = 20;
891
+ return handleCallback({
892
+ host: host,
893
+ appId: appId,
894
+ code: callbackParams.code,
895
+ state: callbackParams.state,
896
+ codeVerifier: codeVerifier
897
+ });
898
+ case 20:
899
+ result = _context.sent;
900
+ if (result) {
901
+ _context.next = 24;
902
+ break;
903
+ }
904
+ removeCallbackParams();
905
+ return _context.abrupt("return", {
906
+ user: null,
907
+ token: null
908
+ });
909
+ case 24:
910
+ return _context.abrupt("return", result);
911
+ case 25:
912
+ _context.next = 30;
913
+ break;
914
+ case 27:
915
+ _context.next = 29;
916
+ return checkAlreadyLoggedUser({
917
+ host: host
918
+ });
919
+ case 29:
920
+ return _context.abrupt("return", _context.sent);
921
+ case 30:
922
+ _context.next = 34;
923
+ break;
924
+ case 32:
925
+ _context.prev = 32;
926
+ _context.t0 = _context["catch"](2);
927
+ case 34:
928
+ return _context.abrupt("return", {
929
+ user: null,
930
+ token: null
931
+ });
932
+ case 35:
933
+ case "end":
934
+ return _context.stop();
935
+ }
936
+ }
937
+ }, _callee, null, [[2, 32]]);
938
+ }))),
939
+ userData = _useSWR.data,
940
+ isLoading = _useSWR.isLoading;
941
+ return {
942
+ user: userData == null ? void 0 : userData.user,
943
+ token: userData == null ? void 0 : userData.token,
944
+ isUserLoading: isLoading
945
+ };
946
+ }
947
+
948
+ Object.keys(authApi).forEach(function (k) {
949
+ if (k !== 'default') Object.defineProperty(exports, k, {
950
+ enumerable: true,
951
+ get: function () {
952
+ return authApi[k];
953
+ }
954
+ });
955
+ });
956
+ exports.usePlasmicAuth = usePlasmicAuth;
957
+ //# sourceMappingURL=auth-react.cjs.development.js.map