@forward-software/react-auth 1.1.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +2 -2
- package/README.md +91 -44
- package/dist/auth.d.ts +237 -0
- package/dist/auth.js +215 -0
- package/dist/index.d.ts +2 -21
- package/dist/index.js +1 -8
- package/dist/{types/eventEmitter.d.ts → utils.d.ts} +17 -10
- package/dist/utils.js +30 -0
- package/package.json +28 -60
- package/src/auth.tsx +546 -0
- package/src/index.ts +2 -0
- package/src/{types/eventEmitter.ts → utils.ts} +25 -2
- package/dist/react-auth.cjs.development.js +0 -789
- package/dist/react-auth.cjs.development.js.map +0 -1
- package/dist/react-auth.cjs.production.min.js +0 -2
- package/dist/react-auth.cjs.production.min.js.map +0 -1
- package/dist/react-auth.esm.js +0 -781
- package/dist/react-auth.esm.js.map +0 -1
- package/dist/types/deferred.d.ts +0 -7
- package/dist/types/index.d.ts +0 -56
- package/src/index.tsx +0 -98
- package/src/types/deferred.ts +0 -18
- package/src/types/index.ts +0 -298
package/dist/react-auth.esm.js
DELETED
|
@@ -1,781 +0,0 @@
|
|
|
1
|
-
import React, { createContext, useState, useEffect, useContext } from 'react';
|
|
2
|
-
import { useSyncExternalStore } from 'use-sync-external-store/shim';
|
|
3
|
-
|
|
4
|
-
function _regeneratorRuntime() {
|
|
5
|
-
_regeneratorRuntime = function () {
|
|
6
|
-
return exports;
|
|
7
|
-
};
|
|
8
|
-
var exports = {},
|
|
9
|
-
Op = Object.prototype,
|
|
10
|
-
hasOwn = Op.hasOwnProperty,
|
|
11
|
-
defineProperty = Object.defineProperty || function (obj, key, desc) {
|
|
12
|
-
obj[key] = desc.value;
|
|
13
|
-
},
|
|
14
|
-
$Symbol = "function" == typeof Symbol ? Symbol : {},
|
|
15
|
-
iteratorSymbol = $Symbol.iterator || "@@iterator",
|
|
16
|
-
asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator",
|
|
17
|
-
toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
|
|
18
|
-
function define(obj, key, value) {
|
|
19
|
-
return Object.defineProperty(obj, key, {
|
|
20
|
-
value: value,
|
|
21
|
-
enumerable: !0,
|
|
22
|
-
configurable: !0,
|
|
23
|
-
writable: !0
|
|
24
|
-
}), obj[key];
|
|
25
|
-
}
|
|
26
|
-
try {
|
|
27
|
-
define({}, "");
|
|
28
|
-
} catch (err) {
|
|
29
|
-
define = function (obj, key, value) {
|
|
30
|
-
return obj[key] = value;
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
function wrap(innerFn, outerFn, self, tryLocsList) {
|
|
34
|
-
var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator,
|
|
35
|
-
generator = Object.create(protoGenerator.prototype),
|
|
36
|
-
context = new Context(tryLocsList || []);
|
|
37
|
-
return defineProperty(generator, "_invoke", {
|
|
38
|
-
value: makeInvokeMethod(innerFn, self, context)
|
|
39
|
-
}), generator;
|
|
40
|
-
}
|
|
41
|
-
function tryCatch(fn, obj, arg) {
|
|
42
|
-
try {
|
|
43
|
-
return {
|
|
44
|
-
type: "normal",
|
|
45
|
-
arg: fn.call(obj, arg)
|
|
46
|
-
};
|
|
47
|
-
} catch (err) {
|
|
48
|
-
return {
|
|
49
|
-
type: "throw",
|
|
50
|
-
arg: err
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
exports.wrap = wrap;
|
|
55
|
-
var ContinueSentinel = {};
|
|
56
|
-
function Generator() {}
|
|
57
|
-
function GeneratorFunction() {}
|
|
58
|
-
function GeneratorFunctionPrototype() {}
|
|
59
|
-
var IteratorPrototype = {};
|
|
60
|
-
define(IteratorPrototype, iteratorSymbol, function () {
|
|
61
|
-
return this;
|
|
62
|
-
});
|
|
63
|
-
var getProto = Object.getPrototypeOf,
|
|
64
|
-
NativeIteratorPrototype = getProto && getProto(getProto(values([])));
|
|
65
|
-
NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype);
|
|
66
|
-
var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype);
|
|
67
|
-
function defineIteratorMethods(prototype) {
|
|
68
|
-
["next", "throw", "return"].forEach(function (method) {
|
|
69
|
-
define(prototype, method, function (arg) {
|
|
70
|
-
return this._invoke(method, arg);
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
function AsyncIterator(generator, PromiseImpl) {
|
|
75
|
-
function invoke(method, arg, resolve, reject) {
|
|
76
|
-
var record = tryCatch(generator[method], generator, arg);
|
|
77
|
-
if ("throw" !== record.type) {
|
|
78
|
-
var result = record.arg,
|
|
79
|
-
value = result.value;
|
|
80
|
-
return value && "object" == typeof value && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) {
|
|
81
|
-
invoke("next", value, resolve, reject);
|
|
82
|
-
}, function (err) {
|
|
83
|
-
invoke("throw", err, resolve, reject);
|
|
84
|
-
}) : PromiseImpl.resolve(value).then(function (unwrapped) {
|
|
85
|
-
result.value = unwrapped, resolve(result);
|
|
86
|
-
}, function (error) {
|
|
87
|
-
return invoke("throw", error, resolve, reject);
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
reject(record.arg);
|
|
91
|
-
}
|
|
92
|
-
var previousPromise;
|
|
93
|
-
defineProperty(this, "_invoke", {
|
|
94
|
-
value: function (method, arg) {
|
|
95
|
-
function callInvokeWithMethodAndArg() {
|
|
96
|
-
return new PromiseImpl(function (resolve, reject) {
|
|
97
|
-
invoke(method, arg, resolve, reject);
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
function makeInvokeMethod(innerFn, self, context) {
|
|
105
|
-
var state = "suspendedStart";
|
|
106
|
-
return function (method, arg) {
|
|
107
|
-
if ("executing" === state) throw new Error("Generator is already running");
|
|
108
|
-
if ("completed" === state) {
|
|
109
|
-
if ("throw" === method) throw arg;
|
|
110
|
-
return doneResult();
|
|
111
|
-
}
|
|
112
|
-
for (context.method = method, context.arg = arg;;) {
|
|
113
|
-
var delegate = context.delegate;
|
|
114
|
-
if (delegate) {
|
|
115
|
-
var delegateResult = maybeInvokeDelegate(delegate, context);
|
|
116
|
-
if (delegateResult) {
|
|
117
|
-
if (delegateResult === ContinueSentinel) continue;
|
|
118
|
-
return delegateResult;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) {
|
|
122
|
-
if ("suspendedStart" === state) throw state = "completed", context.arg;
|
|
123
|
-
context.dispatchException(context.arg);
|
|
124
|
-
} else "return" === context.method && context.abrupt("return", context.arg);
|
|
125
|
-
state = "executing";
|
|
126
|
-
var record = tryCatch(innerFn, self, context);
|
|
127
|
-
if ("normal" === record.type) {
|
|
128
|
-
if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue;
|
|
129
|
-
return {
|
|
130
|
-
value: record.arg,
|
|
131
|
-
done: context.done
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
"throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg);
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
function maybeInvokeDelegate(delegate, context) {
|
|
139
|
-
var method = delegate.iterator[context.method];
|
|
140
|
-
if (undefined === method) {
|
|
141
|
-
if (context.delegate = null, "throw" === context.method) {
|
|
142
|
-
if (delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel;
|
|
143
|
-
context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method");
|
|
144
|
-
}
|
|
145
|
-
return ContinueSentinel;
|
|
146
|
-
}
|
|
147
|
-
var record = tryCatch(method, delegate.iterator, context.arg);
|
|
148
|
-
if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel;
|
|
149
|
-
var info = record.arg;
|
|
150
|
-
return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel);
|
|
151
|
-
}
|
|
152
|
-
function pushTryEntry(locs) {
|
|
153
|
-
var entry = {
|
|
154
|
-
tryLoc: locs[0]
|
|
155
|
-
};
|
|
156
|
-
1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry);
|
|
157
|
-
}
|
|
158
|
-
function resetTryEntry(entry) {
|
|
159
|
-
var record = entry.completion || {};
|
|
160
|
-
record.type = "normal", delete record.arg, entry.completion = record;
|
|
161
|
-
}
|
|
162
|
-
function Context(tryLocsList) {
|
|
163
|
-
this.tryEntries = [{
|
|
164
|
-
tryLoc: "root"
|
|
165
|
-
}], tryLocsList.forEach(pushTryEntry, this), this.reset(!0);
|
|
166
|
-
}
|
|
167
|
-
function values(iterable) {
|
|
168
|
-
if (iterable) {
|
|
169
|
-
var iteratorMethod = iterable[iteratorSymbol];
|
|
170
|
-
if (iteratorMethod) return iteratorMethod.call(iterable);
|
|
171
|
-
if ("function" == typeof iterable.next) return iterable;
|
|
172
|
-
if (!isNaN(iterable.length)) {
|
|
173
|
-
var i = -1,
|
|
174
|
-
next = function next() {
|
|
175
|
-
for (; ++i < iterable.length;) if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next;
|
|
176
|
-
return next.value = undefined, next.done = !0, next;
|
|
177
|
-
};
|
|
178
|
-
return next.next = next;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
return {
|
|
182
|
-
next: doneResult
|
|
183
|
-
};
|
|
184
|
-
}
|
|
185
|
-
function doneResult() {
|
|
186
|
-
return {
|
|
187
|
-
value: undefined,
|
|
188
|
-
done: !0
|
|
189
|
-
};
|
|
190
|
-
}
|
|
191
|
-
return GeneratorFunction.prototype = GeneratorFunctionPrototype, defineProperty(Gp, "constructor", {
|
|
192
|
-
value: GeneratorFunctionPrototype,
|
|
193
|
-
configurable: !0
|
|
194
|
-
}), defineProperty(GeneratorFunctionPrototype, "constructor", {
|
|
195
|
-
value: GeneratorFunction,
|
|
196
|
-
configurable: !0
|
|
197
|
-
}), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) {
|
|
198
|
-
var ctor = "function" == typeof genFun && genFun.constructor;
|
|
199
|
-
return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name));
|
|
200
|
-
}, exports.mark = function (genFun) {
|
|
201
|
-
return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun;
|
|
202
|
-
}, exports.awrap = function (arg) {
|
|
203
|
-
return {
|
|
204
|
-
__await: arg
|
|
205
|
-
};
|
|
206
|
-
}, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () {
|
|
207
|
-
return this;
|
|
208
|
-
}), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) {
|
|
209
|
-
void 0 === PromiseImpl && (PromiseImpl = Promise);
|
|
210
|
-
var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl);
|
|
211
|
-
return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) {
|
|
212
|
-
return result.done ? result.value : iter.next();
|
|
213
|
-
});
|
|
214
|
-
}, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () {
|
|
215
|
-
return this;
|
|
216
|
-
}), define(Gp, "toString", function () {
|
|
217
|
-
return "[object Generator]";
|
|
218
|
-
}), exports.keys = function (val) {
|
|
219
|
-
var object = Object(val),
|
|
220
|
-
keys = [];
|
|
221
|
-
for (var key in object) keys.push(key);
|
|
222
|
-
return keys.reverse(), function next() {
|
|
223
|
-
for (; keys.length;) {
|
|
224
|
-
var key = keys.pop();
|
|
225
|
-
if (key in object) return next.value = key, next.done = !1, next;
|
|
226
|
-
}
|
|
227
|
-
return next.done = !0, next;
|
|
228
|
-
};
|
|
229
|
-
}, exports.values = values, Context.prototype = {
|
|
230
|
-
constructor: Context,
|
|
231
|
-
reset: function (skipTempReset) {
|
|
232
|
-
if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined);
|
|
233
|
-
},
|
|
234
|
-
stop: function () {
|
|
235
|
-
this.done = !0;
|
|
236
|
-
var rootRecord = this.tryEntries[0].completion;
|
|
237
|
-
if ("throw" === rootRecord.type) throw rootRecord.arg;
|
|
238
|
-
return this.rval;
|
|
239
|
-
},
|
|
240
|
-
dispatchException: function (exception) {
|
|
241
|
-
if (this.done) throw exception;
|
|
242
|
-
var context = this;
|
|
243
|
-
function handle(loc, caught) {
|
|
244
|
-
return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught;
|
|
245
|
-
}
|
|
246
|
-
for (var i = this.tryEntries.length - 1; i >= 0; --i) {
|
|
247
|
-
var entry = this.tryEntries[i],
|
|
248
|
-
record = entry.completion;
|
|
249
|
-
if ("root" === entry.tryLoc) return handle("end");
|
|
250
|
-
if (entry.tryLoc <= this.prev) {
|
|
251
|
-
var hasCatch = hasOwn.call(entry, "catchLoc"),
|
|
252
|
-
hasFinally = hasOwn.call(entry, "finallyLoc");
|
|
253
|
-
if (hasCatch && hasFinally) {
|
|
254
|
-
if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
|
|
255
|
-
if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
|
|
256
|
-
} else if (hasCatch) {
|
|
257
|
-
if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
|
|
258
|
-
} else {
|
|
259
|
-
if (!hasFinally) throw new Error("try statement without catch or finally");
|
|
260
|
-
if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
},
|
|
265
|
-
abrupt: function (type, arg) {
|
|
266
|
-
for (var i = this.tryEntries.length - 1; i >= 0; --i) {
|
|
267
|
-
var entry = this.tryEntries[i];
|
|
268
|
-
if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) {
|
|
269
|
-
var finallyEntry = entry;
|
|
270
|
-
break;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null);
|
|
274
|
-
var record = finallyEntry ? finallyEntry.completion : {};
|
|
275
|
-
return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record);
|
|
276
|
-
},
|
|
277
|
-
complete: function (record, afterLoc) {
|
|
278
|
-
if ("throw" === record.type) throw record.arg;
|
|
279
|
-
return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel;
|
|
280
|
-
},
|
|
281
|
-
finish: function (finallyLoc) {
|
|
282
|
-
for (var i = this.tryEntries.length - 1; i >= 0; --i) {
|
|
283
|
-
var entry = this.tryEntries[i];
|
|
284
|
-
if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel;
|
|
285
|
-
}
|
|
286
|
-
},
|
|
287
|
-
catch: function (tryLoc) {
|
|
288
|
-
for (var i = this.tryEntries.length - 1; i >= 0; --i) {
|
|
289
|
-
var entry = this.tryEntries[i];
|
|
290
|
-
if (entry.tryLoc === tryLoc) {
|
|
291
|
-
var record = entry.completion;
|
|
292
|
-
if ("throw" === record.type) {
|
|
293
|
-
var thrown = record.arg;
|
|
294
|
-
resetTryEntry(entry);
|
|
295
|
-
}
|
|
296
|
-
return thrown;
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
throw new Error("illegal catch attempt");
|
|
300
|
-
},
|
|
301
|
-
delegateYield: function (iterable, resultName, nextLoc) {
|
|
302
|
-
return this.delegate = {
|
|
303
|
-
iterator: values(iterable),
|
|
304
|
-
resultName: resultName,
|
|
305
|
-
nextLoc: nextLoc
|
|
306
|
-
}, "next" === this.method && (this.arg = undefined), ContinueSentinel;
|
|
307
|
-
}
|
|
308
|
-
}, exports;
|
|
309
|
-
}
|
|
310
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
311
|
-
try {
|
|
312
|
-
var info = gen[key](arg);
|
|
313
|
-
var value = info.value;
|
|
314
|
-
} catch (error) {
|
|
315
|
-
reject(error);
|
|
316
|
-
return;
|
|
317
|
-
}
|
|
318
|
-
if (info.done) {
|
|
319
|
-
resolve(value);
|
|
320
|
-
} else {
|
|
321
|
-
Promise.resolve(value).then(_next, _throw);
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
function _asyncToGenerator(fn) {
|
|
325
|
-
return function () {
|
|
326
|
-
var self = this,
|
|
327
|
-
args = arguments;
|
|
328
|
-
return new Promise(function (resolve, reject) {
|
|
329
|
-
var gen = fn.apply(self, args);
|
|
330
|
-
function _next(value) {
|
|
331
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
332
|
-
}
|
|
333
|
-
function _throw(err) {
|
|
334
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
335
|
-
}
|
|
336
|
-
_next(undefined);
|
|
337
|
-
});
|
|
338
|
-
};
|
|
339
|
-
}
|
|
340
|
-
function _defineProperties(target, props) {
|
|
341
|
-
for (var i = 0; i < props.length; i++) {
|
|
342
|
-
var descriptor = props[i];
|
|
343
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
|
344
|
-
descriptor.configurable = true;
|
|
345
|
-
if ("value" in descriptor) descriptor.writable = true;
|
|
346
|
-
Object.defineProperty(target, descriptor.key, descriptor);
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
function _createClass(Constructor, protoProps, staticProps) {
|
|
350
|
-
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
351
|
-
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
352
|
-
Object.defineProperty(Constructor, "prototype", {
|
|
353
|
-
writable: false
|
|
354
|
-
});
|
|
355
|
-
return Constructor;
|
|
356
|
-
}
|
|
357
|
-
function _extends() {
|
|
358
|
-
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
359
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
360
|
-
var source = arguments[i];
|
|
361
|
-
for (var key in source) {
|
|
362
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
363
|
-
target[key] = source[key];
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
return target;
|
|
368
|
-
};
|
|
369
|
-
return _extends.apply(this, arguments);
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
var Deferred = /*#__PURE__*/function () {
|
|
373
|
-
function Deferred() {
|
|
374
|
-
var _this = this;
|
|
375
|
-
this.promise = new Promise(function (resolve, reject) {
|
|
376
|
-
_this.reject = reject;
|
|
377
|
-
_this.resolve = resolve;
|
|
378
|
-
});
|
|
379
|
-
}
|
|
380
|
-
var _proto = Deferred.prototype;
|
|
381
|
-
_proto.getPromise = function getPromise() {
|
|
382
|
-
return this.promise;
|
|
383
|
-
};
|
|
384
|
-
return Deferred;
|
|
385
|
-
}();
|
|
386
|
-
|
|
387
|
-
// TODO: Improve -> `listeners` are unbounded -- don't use this in practice!
|
|
388
|
-
function createEventEmitter() {
|
|
389
|
-
var listeners = {};
|
|
390
|
-
return {
|
|
391
|
-
on: function on(key, fn) {
|
|
392
|
-
listeners[key] = (listeners[key] || []).concat(fn);
|
|
393
|
-
},
|
|
394
|
-
off: function off(key, fn) {
|
|
395
|
-
listeners[key] = (listeners[key] || []).filter(function (f) {
|
|
396
|
-
return f !== fn;
|
|
397
|
-
});
|
|
398
|
-
},
|
|
399
|
-
emit: function emit(key, data) {
|
|
400
|
-
(listeners[key] || []).forEach(function (fn) {
|
|
401
|
-
try {
|
|
402
|
-
fn(data);
|
|
403
|
-
} catch (_unused) {}
|
|
404
|
-
});
|
|
405
|
-
}
|
|
406
|
-
};
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
var BaseAuthClient = /*#__PURE__*/function () {
|
|
410
|
-
function BaseAuthClient() {
|
|
411
|
-
var _this = this;
|
|
412
|
-
this._state = {
|
|
413
|
-
isAuthenticated: false,
|
|
414
|
-
isInitialized: false,
|
|
415
|
-
tokens: {}
|
|
416
|
-
};
|
|
417
|
-
// refresh queue - used to avoid concurrency issue during Token refresh
|
|
418
|
-
this.refreshQ = [];
|
|
419
|
-
this.eventEmitter = createEventEmitter();
|
|
420
|
-
this.subscribers = new Set();
|
|
421
|
-
// Should be declared like this to avoid binding issues when used by useSyncExternalStore
|
|
422
|
-
this.subscribe = function (subscription) {
|
|
423
|
-
_this.subscribers.add(subscription);
|
|
424
|
-
return function () {
|
|
425
|
-
return _this.subscribers["delete"](subscription);
|
|
426
|
-
};
|
|
427
|
-
};
|
|
428
|
-
// Should be declared like this to avoid binding issues when used by useSyncExternalStore
|
|
429
|
-
this.getSnapshot = function () {
|
|
430
|
-
return _this._state;
|
|
431
|
-
};
|
|
432
|
-
}
|
|
433
|
-
//
|
|
434
|
-
// Getters
|
|
435
|
-
//
|
|
436
|
-
var _proto = BaseAuthClient.prototype;
|
|
437
|
-
//
|
|
438
|
-
// Public methods
|
|
439
|
-
//
|
|
440
|
-
_proto.init =
|
|
441
|
-
/*#__PURE__*/
|
|
442
|
-
function () {
|
|
443
|
-
var _init = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
444
|
-
var _this$onPostInit;
|
|
445
|
-
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
446
|
-
while (1) {
|
|
447
|
-
switch (_context.prev = _context.next) {
|
|
448
|
-
case 0:
|
|
449
|
-
_context.prev = 0;
|
|
450
|
-
_context.next = 3;
|
|
451
|
-
return this.onInit();
|
|
452
|
-
case 3:
|
|
453
|
-
this.setState({
|
|
454
|
-
isInitialized: true
|
|
455
|
-
});
|
|
456
|
-
this.emit('initSuccess', undefined);
|
|
457
|
-
_context.next = 11;
|
|
458
|
-
break;
|
|
459
|
-
case 7:
|
|
460
|
-
_context.prev = 7;
|
|
461
|
-
_context.t0 = _context["catch"](0);
|
|
462
|
-
this.setState({
|
|
463
|
-
isInitialized: false
|
|
464
|
-
});
|
|
465
|
-
this.emit('initFailed', _context.t0);
|
|
466
|
-
case 11:
|
|
467
|
-
_context.next = 13;
|
|
468
|
-
return (_this$onPostInit = this.onPostInit) == null ? void 0 : _this$onPostInit.call(this);
|
|
469
|
-
case 13:
|
|
470
|
-
return _context.abrupt("return", this.isInitialized);
|
|
471
|
-
case 14:
|
|
472
|
-
case "end":
|
|
473
|
-
return _context.stop();
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
}, _callee, this, [[0, 7]]);
|
|
477
|
-
}));
|
|
478
|
-
function init() {
|
|
479
|
-
return _init.apply(this, arguments);
|
|
480
|
-
}
|
|
481
|
-
return init;
|
|
482
|
-
}();
|
|
483
|
-
_proto.login = /*#__PURE__*/function () {
|
|
484
|
-
var _login = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(credentials) {
|
|
485
|
-
var _this$onPreLogin, _this$onPostLogin;
|
|
486
|
-
var isSuccess, tokens;
|
|
487
|
-
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
488
|
-
while (1) {
|
|
489
|
-
switch (_context2.prev = _context2.next) {
|
|
490
|
-
case 0:
|
|
491
|
-
this.emit('loginStarted', undefined);
|
|
492
|
-
_context2.next = 3;
|
|
493
|
-
return (_this$onPreLogin = this.onPreLogin) == null ? void 0 : _this$onPreLogin.call(this);
|
|
494
|
-
case 3:
|
|
495
|
-
isSuccess = false;
|
|
496
|
-
_context2.prev = 4;
|
|
497
|
-
_context2.next = 7;
|
|
498
|
-
return this.onLogin(credentials);
|
|
499
|
-
case 7:
|
|
500
|
-
tokens = _context2.sent;
|
|
501
|
-
this.setState({
|
|
502
|
-
isAuthenticated: true,
|
|
503
|
-
tokens: tokens
|
|
504
|
-
});
|
|
505
|
-
this.emit('loginSuccess', undefined);
|
|
506
|
-
isSuccess = true;
|
|
507
|
-
_context2.next = 18;
|
|
508
|
-
break;
|
|
509
|
-
case 13:
|
|
510
|
-
_context2.prev = 13;
|
|
511
|
-
_context2.t0 = _context2["catch"](4);
|
|
512
|
-
this.setState({
|
|
513
|
-
isAuthenticated: false,
|
|
514
|
-
tokens: {}
|
|
515
|
-
});
|
|
516
|
-
this.emit('loginFailed', _context2.t0);
|
|
517
|
-
isSuccess = false;
|
|
518
|
-
case 18:
|
|
519
|
-
_context2.next = 20;
|
|
520
|
-
return (_this$onPostLogin = this.onPostLogin) == null ? void 0 : _this$onPostLogin.call(this, isSuccess);
|
|
521
|
-
case 20:
|
|
522
|
-
return _context2.abrupt("return", this.isAuthenticated);
|
|
523
|
-
case 21:
|
|
524
|
-
case "end":
|
|
525
|
-
return _context2.stop();
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
}, _callee2, this, [[4, 13]]);
|
|
529
|
-
}));
|
|
530
|
-
function login(_x) {
|
|
531
|
-
return _login.apply(this, arguments);
|
|
532
|
-
}
|
|
533
|
-
return login;
|
|
534
|
-
}();
|
|
535
|
-
_proto.refresh = /*#__PURE__*/function () {
|
|
536
|
-
var _refresh = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(minValidity) {
|
|
537
|
-
var deferred;
|
|
538
|
-
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
539
|
-
while (1) {
|
|
540
|
-
switch (_context3.prev = _context3.next) {
|
|
541
|
-
case 0:
|
|
542
|
-
deferred = new Deferred();
|
|
543
|
-
this.runRefresh(deferred, minValidity);
|
|
544
|
-
return _context3.abrupt("return", deferred.getPromise());
|
|
545
|
-
case 3:
|
|
546
|
-
case "end":
|
|
547
|
-
return _context3.stop();
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
}, _callee3, this);
|
|
551
|
-
}));
|
|
552
|
-
function refresh(_x2) {
|
|
553
|
-
return _refresh.apply(this, arguments);
|
|
554
|
-
}
|
|
555
|
-
return refresh;
|
|
556
|
-
}();
|
|
557
|
-
_proto.logout = /*#__PURE__*/function () {
|
|
558
|
-
var _logout = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
|
|
559
|
-
var _this$onPreLogout, _this$onPostLogout;
|
|
560
|
-
var isSuccess;
|
|
561
|
-
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
562
|
-
while (1) {
|
|
563
|
-
switch (_context4.prev = _context4.next) {
|
|
564
|
-
case 0:
|
|
565
|
-
this.emit('logoutStarted', undefined);
|
|
566
|
-
_context4.next = 3;
|
|
567
|
-
return (_this$onPreLogout = this.onPreLogout) == null ? void 0 : _this$onPreLogout.call(this);
|
|
568
|
-
case 3:
|
|
569
|
-
isSuccess = false;
|
|
570
|
-
_context4.prev = 4;
|
|
571
|
-
_context4.next = 7;
|
|
572
|
-
return this.onLogout();
|
|
573
|
-
case 7:
|
|
574
|
-
this.setState({
|
|
575
|
-
isAuthenticated: false,
|
|
576
|
-
tokens: {}
|
|
577
|
-
});
|
|
578
|
-
this.emit('logoutSuccess', undefined);
|
|
579
|
-
isSuccess = true;
|
|
580
|
-
_context4.next = 16;
|
|
581
|
-
break;
|
|
582
|
-
case 12:
|
|
583
|
-
_context4.prev = 12;
|
|
584
|
-
_context4.t0 = _context4["catch"](4);
|
|
585
|
-
this.emit('logoutFailed', _context4.t0);
|
|
586
|
-
isSuccess = false;
|
|
587
|
-
case 16:
|
|
588
|
-
_context4.next = 18;
|
|
589
|
-
return (_this$onPostLogout = this.onPostLogout) == null ? void 0 : _this$onPostLogout.call(this, isSuccess);
|
|
590
|
-
case 18:
|
|
591
|
-
case "end":
|
|
592
|
-
return _context4.stop();
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
}, _callee4, this, [[4, 12]]);
|
|
596
|
-
}));
|
|
597
|
-
function logout() {
|
|
598
|
-
return _logout.apply(this, arguments);
|
|
599
|
-
}
|
|
600
|
-
return logout;
|
|
601
|
-
}();
|
|
602
|
-
_proto.on = function on(eventName, listener) {
|
|
603
|
-
this.eventEmitter.on(eventName, listener);
|
|
604
|
-
};
|
|
605
|
-
_proto.off = function off(eventName, listener) {
|
|
606
|
-
this.eventEmitter.off(eventName, listener);
|
|
607
|
-
}
|
|
608
|
-
//
|
|
609
|
-
// Protected methods
|
|
610
|
-
//
|
|
611
|
-
;
|
|
612
|
-
_proto.setState = function setState(stateUpdate) {
|
|
613
|
-
this._state = _extends({}, this._state, stateUpdate);
|
|
614
|
-
this.notifySubscribers();
|
|
615
|
-
}
|
|
616
|
-
//
|
|
617
|
-
// Private methods
|
|
618
|
-
//
|
|
619
|
-
;
|
|
620
|
-
_proto.runRefresh =
|
|
621
|
-
/*#__PURE__*/
|
|
622
|
-
function () {
|
|
623
|
-
var _runRefresh = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(deferred, minValidity) {
|
|
624
|
-
var _this$onPreRefresh, _this$onPostRefresh;
|
|
625
|
-
var isAuthenticated, tokens, p;
|
|
626
|
-
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
627
|
-
while (1) {
|
|
628
|
-
switch (_context5.prev = _context5.next) {
|
|
629
|
-
case 0:
|
|
630
|
-
// Add deferred Promise to refresh queue
|
|
631
|
-
this.refreshQ.push(deferred);
|
|
632
|
-
// If refresh queue already has promises enqueued do not attempt a new refresh - one is already in progress
|
|
633
|
-
if (!(this.refreshQ.length !== 1)) {
|
|
634
|
-
_context5.next = 3;
|
|
635
|
-
break;
|
|
636
|
-
}
|
|
637
|
-
return _context5.abrupt("return");
|
|
638
|
-
case 3:
|
|
639
|
-
this.emit('refreshStarted', undefined);
|
|
640
|
-
_context5.next = 6;
|
|
641
|
-
return (_this$onPreRefresh = this.onPreRefresh) == null ? void 0 : _this$onPreRefresh.call(this);
|
|
642
|
-
case 6:
|
|
643
|
-
isAuthenticated = false;
|
|
644
|
-
tokens = {};
|
|
645
|
-
_context5.prev = 8;
|
|
646
|
-
_context5.next = 11;
|
|
647
|
-
return this.onRefresh(minValidity);
|
|
648
|
-
case 11:
|
|
649
|
-
tokens = _context5.sent;
|
|
650
|
-
isAuthenticated = true;
|
|
651
|
-
this.emit('refreshSuccess', undefined);
|
|
652
|
-
_context5.next = 20;
|
|
653
|
-
break;
|
|
654
|
-
case 16:
|
|
655
|
-
_context5.prev = 16;
|
|
656
|
-
_context5.t0 = _context5["catch"](8);
|
|
657
|
-
isAuthenticated = false;
|
|
658
|
-
this.emit('refreshFailed', _context5.t0);
|
|
659
|
-
case 20:
|
|
660
|
-
this.setState({
|
|
661
|
-
isAuthenticated: isAuthenticated,
|
|
662
|
-
tokens: tokens
|
|
663
|
-
});
|
|
664
|
-
_context5.next = 23;
|
|
665
|
-
return (_this$onPostRefresh = this.onPostRefresh) == null ? void 0 : _this$onPostRefresh.call(this, isAuthenticated);
|
|
666
|
-
case 23:
|
|
667
|
-
for (p = this.refreshQ.pop(); p != null; p = this.refreshQ.pop()) {
|
|
668
|
-
p.resolve(isAuthenticated);
|
|
669
|
-
}
|
|
670
|
-
case 24:
|
|
671
|
-
case "end":
|
|
672
|
-
return _context5.stop();
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
}, _callee5, this, [[8, 16]]);
|
|
676
|
-
}));
|
|
677
|
-
function runRefresh(_x3, _x4) {
|
|
678
|
-
return _runRefresh.apply(this, arguments);
|
|
679
|
-
}
|
|
680
|
-
return runRefresh;
|
|
681
|
-
}();
|
|
682
|
-
_proto.emit = function emit(eventName, error) {
|
|
683
|
-
this.eventEmitter.emit(eventName, error);
|
|
684
|
-
};
|
|
685
|
-
_proto.notifySubscribers = function notifySubscribers() {
|
|
686
|
-
this.subscribers.forEach(function (s) {
|
|
687
|
-
try {
|
|
688
|
-
s();
|
|
689
|
-
} catch (_unused) {}
|
|
690
|
-
});
|
|
691
|
-
};
|
|
692
|
-
_createClass(BaseAuthClient, [{
|
|
693
|
-
key: "isInitialized",
|
|
694
|
-
get: function get() {
|
|
695
|
-
return this._state.isInitialized;
|
|
696
|
-
}
|
|
697
|
-
}, {
|
|
698
|
-
key: "isAuthenticated",
|
|
699
|
-
get: function get() {
|
|
700
|
-
return this._state.isAuthenticated;
|
|
701
|
-
}
|
|
702
|
-
}, {
|
|
703
|
-
key: "tokens",
|
|
704
|
-
get: function get() {
|
|
705
|
-
return this._state.tokens;
|
|
706
|
-
}
|
|
707
|
-
}]);
|
|
708
|
-
return BaseAuthClient;
|
|
709
|
-
}();
|
|
710
|
-
|
|
711
|
-
function createAuth(authClient) {
|
|
712
|
-
// Create a React context containing a BaseAuthClient instance.
|
|
713
|
-
var authContext = createContext(null);
|
|
714
|
-
// Create the React Context Provider for the BaseAuthClient instance.
|
|
715
|
-
var AuthProvider = function AuthProvider(_ref) {
|
|
716
|
-
var children = _ref.children,
|
|
717
|
-
ErrorComponent = _ref.ErrorComponent,
|
|
718
|
-
LoadingComponent = _ref.LoadingComponent;
|
|
719
|
-
var _useState = useState(false),
|
|
720
|
-
isInitFailed = _useState[0],
|
|
721
|
-
setInitFailed = _useState[1];
|
|
722
|
-
var _useSyncExternalStore = useSyncExternalStore(authClient.subscribe, authClient.getSnapshot),
|
|
723
|
-
isAuthenticated = _useSyncExternalStore.isAuthenticated,
|
|
724
|
-
isInitialized = _useSyncExternalStore.isInitialized;
|
|
725
|
-
useEffect(function () {
|
|
726
|
-
function initAuthClient() {
|
|
727
|
-
return _initAuthClient.apply(this, arguments);
|
|
728
|
-
} // Init AuthClient
|
|
729
|
-
function _initAuthClient() {
|
|
730
|
-
_initAuthClient = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
731
|
-
var initSuccess;
|
|
732
|
-
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
733
|
-
while (1) {
|
|
734
|
-
switch (_context.prev = _context.next) {
|
|
735
|
-
case 0:
|
|
736
|
-
_context.next = 2;
|
|
737
|
-
return authClient.init();
|
|
738
|
-
case 2:
|
|
739
|
-
initSuccess = _context.sent;
|
|
740
|
-
setInitFailed(!initSuccess);
|
|
741
|
-
case 4:
|
|
742
|
-
case "end":
|
|
743
|
-
return _context.stop();
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
}, _callee);
|
|
747
|
-
}));
|
|
748
|
-
return _initAuthClient.apply(this, arguments);
|
|
749
|
-
}
|
|
750
|
-
initAuthClient();
|
|
751
|
-
}, []);
|
|
752
|
-
if (!!ErrorComponent && isInitFailed) {
|
|
753
|
-
return ErrorComponent;
|
|
754
|
-
}
|
|
755
|
-
if (!!LoadingComponent && !isInitialized) {
|
|
756
|
-
return LoadingComponent;
|
|
757
|
-
}
|
|
758
|
-
return React.createElement(authContext.Provider, {
|
|
759
|
-
value: {
|
|
760
|
-
authClient: authClient,
|
|
761
|
-
isAuthenticated: isAuthenticated,
|
|
762
|
-
isInitialized: isInitialized
|
|
763
|
-
}
|
|
764
|
-
}, children);
|
|
765
|
-
};
|
|
766
|
-
// Retrieve the AuthClient from the current context
|
|
767
|
-
var useAuthClient = function useAuthClient() {
|
|
768
|
-
var ctx = useContext(authContext);
|
|
769
|
-
if (!ctx) {
|
|
770
|
-
throw new Error('useAuthClient hook should be used inside AuthProvider');
|
|
771
|
-
}
|
|
772
|
-
return ctx.authClient;
|
|
773
|
-
};
|
|
774
|
-
return {
|
|
775
|
-
AuthProvider: AuthProvider,
|
|
776
|
-
useAuthClient: useAuthClient
|
|
777
|
-
};
|
|
778
|
-
}
|
|
779
|
-
|
|
780
|
-
export { BaseAuthClient, createAuth };
|
|
781
|
-
//# sourceMappingURL=react-auth.esm.js.map
|