@versini/auth0-react-thin 2.19.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.
Files changed (41) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +185 -0
  3. package/dist/auth-state.d.ts +15 -0
  4. package/dist/auth-state.d.ts.map +1 -0
  5. package/dist/auth0-context.d.ts +423 -0
  6. package/dist/auth0-context.d.ts.map +1 -0
  7. package/dist/auth0-provider.d.ts +96 -0
  8. package/dist/auth0-provider.d.ts.map +1 -0
  9. package/dist/auth0-react.cjs.js +830 -0
  10. package/dist/auth0-react.cjs.js.map +1 -0
  11. package/dist/auth0-react.esm.js +723 -0
  12. package/dist/auth0-react.esm.js.map +1 -0
  13. package/dist/auth0-react.js +759 -0
  14. package/dist/auth0-react.js.map +1 -0
  15. package/dist/auth0-react.min.js +2 -0
  16. package/dist/auth0-react.min.js.map +1 -0
  17. package/dist/errors.d.ts +12 -0
  18. package/dist/errors.d.ts.map +1 -0
  19. package/dist/index.d.ts +9 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/reducer.d.ts +19 -0
  22. package/dist/reducer.d.ts.map +1 -0
  23. package/dist/use-auth0.d.ts +29 -0
  24. package/dist/use-auth0.d.ts.map +1 -0
  25. package/dist/utils.d.ts +10 -0
  26. package/dist/utils.d.ts.map +1 -0
  27. package/dist/with-auth0.d.ts +29 -0
  28. package/dist/with-auth0.d.ts.map +1 -0
  29. package/dist/with-authentication-required.d.ts +77 -0
  30. package/dist/with-authentication-required.d.ts.map +1 -0
  31. package/package.json +43 -0
  32. package/src/auth-state.tsx +21 -0
  33. package/src/auth0-context.tsx +506 -0
  34. package/src/auth0-provider.tsx +488 -0
  35. package/src/errors.tsx +14 -0
  36. package/src/index.tsx +100 -0
  37. package/src/reducer.tsx +59 -0
  38. package/src/use-auth0.tsx +34 -0
  39. package/src/utils.tsx +74 -0
  40. package/src/with-auth0.tsx +44 -0
  41. package/src/with-authentication-required.tsx +141 -0
