@yiminlab/authkit 0.1.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/dist/index.mjs ADDED
@@ -0,0 +1,695 @@
1
+ // src/core/types.ts
2
+ var DEFAULT_AUTH_CONFIG = {
3
+ portalUrl: typeof process !== "undefined" ? process.env.NEXT_PUBLIC_PORTAL_URL || "https://yiminlab.site" : "https://yiminlab.site",
4
+ apiBaseUrl: typeof process !== "undefined" ? process.env.NEXT_PUBLIC_API_BASE_URL || "https://api.yiminlab.site" : "https://api.yiminlab.site",
5
+ callbackPath: "/auth/callback",
6
+ tokenKeys: {
7
+ accessToken: "authkit_access_token",
8
+ refreshToken: "authkit_refresh_token",
9
+ tokenExpiry: "authkit_token_expiry"
10
+ }
11
+ };
12
+
13
+ // src/core/token-manager.ts
14
+ var LocalStorageAdapter = class {
15
+ getItem(key) {
16
+ if (typeof window === "undefined") return null;
17
+ return localStorage.getItem(key);
18
+ }
19
+ setItem(key, value) {
20
+ if (typeof window === "undefined") return;
21
+ localStorage.setItem(key, value);
22
+ }
23
+ removeItem(key) {
24
+ if (typeof window === "undefined") return;
25
+ localStorage.removeItem(key);
26
+ }
27
+ };
28
+ var TokenManager = class {
29
+ constructor(storage, config) {
30
+ this.storage = storage || new LocalStorageAdapter();
31
+ this.config = { ...DEFAULT_AUTH_CONFIG, ...config };
32
+ }
33
+ /**
34
+ * 保存 tokens
35
+ */
36
+ setTokens(accessToken, refreshToken, expiresIn) {
37
+ const expiryTime = Date.now() + expiresIn * 1e3;
38
+ this.storage.setItem(this.config.tokenKeys.accessToken, accessToken);
39
+ this.storage.setItem(this.config.tokenKeys.refreshToken, refreshToken);
40
+ this.storage.setItem(this.config.tokenKeys.tokenExpiry, expiryTime.toString());
41
+ }
42
+ /**
43
+ * 获取 Access Token
44
+ */
45
+ getAccessToken() {
46
+ return this.storage.getItem(this.config.tokenKeys.accessToken);
47
+ }
48
+ /**
49
+ * 获取 Refresh Token
50
+ */
51
+ getRefreshToken() {
52
+ return this.storage.getItem(this.config.tokenKeys.refreshToken);
53
+ }
54
+ /**
55
+ * 获取 Token 过期时间
56
+ */
57
+ getTokenExpiry() {
58
+ const expiry = this.storage.getItem(this.config.tokenKeys.tokenExpiry);
59
+ return expiry ? parseInt(expiry, 10) : null;
60
+ }
61
+ /**
62
+ * 检查 Access Token 是否过期
63
+ * 提前 60 秒认为过期,留出刷新时间
64
+ */
65
+ isAccessTokenExpired() {
66
+ const expiryTime = this.getTokenExpiry();
67
+ if (!expiryTime) return true;
68
+ const now = Date.now();
69
+ return now >= expiryTime - 6e4;
70
+ }
71
+ /**
72
+ * 清除所有 tokens
73
+ */
74
+ clearTokens() {
75
+ this.storage.removeItem(this.config.tokenKeys.accessToken);
76
+ this.storage.removeItem(this.config.tokenKeys.refreshToken);
77
+ this.storage.removeItem(this.config.tokenKeys.tokenExpiry);
78
+ }
79
+ /**
80
+ * 检查是否有有效的认证信息
81
+ */
82
+ hasValidAuth() {
83
+ const accessToken = this.getAccessToken();
84
+ const refreshToken = this.getRefreshToken();
85
+ return !!(accessToken && !this.isAccessTokenExpired()) || !!refreshToken;
86
+ }
87
+ /**
88
+ * 获取配置
89
+ */
90
+ getConfig() {
91
+ return { ...this.config };
92
+ }
93
+ };
94
+ var instance = null;
95
+ function getTokenManager(config) {
96
+ if (!instance) {
97
+ instance = new TokenManager(void 0, config);
98
+ }
99
+ return instance;
100
+ }
101
+ function resetTokenManager() {
102
+ instance = null;
103
+ }
104
+
105
+ // src/core/auth-redirect.ts
106
+ var ALLOWED_REDIRECT_DOMAINS = [
107
+ "yiminlab.site",
108
+ "jsontailor.yiminlab.site",
109
+ "ai.yiminlab.site",
110
+ "localhost"
111
+ ];
112
+ var AuthRedirect = class {
113
+ constructor(config) {
114
+ this.config = {
115
+ portalUrl: config?.portalUrl || DEFAULT_AUTH_CONFIG.portalUrl,
116
+ callbackPath: config?.callbackPath || DEFAULT_AUTH_CONFIG.callbackPath
117
+ };
118
+ }
119
+ /**
120
+ * 获取当前页面的完整 URL
121
+ */
122
+ getCurrentUrl() {
123
+ if (typeof window === "undefined") return "";
124
+ return window.location.href;
125
+ }
126
+ /**
127
+ * 获取当前页面的 origin
128
+ */
129
+ getCurrentOrigin() {
130
+ if (typeof window === "undefined") return "";
131
+ return window.location.origin;
132
+ }
133
+ /**
134
+ * 构建回调 URL
135
+ */
136
+ buildCallbackUrl() {
137
+ return `${this.getCurrentOrigin()}${this.config.callbackPath}`;
138
+ }
139
+ /**
140
+ * 构建登录跳转 URL
141
+ * @param returnPath 登录成功后返回的路径(默认当前页面)
142
+ */
143
+ buildLoginUrl(returnPath) {
144
+ const callbackUrl = this.buildCallbackUrl();
145
+ const loginUrl = new URL("/login", this.config.portalUrl);
146
+ loginUrl.searchParams.set("redirect", callbackUrl);
147
+ if (returnPath) {
148
+ loginUrl.searchParams.set("return", returnPath);
149
+ }
150
+ return loginUrl.toString();
151
+ }
152
+ /**
153
+ * 跳转到 Portal 登录页
154
+ * @param returnPath 登录成功后返回的路径
155
+ */
156
+ redirectToLogin(returnPath) {
157
+ if (typeof window === "undefined") return;
158
+ const currentPath = window.location.pathname + window.location.search;
159
+ const loginUrl = this.buildLoginUrl(returnPath || currentPath);
160
+ window.location.href = loginUrl;
161
+ }
162
+ /**
163
+ * 从 URL 参数中解析 Token
164
+ */
165
+ parseTokensFromUrl() {
166
+ if (typeof window === "undefined") return null;
167
+ const params = new URLSearchParams(window.location.search);
168
+ const accessToken = params.get("token");
169
+ const refreshToken = params.get("refresh");
170
+ const expiresInStr = params.get("expires");
171
+ const returnPath = params.get("return");
172
+ if (!accessToken || !refreshToken) {
173
+ return null;
174
+ }
175
+ return {
176
+ accessToken,
177
+ refreshToken,
178
+ expiresIn: expiresInStr ? parseInt(expiresInStr, 10) : 3600,
179
+ returnPath
180
+ };
181
+ }
182
+ /**
183
+ * 清除 URL 中的 Token 参数
184
+ */
185
+ clearTokensFromUrl() {
186
+ if (typeof window === "undefined") return;
187
+ const url = new URL(window.location.href);
188
+ url.searchParams.delete("token");
189
+ url.searchParams.delete("refresh");
190
+ url.searchParams.delete("expires");
191
+ url.searchParams.delete("return");
192
+ window.history.replaceState({}, "", url.pathname + url.search);
193
+ }
194
+ /**
195
+ * 验证跳转 URL 是否在白名单中
196
+ */
197
+ isAllowedRedirectUrl(url) {
198
+ try {
199
+ const { hostname } = new URL(url);
200
+ return ALLOWED_REDIRECT_DOMAINS.some(
201
+ (domain) => hostname === domain || hostname.endsWith("." + domain)
202
+ );
203
+ } catch {
204
+ return false;
205
+ }
206
+ }
207
+ };
208
+ var instance2 = null;
209
+ function getAuthRedirect(config) {
210
+ if (!instance2) {
211
+ instance2 = new AuthRedirect(config);
212
+ }
213
+ return instance2;
214
+ }
215
+ function resetAuthRedirect() {
216
+ instance2 = null;
217
+ }
218
+
219
+ // src/core/authkit-client.ts
220
+ var AuthKitClient = class {
221
+ constructor(config) {
222
+ this.config = {
223
+ baseUrl: config.baseUrl || DEFAULT_AUTH_CONFIG.apiBaseUrl,
224
+ getAccessToken: config.getAccessToken
225
+ };
226
+ }
227
+ /**
228
+ * 构建请求头
229
+ */
230
+ buildHeaders() {
231
+ const headers = {
232
+ "Content-Type": "application/json"
233
+ };
234
+ const token = this.config.getAccessToken();
235
+ if (token) {
236
+ headers["Authorization"] = `Bearer ${token}`;
237
+ }
238
+ return headers;
239
+ }
240
+ /**
241
+ * 解析 API 响应为标准格式
242
+ */
243
+ parseTokenResponse(data) {
244
+ if (data.access_token) {
245
+ const expiresAt = new Date(Date.now() + data.expires_in * 1e3).toISOString();
246
+ return {
247
+ token: data.access_token,
248
+ expires_at: expiresAt,
249
+ scopes: data.scopes
250
+ };
251
+ }
252
+ throw new Error("Invalid response format");
253
+ }
254
+ /**
255
+ * 获取当前用户信息
256
+ */
257
+ async getMe() {
258
+ const response = await fetch(`${this.config.baseUrl}/api/auth/me`, {
259
+ method: "GET",
260
+ headers: this.buildHeaders()
261
+ });
262
+ if (!response.ok) {
263
+ const error = await response.json().catch(() => ({}));
264
+ throw new Error(error.error || error.message || `HTTP ${response.status}`);
265
+ }
266
+ return await response.json();
267
+ }
268
+ /**
269
+ * 获取全局 Scoped Token(自动创建/续期)
270
+ *
271
+ * @param scope 权限范围,如 'streamock:stream:read'
272
+ * @returns 短 token,如 'st_7kB2xM9pQr3n'
273
+ */
274
+ async getScopedToken(scope) {
275
+ const response = await fetch(
276
+ `${this.config.baseUrl}/api/auth/token/scoped?scope=${encodeURIComponent(scope)}`,
277
+ {
278
+ method: "GET",
279
+ headers: this.buildHeaders()
280
+ }
281
+ );
282
+ if (!response.ok) {
283
+ const error = await response.json().catch(() => ({}));
284
+ throw new Error(error.error || error.message || `HTTP ${response.status}`);
285
+ }
286
+ const data = await response.json();
287
+ return this.parseTokenResponse(data);
288
+ }
289
+ /**
290
+ * 轮换 Scoped Token(废弃旧的,生成新的)
291
+ *
292
+ * @param scope 权限范围,如 'streamock:stream:read'
293
+ * @returns 新的短 token
294
+ */
295
+ async rotateScopedToken(scope) {
296
+ const response = await fetch(
297
+ `${this.config.baseUrl}/api/auth/token/scoped/rotate?scope=${encodeURIComponent(scope)}`,
298
+ {
299
+ method: "POST",
300
+ headers: this.buildHeaders()
301
+ }
302
+ );
303
+ if (!response.ok) {
304
+ const error = await response.json().catch(() => ({}));
305
+ throw new Error(error.error || error.message || `HTTP ${response.status}`);
306
+ }
307
+ const data = await response.json();
308
+ return this.parseTokenResponse(data);
309
+ }
310
+ /**
311
+ * 创建 Scoped Token(短 token)- 保留兼容性
312
+ *
313
+ * @param scopes 权限范围,如 ['streamock:stream:read']
314
+ * @param expiresIn 过期时间(秒),默认 24 小时
315
+ * @returns 短 token,如 'st_7kB2xM9pQr3n'
316
+ * @deprecated 请使用 getScopedToken 获取全局 token
317
+ */
318
+ async createScopedToken(scopes, expiresIn = 86400) {
319
+ const response = await fetch(`${this.config.baseUrl}/api/auth/token/scoped`, {
320
+ method: "POST",
321
+ headers: this.buildHeaders(),
322
+ body: JSON.stringify({
323
+ scopes,
324
+ expires_in: expiresIn
325
+ })
326
+ });
327
+ if (!response.ok) {
328
+ const error = await response.json().catch(() => ({}));
329
+ throw new Error(error.error || error.message || `HTTP ${response.status}`);
330
+ }
331
+ const data = await response.json();
332
+ return this.parseTokenResponse(data);
333
+ }
334
+ };
335
+ var instance3 = null;
336
+ function getAuthKitClient(getAccessToken) {
337
+ if (!instance3) {
338
+ instance3 = new AuthKitClient({ getAccessToken });
339
+ }
340
+ return instance3;
341
+ }
342
+ function resetAuthKitClient() {
343
+ instance3 = null;
344
+ }
345
+
346
+ // src/react/use-auth.ts
347
+ import { useState, useEffect, useCallback, useMemo } from "react";
348
+ function useAuth(options = {}) {
349
+ const {
350
+ tokenManager = getTokenManager(options.config),
351
+ authRedirect = getAuthRedirect(options.config),
352
+ fetchUser = true
353
+ } = options;
354
+ const [authState, setAuthState] = useState({
355
+ isAuthenticated: false,
356
+ isLoading: true,
357
+ user: null,
358
+ error: null
359
+ });
360
+ const fetchUserInfo = useCallback(async () => {
361
+ try {
362
+ const client = getAuthKitClient(() => tokenManager.getAccessToken());
363
+ const user = await client.getMe();
364
+ setAuthState((prev) => ({
365
+ ...prev,
366
+ user,
367
+ error: null
368
+ }));
369
+ } catch (error) {
370
+ console.error("Failed to fetch user info:", error);
371
+ }
372
+ }, [tokenManager]);
373
+ const checkAuth = useCallback(async () => {
374
+ const hasValidAuth = tokenManager.hasValidAuth();
375
+ const accessToken = tokenManager.getAccessToken();
376
+ if (hasValidAuth && accessToken) {
377
+ setAuthState({
378
+ isAuthenticated: true,
379
+ isLoading: false,
380
+ user: null,
381
+ error: null
382
+ });
383
+ if (fetchUser) {
384
+ await fetchUserInfo();
385
+ }
386
+ } else {
387
+ setAuthState({
388
+ isAuthenticated: false,
389
+ isLoading: false,
390
+ user: null,
391
+ error: null
392
+ });
393
+ }
394
+ }, [tokenManager, fetchUser, fetchUserInfo]);
395
+ const login = useCallback(
396
+ (returnPath) => {
397
+ authRedirect.redirectToLogin(returnPath);
398
+ },
399
+ [authRedirect]
400
+ );
401
+ const logout = useCallback(() => {
402
+ tokenManager.clearTokens();
403
+ setAuthState({
404
+ isAuthenticated: false,
405
+ isLoading: false,
406
+ user: null,
407
+ error: null
408
+ });
409
+ }, [tokenManager]);
410
+ const getAccessToken = useCallback(() => {
411
+ return tokenManager.getAccessToken();
412
+ }, [tokenManager]);
413
+ const refresh = useCallback(() => {
414
+ checkAuth();
415
+ }, [checkAuth]);
416
+ const refreshUser = useCallback(async () => {
417
+ await fetchUserInfo();
418
+ }, [fetchUserInfo]);
419
+ useEffect(() => {
420
+ checkAuth();
421
+ }, [checkAuth]);
422
+ useEffect(() => {
423
+ const handleStorageChange = (event) => {
424
+ const config = tokenManager.getConfig();
425
+ if (event.key === config.tokenKeys.accessToken || event.key === config.tokenKeys.refreshToken) {
426
+ checkAuth();
427
+ }
428
+ };
429
+ window.addEventListener("storage", handleStorageChange);
430
+ return () => {
431
+ window.removeEventListener("storage", handleStorageChange);
432
+ };
433
+ }, [tokenManager, checkAuth]);
434
+ return useMemo(
435
+ () => ({
436
+ ...authState,
437
+ login,
438
+ logout,
439
+ getAccessToken,
440
+ refresh,
441
+ refreshUser
442
+ }),
443
+ [authState, login, logout, getAccessToken, refresh, refreshUser]
444
+ );
445
+ }
446
+ function useAccessToken() {
447
+ const { getAccessToken } = useAuth({ fetchUser: false });
448
+ return getAccessToken();
449
+ }
450
+
451
+ // src/react/auth-guard.tsx
452
+ import { useEffect as useEffect2 } from "react";
453
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
454
+ function DefaultLoading() {
455
+ return /* @__PURE__ */ jsxs("div", { style: {
456
+ display: "flex",
457
+ height: "50vh",
458
+ width: "100%",
459
+ alignItems: "center",
460
+ justifyContent: "center"
461
+ }, children: [
462
+ /* @__PURE__ */ jsx("div", { style: {
463
+ width: "32px",
464
+ height: "32px",
465
+ border: "3px solid #e5e7eb",
466
+ borderTopColor: "#3b82f6",
467
+ borderRadius: "50%",
468
+ animation: "spin 1s linear infinite"
469
+ } }),
470
+ /* @__PURE__ */ jsx("style", { children: `
471
+ @keyframes spin {
472
+ to { transform: rotate(360deg); }
473
+ }
474
+ ` })
475
+ ] });
476
+ }
477
+ function DefaultLoginPrompt({
478
+ onLogin,
479
+ title = "\u9700\u8981\u767B\u5F55",
480
+ description = "\u8BF7\u767B\u5F55\u4EE5\u7EE7\u7EED\u4F7F\u7528",
481
+ buttonText = "\u767B\u5F55"
482
+ }) {
483
+ return /* @__PURE__ */ jsxs("div", { style: {
484
+ display: "flex",
485
+ height: "50vh",
486
+ width: "100%",
487
+ flexDirection: "column",
488
+ alignItems: "center",
489
+ justifyContent: "center",
490
+ gap: "16px"
491
+ }, children: [
492
+ /* @__PURE__ */ jsxs("div", { style: { textAlign: "center" }, children: [
493
+ /* @__PURE__ */ jsx("h2", { style: {
494
+ fontSize: "1.5rem",
495
+ fontWeight: 600,
496
+ margin: 0
497
+ }, children: title }),
498
+ /* @__PURE__ */ jsx("p", { style: {
499
+ marginTop: "8px",
500
+ color: "#6b7280"
501
+ }, children: description })
502
+ ] }),
503
+ /* @__PURE__ */ jsx(
504
+ "button",
505
+ {
506
+ onClick: onLogin,
507
+ style: {
508
+ display: "inline-flex",
509
+ alignItems: "center",
510
+ justifyContent: "center",
511
+ padding: "10px 20px",
512
+ fontSize: "1rem",
513
+ fontWeight: 500,
514
+ backgroundColor: "#3b82f6",
515
+ color: "white",
516
+ border: "none",
517
+ borderRadius: "6px",
518
+ cursor: "pointer",
519
+ transition: "background-color 0.2s"
520
+ },
521
+ onMouseOver: (e) => e.currentTarget.style.backgroundColor = "#2563eb",
522
+ onMouseOut: (e) => e.currentTarget.style.backgroundColor = "#3b82f6",
523
+ children: buttonText
524
+ }
525
+ )
526
+ ] });
527
+ }
528
+ function AuthGuard({
529
+ children,
530
+ fallback = "prompt",
531
+ returnPath,
532
+ loadingContent,
533
+ promptContent,
534
+ promptTitle,
535
+ promptDescription,
536
+ loginButtonText,
537
+ authOptions
538
+ }) {
539
+ const { isAuthenticated, isLoading, login } = useAuth(authOptions);
540
+ useEffect2(() => {
541
+ if (!isLoading && !isAuthenticated && fallback === "redirect") {
542
+ login(returnPath);
543
+ }
544
+ }, [isLoading, isAuthenticated, fallback, login, returnPath]);
545
+ if (isLoading) {
546
+ return /* @__PURE__ */ jsx(Fragment, { children: loadingContent || /* @__PURE__ */ jsx(DefaultLoading, {}) });
547
+ }
548
+ if (!isAuthenticated) {
549
+ if (fallback === "redirect") {
550
+ return /* @__PURE__ */ jsx(Fragment, { children: loadingContent || /* @__PURE__ */ jsx(DefaultLoading, {}) });
551
+ }
552
+ return /* @__PURE__ */ jsx(Fragment, { children: promptContent || /* @__PURE__ */ jsx(
553
+ DefaultLoginPrompt,
554
+ {
555
+ onLogin: () => login(returnPath),
556
+ title: promptTitle,
557
+ description: promptDescription,
558
+ buttonText: loginButtonText
559
+ }
560
+ ) });
561
+ }
562
+ return /* @__PURE__ */ jsx(Fragment, { children });
563
+ }
564
+ function withAuthGuard(Component, guardProps) {
565
+ return function WrappedComponent(props) {
566
+ return /* @__PURE__ */ jsx(AuthGuard, { ...guardProps, children: /* @__PURE__ */ jsx(Component, { ...props }) });
567
+ };
568
+ }
569
+
570
+ // src/react/callback-handler.tsx
571
+ import { useEffect as useEffect3, useState as useState2 } from "react";
572
+ import { Fragment as Fragment2, jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
573
+ function DefaultLoading2() {
574
+ return /* @__PURE__ */ jsxs2("div", { style: {
575
+ display: "flex",
576
+ height: "100vh",
577
+ width: "100%",
578
+ alignItems: "center",
579
+ justifyContent: "center",
580
+ flexDirection: "column",
581
+ gap: "16px"
582
+ }, children: [
583
+ /* @__PURE__ */ jsx2("div", { style: {
584
+ width: "40px",
585
+ height: "40px",
586
+ border: "3px solid #e5e7eb",
587
+ borderTopColor: "#3b82f6",
588
+ borderRadius: "50%",
589
+ animation: "spin 1s linear infinite"
590
+ } }),
591
+ /* @__PURE__ */ jsx2("p", { style: { color: "#6b7280" }, children: "\u6B63\u5728\u5904\u7406\u767B\u5F55..." }),
592
+ /* @__PURE__ */ jsx2("style", { children: `
593
+ @keyframes spin {
594
+ to { transform: rotate(360deg); }
595
+ }
596
+ ` })
597
+ ] });
598
+ }
599
+ function DefaultError({ error, onRetry }) {
600
+ return /* @__PURE__ */ jsxs2("div", { style: {
601
+ display: "flex",
602
+ height: "100vh",
603
+ width: "100%",
604
+ alignItems: "center",
605
+ justifyContent: "center",
606
+ flexDirection: "column",
607
+ gap: "16px"
608
+ }, children: [
609
+ /* @__PURE__ */ jsx2("div", { style: {
610
+ color: "#ef4444",
611
+ fontSize: "48px"
612
+ }, children: "\u26A0\uFE0F" }),
613
+ /* @__PURE__ */ jsx2("h2", { style: { margin: 0 }, children: "\u767B\u5F55\u5931\u8D25" }),
614
+ /* @__PURE__ */ jsx2("p", { style: { color: "#6b7280", margin: 0 }, children: error }),
615
+ /* @__PURE__ */ jsx2(
616
+ "button",
617
+ {
618
+ onClick: onRetry,
619
+ style: {
620
+ padding: "10px 20px",
621
+ fontSize: "1rem",
622
+ backgroundColor: "#3b82f6",
623
+ color: "white",
624
+ border: "none",
625
+ borderRadius: "6px",
626
+ cursor: "pointer"
627
+ },
628
+ children: "\u91CD\u8BD5"
629
+ }
630
+ )
631
+ ] });
632
+ }
633
+ function CallbackHandler({
634
+ defaultRedirectPath = "/",
635
+ loadingContent,
636
+ errorContent,
637
+ config,
638
+ onSuccess,
639
+ onError
640
+ }) {
641
+ const [error, setError] = useState2(null);
642
+ useEffect3(() => {
643
+ const handleCallback = () => {
644
+ const tokenManager = getTokenManager(config);
645
+ const authRedirect = getAuthRedirect(config);
646
+ const tokens = authRedirect.parseTokensFromUrl();
647
+ if (!tokens || !tokens.accessToken || !tokens.refreshToken) {
648
+ const errorMsg = "\u672A\u627E\u5230\u8BA4\u8BC1\u4FE1\u606F";
649
+ setError(errorMsg);
650
+ onError?.(errorMsg);
651
+ return;
652
+ }
653
+ tokenManager.setTokens(
654
+ tokens.accessToken,
655
+ tokens.refreshToken,
656
+ tokens.expiresIn || 3600
657
+ );
658
+ authRedirect.clearTokensFromUrl();
659
+ onSuccess?.();
660
+ const redirectPath = tokens.returnPath || defaultRedirectPath;
661
+ window.location.href = redirectPath;
662
+ };
663
+ handleCallback();
664
+ }, [defaultRedirectPath, config, onSuccess, onError]);
665
+ const handleRetry = () => {
666
+ const authRedirect = getAuthRedirect(config);
667
+ authRedirect.redirectToLogin(defaultRedirectPath);
668
+ };
669
+ if (error) {
670
+ if (typeof errorContent === "function") {
671
+ return /* @__PURE__ */ jsx2(Fragment2, { children: errorContent(error) });
672
+ }
673
+ return /* @__PURE__ */ jsx2(Fragment2, { children: errorContent || /* @__PURE__ */ jsx2(DefaultError, { error, onRetry: handleRetry }) });
674
+ }
675
+ return /* @__PURE__ */ jsx2(Fragment2, { children: loadingContent || /* @__PURE__ */ jsx2(DefaultLoading2, {}) });
676
+ }
677
+ export {
678
+ AuthGuard,
679
+ AuthKitClient,
680
+ AuthRedirect,
681
+ CallbackHandler,
682
+ DEFAULT_AUTH_CONFIG,
683
+ LocalStorageAdapter,
684
+ TokenManager,
685
+ getAuthKitClient,
686
+ getAuthRedirect,
687
+ getTokenManager,
688
+ resetAuthKitClient,
689
+ resetAuthRedirect,
690
+ resetTokenManager,
691
+ useAccessToken,
692
+ useAuth,
693
+ withAuthGuard
694
+ };
695
+ //# sourceMappingURL=index.mjs.map