@@ -0,0 +1,723 @@
1
+ import React, { createContext, useState, useReducer, useRef, useCallback, useEffect, useMemo, useContext } from 'react';
2
+ import { Auth0Client, ResponseType } from '@auth0/auth0-spa-js';
3
+ export { AuthenticationError, ConnectError, GenericError, InMemoryCache, LocalStorageCache, MfaChallengeError, MfaEnrollmentError, MfaEnrollmentFactorsError, MfaError, MfaListAuthenticatorsError, MfaRequiredError, MfaVerifyError, MissingRefreshTokenError, MyAccountApiError, PasskeyChallengeError, PasskeyError, PasskeyGetTokenError, PasskeyRegisterError, PopupCancelledError, PopupOpenError, PopupTimeoutError, ResponseType, TimeoutError, UseDpopNonceError, User } from '@auth0/auth0-spa-js';
4
+
5
+ /******************************************************************************
6
+ Copyright (c) Microsoft Corporation.
7
+
8
+ Permission to use, copy, modify, and/or distribute this software for any
9
+ purpose with or without fee is hereby granted.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ ***************************************************************************** */
19
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
20
+
21
+ var extendStatics = function(d, b) {
22
+ extendStatics = Object.setPrototypeOf ||
23
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
24
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
25
+ return extendStatics(d, b);
26
+ };
27
+
28
+ function __extends(d, b) {
29
+ if (typeof b !== "function" && b !== null)
30
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
31
+ extendStatics(d, b);
32
+ function __() { this.constructor = d; }
33
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
34
+ }
35
+
36
+ var __assign = function() {
37
+ __assign = Object.assign || function __assign(t) {
38
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
39
+ s = arguments[i];
40
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
41
+ }
42
+ return t;
43
+ };
44
+ return __assign.apply(this, arguments);
45
+ };
46
+
47
+ function __rest(s, e) {
48
+ var t = {};
49
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
50
+ t[p] = s[p];
51
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
52
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
53
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
54
+ t[p[i]] = s[p[i]];
55
+ }
56
+ return t;
57
+ }
58
+
59
+ function __awaiter(thisArg, _arguments, P, generator) {
60
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
61
+ return new (P || (P = Promise))(function (resolve, reject) {
62
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
63
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
64
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
65
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
66
+ });
67
+ }
68
+
69
+ function __generator(thisArg, body) {
70
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
71
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
72
+ function verb(n) { return function (v) { return step([n, v]); }; }
73
+ function step(op) {
74
+ if (f) throw new TypeError("Generator is already executing.");
75
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
76
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
77
+ if (y = 0, t) op = [op[0] & 2, t.value];
78
+ switch (op[0]) {
79
+ case 0: case 1: t = op; break;
80
+ case 4: _.label++; return { value: op[1], done: false };
81
+ case 5: _.label++; y = op[1]; op = [0]; continue;
82
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
83
+ default:
84
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
85
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
86
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
87
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
88
+ if (t[2]) _.ops.pop();
89
+ _.trys.pop(); continue;
90
+ }
91
+ op = body.call(thisArg, _);
92
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
93
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
94
+ }
95
+ }
96
+
97
+ function __spreadArray(to, from, pack) {
98
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
99
+ if (ar || !(i in from)) {
100
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
101
+ ar[i] = from[i];
102
+ }
103
+ }
104
+ return to.concat(ar || Array.prototype.slice.call(from));
105
+ }
106
+
107
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
108
+ var e = new Error(message);
109
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
110
+ };
111
+
112
+ /**
113
+ * The initial auth state.
114
+ */
115
+ var initialAuthState = {
116
+ isAuthenticated: false,
117
+ isLoading: true,
118
+ error: undefined,
119
+ user: undefined,
120
+ };
121
+
122
+ /**
123
+ * @ignore
124
+ */
125
+ var stub = function () {
126
+ throw new Error('You forgot to wrap your component in <Auth0Provider>.');
127
+ };
128
+ /**
129
+ * @ignore
130
+ */
131
+ var initialContext = __assign(__assign({}, initialAuthState), { buildAuthorizeUrl: stub, buildLogoutUrl: stub, getAccessTokenSilently: stub, getAccessTokenWithPopup: stub, getIdTokenClaims: stub, loginWithCustomTokenExchange: stub, customTokenExchange: stub, exchangeToken: stub, loginWithRedirect: stub, loginWithPopup: stub, connectAccountWithRedirect: stub, logout: stub, handleRedirectCallback: stub, getDpopNonce: stub, setDpopNonce: stub, generateDpopProof: stub, createFetcher: stub, getConfiguration: stub, mfa: {
132
+ getAuthenticators: stub,
133
+ enroll: stub,
134
+ challenge: stub,
135
+ verify: stub,
136
+ getEnrollmentFactors: stub,
137
+ }, passkey: {
138
+ signup: stub,
139
+ login: stub,
140
+ }, myAccount: {
141
+ getFactors: stub,
142
+ getAuthenticationMethods: stub,
143
+ getAuthenticationMethod: stub,
144
+ updateAuthenticationMethod: stub,
145
+ deleteAuthenticationMethod: stub,
146
+ enrollmentChallenge: stub,
147
+ enrollmentVerify: stub,
148
+ } });
149
+ /**
150
+ * The Auth0 Context
151
+ */
152
+ var Auth0Context = createContext(initialContext);
153
+
154
+ /**
155
+ * An OAuth2 error will come from the authorization server and will have at least an `error` property which will
156
+ * be the error code. And possibly an `error_description` property
157
+ *
158
+ * See: https://openid.net/specs/openid-connect-core-1_0.html#rfc.section.3.1.2.6
159
+ */
160
+ var OAuthError = /** @class */ (function (_super) {
161
+ __extends(OAuthError, _super);
162
+ function OAuthError(error, error_description) {
163
+ var _this = _super.call(this, error_description !== null && error_description !== void 0 ? error_description : error) || this;
164
+ _this.error = error;
165
+ _this.error_description = error_description;
166
+ // https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
167
+ Object.setPrototypeOf(_this, OAuthError.prototype);
168
+ return _this;
169
+ }
170
+ return OAuthError;
171
+ }(Error));
172
+
173
+ var CODE_RE = /[?&](?:connect_)?code=[^&]+/;
174
+ var STATE_RE = /[?&]state=[^&]+/;
175
+ var ERROR_RE = /[?&]error=[^&]+/;
176
+ var hasAuthParams = function (searchParams) {
177
+ if (searchParams === void 0) { searchParams = window.location.search; }
178
+ return (CODE_RE.test(searchParams) || ERROR_RE.test(searchParams)) &&
179
+ STATE_RE.test(searchParams);
180
+ };
181
+ var normalizeErrorFn = function (fallbackMessage) {
182
+ return function (error) {
183
+ if (error instanceof Error) {
184
+ return error;
185
+ }
186
+ // try to check errors of the following form: {error: string; error_description?: string}
187
+ if (error !== null &&
188
+ typeof error === 'object' &&
189
+ 'error' in error &&
190
+ typeof error.error === 'string') {
191
+ if ('error_description' in error &&
192
+ typeof error.error_description === 'string') {
193
+ var e_1 = error;
194
+ return new OAuthError(e_1.error, e_1.error_description);
195
+ }
196
+ var e = error;
197
+ return new OAuthError(e.error);
198
+ }
199
+ return new Error(fallbackMessage);
200
+ };
201
+ };
202
+ var loginError = normalizeErrorFn('Login failed');
203
+ var tokenError = normalizeErrorFn('Get access token failed');
204
+ /**
205
+ * @ignore
206
+ * Helper function to map the v1 `redirectUri` option to the v2 `authorizationParams.redirect_uri`
207
+ * and log a warning.
208
+ */
209
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
210
+ var deprecateRedirectUri = function (options) {
211
+ var _a, _b;
212
+ if (options === null || options === void 0 ? void 0 : options.redirectUri) {
213
+ console.warn('Using `redirectUri` has been deprecated, please use `authorizationParams.redirect_uri` instead as `redirectUri` will be no longer supported in a future version');
214
+ options.authorizationParams = (_a = options.authorizationParams) !== null && _a !== void 0 ? _a : {};
215
+ options.authorizationParams.redirect_uri = options.redirectUri;
216
+ delete options.redirectUri;
217
+ }
218
+ if ((_b = options === null || options === void 0 ? void 0 : options.authorizationParams) === null || _b === void 0 ? void 0 : _b.redirectUri) {
219
+ console.warn('Using `authorizationParams.redirectUri` has been deprecated, please use `authorizationParams.redirect_uri` instead as `authorizationParams.redirectUri` will be removed in a future version');
220
+ options.authorizationParams.redirect_uri =
221
+ options.authorizationParams.redirectUri;
222
+ delete options.authorizationParams.redirectUri;
223
+ }
224
+ };
225
+
226
+ /**
227
+ * Handles how that state changes in the `useAuth0` hook.
228
+ */
229
+ var reducer = function (state, action) {
230
+ switch (action.type) {
231
+ case 'LOGIN_POPUP_STARTED':
232
+ return __assign(__assign({}, state), { isLoading: true });
233
+ case 'LOGIN_POPUP_COMPLETE':
234
+ case 'INITIALISED':
235
+ return __assign(__assign({}, state), { isAuthenticated: !!action.user, user: action.user, isLoading: false, error: undefined });
236
+ case 'HANDLE_REDIRECT_COMPLETE':
237
+ case 'GET_ACCESS_TOKEN_COMPLETE':
238
+ if (state.user === action.user) {
239
+ return state;
240
+ }
241
+ return __assign(__assign({}, state), { isAuthenticated: !!action.user, user: action.user });
242
+ case 'LOGOUT':
243
+ return __assign(__assign({}, state), { isAuthenticated: false, user: undefined });
244
+ case 'ERROR':
245
+ return __assign(__assign({}, state), { isLoading: false, error: action.error });
246
+ }
247
+ };
248
+
249
+ /**
250
+ * @ignore
251
+ */
252
+ var toAuth0ClientOptions = function (opts) {
253
+ deprecateRedirectUri(opts);
254
+ return __assign(__assign({}, opts), { auth0Client: {
255
+ name: 'auth0-react',
256
+ version: '2.19.0',
257
+ } });
258
+ };
259
+ /**
260
+ * @ignore
261
+ */
262
+ var defaultOnRedirectCallback = function (appState) {
263
+ var _a;
264
+ window.history.replaceState({}, document.title, (_a = appState.returnTo) !== null && _a !== void 0 ? _a : window.location.pathname);
265
+ };
266
+ /**
267
+ * ```jsx
268
+ * <Auth0Provider
269
+ * domain={domain}
270
+ * clientId={clientId}
271
+ * authorizationParams={{ redirect_uri: window.location.origin }}>
272
+ * <MyApp />
273
+ * </Auth0Provider>
274
+ * ```
275
+ *
276
+ * Provides the Auth0Context to its child components.
277
+ */
278
+ var Auth0Provider = function (opts) {
279
+ var _a = opts, children = _a.children, skipRedirectCallback = _a.skipRedirectCallback, _b = _a.onRedirectCallback, onRedirectCallback = _b === void 0 ? defaultOnRedirectCallback : _b, _c = _a.context, context = _c === void 0 ? Auth0Context : _c, providedClient = _a.client, clientOpts = __rest(_a, ["children", "skipRedirectCallback", "onRedirectCallback", "context", "client"]);
280
+ if (providedClient && (clientOpts.domain || clientOpts.clientId)) {
281
+ console.warn('Auth0Provider: the `client` prop takes precedence over `domain`/`clientId` and other ' +
282
+ 'configuration options. Remove `domain`, `clientId`, and any other Auth0Client configuration ' +
283
+ 'props when using the `client` prop.');
284
+ }
285
+ var client = useState(function () { return providedClient !== null && providedClient !== void 0 ? providedClient : new Auth0Client(toAuth0ClientOptions(clientOpts)); })[0];
286
+ var _d = useReducer((reducer), initialAuthState), state = _d[0], dispatch = _d[1];
287
+ var didInitialise = useRef(false);
288
+ var handleError = useCallback(function (error) {
289
+ dispatch({ type: 'ERROR', error: error });
290
+ return error;
291
+ }, []);
292
+ useEffect(function () {
293
+ if (didInitialise.current) {
294
+ return;
295
+ }
296
+ didInitialise.current = true;
297
+ (function () { return __awaiter(void 0, void 0, void 0, function () {
298
+ var user, _a, _b, appState, response_type, result, error_1;
299
+ return __generator(this, function (_c) {
300
+ switch (_c.label) {
301
+ case 0:
302
+ _c.trys.push([0, 7, , 8]);
303
+ user = void 0;
304
+ if (!(hasAuthParams() && !skipRedirectCallback)) return [3 /*break*/, 3];
305
+ return [4 /*yield*/, client.handleRedirectCallback()];
306
+ case 1:
307
+ _a = _c.sent(), _b = _a.appState, appState = _b === void 0 ? {} : _b, response_type = _a.response_type, result = __rest(_a, ["appState", "response_type"]);
308
+ return [4 /*yield*/, client.getUser()];
309
+ case 2:
310
+ user = _c.sent();
311
+ appState.response_type = response_type;
312
+ if (response_type === ResponseType.ConnectCode) {
313
+ appState.connectedAccount = result;
314
+ }
315
+ onRedirectCallback(appState, user);
316
+ return [3 /*break*/, 6];
317
+ case 3: return [4 /*yield*/, client.checkSession()];
318
+ case 4:
319
+ _c.sent();
320
+ return [4 /*yield*/, client.getUser()];
321
+ case 5:
322
+ user = _c.sent();
323
+ _c.label = 6;
324
+ case 6:
325
+ dispatch({ type: 'INITIALISED', user: user });
326
+ return [3 /*break*/, 8];
327
+ case 7:
328
+ error_1 = _c.sent();
329
+ handleError(loginError(error_1));
330
+ return [3 /*break*/, 8];
331
+ case 8: return [2 /*return*/];
332
+ }
333
+ });
334
+ }); })();
335
+ }, [client, onRedirectCallback, skipRedirectCallback, handleError]);
336
+ var loginWithRedirect = useCallback(function (opts) {
337
+ deprecateRedirectUri(opts);
338
+ return client.loginWithRedirect(opts);
339
+ }, [client]);
340
+ var loginWithPopup = useCallback(function (options, config) { return __awaiter(void 0, void 0, void 0, function () {
341
+ var error_2, user;
342
+ return __generator(this, function (_a) {
343
+ switch (_a.label) {
344
+ case 0:
345
+ dispatch({ type: 'LOGIN_POPUP_STARTED' });
346
+ _a.label = 1;
347
+ case 1:
348
+ _a.trys.push([1, 3, , 4]);
349
+ return [4 /*yield*/, client.loginWithPopup(options, config)];
350
+ case 2:
351
+ _a.sent();
352
+ return [3 /*break*/, 4];
353
+ case 3:
354
+ error_2 = _a.sent();
355
+ handleError(loginError(error_2));
356
+ return [2 /*return*/];
357
+ case 4: return [4 /*yield*/, client.getUser()];
358
+ case 5:
359
+ user = _a.sent();
360
+ dispatch({ type: 'LOGIN_POPUP_COMPLETE', user: user });
361
+ return [2 /*return*/];
362
+ }
363
+ });
364
+ }); }, [client, handleError]);
365
+ var logout = useCallback(function () {
366
+ var args_1 = [];
367
+ for (var _i = 0; _i < arguments.length; _i++) {
368
+ args_1[_i] = arguments[_i];
369
+ }
370
+ return __awaiter(void 0, __spreadArray([], args_1, true), void 0, function (opts) {
371
+ if (opts === void 0) { opts = {}; }
372
+ return __generator(this, function (_a) {
373
+ switch (_a.label) {
374
+ case 0: return [4 /*yield*/, client.logout(opts)];
375
+ case 1:
376
+ _a.sent();
377
+ if (opts.openUrl || opts.openUrl === false) {
378
+ dispatch({ type: 'LOGOUT' });
379
+ }
380
+ return [2 /*return*/];
381
+ }
382
+ });
383
+ });
384
+ }, [client]);
385
+ var getAccessTokenSilently = useCallback(
386
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
387
+ function (opts) { return __awaiter(void 0, void 0, void 0, function () {
388
+ var token, error_3, _a;
389
+ var _b;
390
+ return __generator(this, function (_c) {
391
+ switch (_c.label) {
392
+ case 0:
393
+ _c.trys.push([0, 2, 3, 5]);
394
+ return [4 /*yield*/, client.getTokenSilently(opts)];
395
+ case 1:
396
+ token = _c.sent();
397
+ return [3 /*break*/, 5];
398
+ case 2:
399
+ error_3 = _c.sent();
400
+ throw tokenError(error_3);
401
+ case 3:
402
+ _a = dispatch;
403
+ _b = {
404
+ type: 'GET_ACCESS_TOKEN_COMPLETE'
405
+ };
406
+ return [4 /*yield*/, client.getUser()];
407
+ case 4:
408
+ _a.apply(void 0, [(_b.user = _c.sent(),
409
+ _b)]);
410
+ return [7 /*endfinally*/];
411
+ case 5: return [2 /*return*/, token];
412
+ }
413
+ });
414
+ }); }, [client]);
415
+ var getAccessTokenWithPopup = useCallback(function (opts, config) { return __awaiter(void 0, void 0, void 0, function () {
416
+ var token, error_4, _a;
417
+ var _b;
418
+ return __generator(this, function (_c) {
419
+ switch (_c.label) {
420
+ case 0:
421
+ _c.trys.push([0, 2, 3, 5]);
422
+ return [4 /*yield*/, client.getTokenWithPopup(opts, config)];
423
+ case 1:
424
+ token = _c.sent();
425
+ return [3 /*break*/, 5];
426
+ case 2:
427
+ error_4 = _c.sent();
428
+ throw tokenError(error_4);
429
+ case 3:
430
+ _a = dispatch;
431
+ _b = {
432
+ type: 'GET_ACCESS_TOKEN_COMPLETE'
433
+ };
434
+ return [4 /*yield*/, client.getUser()];
435
+ case 4:
436
+ _a.apply(void 0, [(_b.user = _c.sent(),
437
+ _b)]);
438
+ return [7 /*endfinally*/];
439
+ case 5: return [2 /*return*/, token];
440
+ }
441
+ });
442
+ }); }, [client]);
443
+ var connectAccountWithRedirect = useCallback(function (options) {
444
+ return client.connectAccountWithRedirect(options);
445
+ }, [client]);
446
+ var getIdTokenClaims = useCallback(function () { return client.getIdTokenClaims(); }, [client]);
447
+ var loginWithCustomTokenExchange = useCallback(function (options) { return __awaiter(void 0, void 0, void 0, function () {
448
+ var tokenResponse, error_5, _a;
449
+ var _b;
450
+ return __generator(this, function (_c) {
451
+ switch (_c.label) {
452
+ case 0:
453
+ _c.trys.push([0, 2, 3, 5]);
454
+ return [4 /*yield*/, client.loginWithCustomTokenExchange(options)];
455
+ case 1:
456
+ tokenResponse = _c.sent();
457
+ return [3 /*break*/, 5];
458
+ case 2:
459
+ error_5 = _c.sent();
460
+ throw tokenError(error_5);
461
+ case 3:
462
+ // We dispatch the standard GET_ACCESS_TOKEN_COMPLETE action here to maintain
463
+ // backward compatibility and consistency with the getAccessTokenSilently flow.
464
+ // This ensures the SDK's internal state lifecycle (loading/user updates) remains
465
+ // identical regardless of whether the token was retrieved via silent auth or CTE.
466
+ _a = dispatch;
467
+ _b = {
468
+ type: 'GET_ACCESS_TOKEN_COMPLETE'
469
+ };
470
+ return [4 /*yield*/, client.getUser()];
471
+ case 4:
472
+ // We dispatch the standard GET_ACCESS_TOKEN_COMPLETE action here to maintain
473
+ // backward compatibility and consistency with the getAccessTokenSilently flow.
474
+ // This ensures the SDK's internal state lifecycle (loading/user updates) remains
475
+ // identical regardless of whether the token was retrieved via silent auth or CTE.
476
+ _a.apply(void 0, [(_b.user = _c.sent(),
477
+ _b)]);
478
+ return [7 /*endfinally*/];
479
+ case 5: return [2 /*return*/, tokenResponse];
480
+ }
481
+ });
482
+ }); }, [client]);
483
+ var customTokenExchange = useCallback(function (options) {
484
+ return client.customTokenExchange(options);
485
+ }, [client]);
486
+ var exchangeToken = useCallback(function (options) { return __awaiter(void 0, void 0, void 0, function () {
487
+ return __generator(this, function (_a) {
488
+ return [2 /*return*/, loginWithCustomTokenExchange(options)];
489
+ });
490
+ }); }, [loginWithCustomTokenExchange]);
491
+ var handleRedirectCallback = useCallback(function (url) { return __awaiter(void 0, void 0, void 0, function () {
492
+ var error_6, _a;
493
+ var _b;
494
+ return __generator(this, function (_c) {
495
+ switch (_c.label) {
496
+ case 0:
497
+ _c.trys.push([0, 2, 3, 5]);
498
+ return [4 /*yield*/, client.handleRedirectCallback(url)];
499
+ case 1: return [2 /*return*/, _c.sent()];
500
+ case 2:
501
+ error_6 = _c.sent();
502
+ throw tokenError(error_6);
503
+ case 3:
504
+ _a = dispatch;
505
+ _b = {
506
+ type: 'HANDLE_REDIRECT_COMPLETE'
507
+ };
508
+ return [4 /*yield*/, client.getUser()];
509
+ case 4:
510
+ _a.apply(void 0, [(_b.user = _c.sent(),
511
+ _b)]);
512
+ return [7 /*endfinally*/];
513
+ case 5: return [2 /*return*/];
514
+ }
515
+ });
516
+ }); }, [client]);
517
+ var getDpopNonce = useCallback(function (id) { return client.getDpopNonce(id); }, [client]);
518
+ var setDpopNonce = useCallback(function (nonce, id) { return client.setDpopNonce(nonce, id); }, [client]);
519
+ var generateDpopProof = useCallback(function (params) { return client.generateDpopProof(params); }, [client]);
520
+ var createFetcher = useCallback(function (config) { return client.createFetcher(config); }, [client]);
521
+ var getConfiguration = useCallback(function () { return client.getConfiguration(); }, [client]);
522
+ var mfa = useMemo(function () { return client.mfa; }, [client]);
523
+ var myAccount = useMemo(function () { return client.myAccount; }, [client]);
524
+ var passkeySignup = useCallback(function (options) { return __awaiter(void 0, void 0, void 0, function () {
525
+ var tokenResponse, error_7, _a;
526
+ var _b;
527
+ return __generator(this, function (_c) {
528
+ switch (_c.label) {
529
+ case 0:
530
+ _c.trys.push([0, 2, 3, 5]);
531
+ return [4 /*yield*/, client.passkey.signup(options)];
532
+ case 1:
533
+ tokenResponse = _c.sent();
534
+ return [3 /*break*/, 5];
535
+ case 2:
536
+ error_7 = _c.sent();
537
+ throw tokenError(error_7);
538
+ case 3:
539
+ _a = dispatch;
540
+ _b = {
541
+ type: 'GET_ACCESS_TOKEN_COMPLETE'
542
+ };
543
+ return [4 /*yield*/, client.getUser()];
544
+ case 4:
545
+ _a.apply(void 0, [(_b.user = _c.sent(),
546
+ _b)]);
547
+ return [7 /*endfinally*/];
548
+ case 5: return [2 /*return*/, tokenResponse];
549
+ }
550
+ });
551
+ }); }, [client]);
552
+ var passkeyLogin = useCallback(function (options) { return __awaiter(void 0, void 0, void 0, function () {
553
+ var tokenResponse, error_8, _a;
554
+ var _b;
555
+ return __generator(this, function (_c) {
556
+ switch (_c.label) {
557
+ case 0:
558
+ _c.trys.push([0, 2, 3, 5]);
559
+ return [4 /*yield*/, client.passkey.login(options)];
560
+ case 1:
561
+ tokenResponse = _c.sent();
562
+ return [3 /*break*/, 5];
563
+ case 2:
564
+ error_8 = _c.sent();
565
+ throw tokenError(error_8);
566
+ case 3:
567
+ _a = dispatch;
568
+ _b = {
569
+ type: 'GET_ACCESS_TOKEN_COMPLETE'
570
+ };
571
+ return [4 /*yield*/, client.getUser()];
572
+ case 4:
573
+ _a.apply(void 0, [(_b.user = _c.sent(),
574
+ _b)]);
575
+ return [7 /*endfinally*/];
576
+ case 5: return [2 /*return*/, tokenResponse];
577
+ }
578
+ });
579
+ }); }, [client]);
580
+ var passkey = useMemo(function () { return ({ signup: passkeySignup, login: passkeyLogin }); }, [passkeySignup, passkeyLogin]);
581
+ var contextValue = useMemo(function () {
582
+ return __assign(__assign({}, state), { getAccessTokenSilently: getAccessTokenSilently, getAccessTokenWithPopup: getAccessTokenWithPopup, getIdTokenClaims: getIdTokenClaims, loginWithCustomTokenExchange: loginWithCustomTokenExchange, customTokenExchange: customTokenExchange, exchangeToken: exchangeToken, loginWithRedirect: loginWithRedirect, loginWithPopup: loginWithPopup, connectAccountWithRedirect: connectAccountWithRedirect, logout: logout, handleRedirectCallback: handleRedirectCallback, getDpopNonce: getDpopNonce, setDpopNonce: setDpopNonce, generateDpopProof: generateDpopProof, createFetcher: createFetcher, getConfiguration: getConfiguration, mfa: mfa, passkey: passkey, myAccount: myAccount });
583
+ }, [
584
+ state,
585
+ getAccessTokenSilently,
586
+ getAccessTokenWithPopup,
587
+ getIdTokenClaims,
588
+ loginWithCustomTokenExchange,
589
+ customTokenExchange,
590
+ exchangeToken,
591
+ loginWithRedirect,
592
+ loginWithPopup,
593
+ connectAccountWithRedirect,
594
+ logout,
595
+ handleRedirectCallback,
596
+ getDpopNonce,
597
+ setDpopNonce,
598
+ generateDpopProof,
599
+ createFetcher,
600
+ getConfiguration,
601
+ mfa,
602
+ passkey,
603
+ myAccount,
604
+ ]);
605
+ return React.createElement(context.Provider, { value: contextValue }, children);
606
+ };
607
+
608
+ /**
609
+ * ```js
610
+ * const {
611
+ * // Auth state:
612
+ * error,
613
+ * isAuthenticated,
614
+ * isLoading,
615
+ * user,
616
+ * // Auth methods:
617
+ * getAccessTokenSilently,
618
+ * getAccessTokenWithPopup,
619
+ * getIdTokenClaims,
620
+ * loginWithCustomTokenExchange,
621
+ * exchangeToken, // deprecated - use loginWithCustomTokenExchange
622
+ * loginWithRedirect,
623
+ * loginWithPopup,
624
+ * logout,
625
+ * } = useAuth0<TUser>();
626
+ * ```
627
+ *
628
+ * Use the `useAuth0` hook in your components to access the auth state and methods.
629
+ *
630
+ * TUser is an optional type param to provide a type to the `user` field.
631
+ */
632
+ var useAuth0 = function (context) {
633
+ if (context === void 0) { context = Auth0Context; }
634
+ return useContext(context);
635
+ };
636
+
637
+ /**
638
+ * ```jsx
639
+ * class MyComponent extends Component {
640
+ * render() {
641
+ * // Access the auth context from the `auth0` prop
642
+ * const { user } = this.props.auth0;
643
+ * return <div>Hello {user.name}!</div>
644
+ * }
645
+ * }
646
+ * // Wrap your class component in withAuth0
647
+ * export default withAuth0(MyComponent);
648
+ * ```
649
+ *
650
+ * Wrap your class components in this Higher Order Component to give them access to the Auth0Context.
651
+ *
652
+ * Providing a context as the second argument allows you to configure the Auth0Provider the Auth0Context
653
+ * should come from f you have multiple within your application.
654
+ */
655
+ var withAuth0 = function (Component, context) {
656
+ if (context === void 0) { context = Auth0Context; }
657
+ return function WithAuth(props) {
658
+ return (React.createElement(context.Consumer, null, function (auth) { return (React.createElement(Component, __assign({}, props, { auth0: auth }))); }));
659
+ };
660
+ };
661
+
662
+ /**
663
+ * @ignore
664
+ */
665
+ var defaultOnRedirecting = function () { return React.createElement(React.Fragment, null); };
666
+ /**
667
+ * @ignore
668
+ */
669
+ var defaultOnBeforeAuthentication = function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
670
+ return [2 /*return*/];
671
+ }); }); };
672
+ /**
673
+ * @ignore
674
+ */
675
+ var defaultReturnTo = function () {
676
+ return "".concat(window.location.pathname).concat(window.location.search);
677
+ };
678
+ /**
679
+ * ```js
680
+ * const MyProtectedComponent = withAuthenticationRequired(MyComponent);
681
+ * ```
682
+ *
683
+ * When you wrap your components in this Higher Order Component and an anonymous user visits your component
684
+ * they will be redirected to the login page; after login they will be returned to the page they were redirected from.
685
+ */
686
+ var withAuthenticationRequired = function (Component, options) {
687
+ if (options === void 0) { options = {}; }
688
+ return function WithAuthenticationRequired(props) {
689
+ var _this = this;
690
+ var _a = options.returnTo, returnTo = _a === void 0 ? defaultReturnTo : _a, _b = options.onRedirecting, onRedirecting = _b === void 0 ? defaultOnRedirecting : _b, _c = options.onBeforeAuthentication, onBeforeAuthentication = _c === void 0 ? defaultOnBeforeAuthentication : _c, loginOptions = options.loginOptions, _d = options.context, context = _d === void 0 ? Auth0Context : _d;
691
+ var _e = useAuth0(context), isAuthenticated = _e.isAuthenticated, isLoading = _e.isLoading, loginWithRedirect = _e.loginWithRedirect;
692
+ useEffect(function () {
693
+ if (isLoading || isAuthenticated) {
694
+ return;
695
+ }
696
+ var opts = __assign(__assign({}, loginOptions), { appState: __assign(__assign({}, loginOptions === null || loginOptions === void 0 ? void 0 : loginOptions.appState), { returnTo: typeof returnTo === 'function' ? returnTo() : returnTo }) });
697
+ void (function () { return __awaiter(_this, void 0, void 0, function () {
698
+ return __generator(this, function (_a) {
699
+ switch (_a.label) {
700
+ case 0: return [4 /*yield*/, onBeforeAuthentication()];
701
+ case 1:
702
+ _a.sent();
703
+ return [4 /*yield*/, loginWithRedirect(opts)];
704
+ case 2:
705
+ _a.sent();
706
+ return [2 /*return*/];
707
+ }
708
+ });
709
+ }); })();
710
+ }, [
711
+ isLoading,
712
+ isAuthenticated,
713
+ loginWithRedirect,
714
+ onBeforeAuthentication,
715
+ loginOptions,
716
+ returnTo,
717
+ ]);
718
+ return isAuthenticated ? React.createElement(Component, __assign({}, props)) : onRedirecting();
719
+ };
720
+ };
721
+
722
+ export { Auth0Context, Auth0Provider, OAuthError, initialContext, useAuth0, withAuth0, withAuthenticationRequired };
723
+ //# sourceMappingURL=auth0-react.esm.js.map