@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.
@@ -0,0 +1,349 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ReactNode } from 'react';
3
+
4
+ /**
5
+ * 认证相关类型定义
6
+ */
7
+ /**
8
+ * 用户信息(来自 /api/auth/me)
9
+ */
10
+ interface User {
11
+ id: string;
12
+ email?: string;
13
+ username?: string;
14
+ display_name?: string;
15
+ avatar_url?: string;
16
+ is_active: boolean;
17
+ email_verified: boolean;
18
+ sync_enabled: boolean;
19
+ }
20
+ /**
21
+ * Token 响应
22
+ */
23
+ interface TokenResponse {
24
+ access_token: string;
25
+ refresh_token: string;
26
+ expires_in: number;
27
+ token_type: string;
28
+ }
29
+ /**
30
+ * 认证响应(登录/注册)
31
+ */
32
+ interface AuthResponse extends TokenResponse {
33
+ user: User;
34
+ }
35
+ /**
36
+ * Token 存储数据
37
+ */
38
+ interface StoredTokens {
39
+ accessToken: string;
40
+ refreshToken: string;
41
+ expiresAt: number;
42
+ }
43
+ /**
44
+ * 认证状态
45
+ */
46
+ interface AuthState {
47
+ isAuthenticated: boolean;
48
+ isLoading: boolean;
49
+ user: User | null;
50
+ error: string | null;
51
+ }
52
+ /**
53
+ * Token 管理器接口(依赖注入)
54
+ */
55
+ interface ITokenManager {
56
+ setTokens(accessToken: string, refreshToken: string, expiresIn: number): void;
57
+ getAccessToken(): string | null;
58
+ getRefreshToken(): string | null;
59
+ isAccessTokenExpired(): boolean;
60
+ clearTokens(): void;
61
+ hasValidAuth(): boolean;
62
+ }
63
+ /**
64
+ * 认证配置
65
+ */
66
+ interface AuthConfig {
67
+ portalUrl: string;
68
+ apiBaseUrl: string;
69
+ callbackPath: string;
70
+ tokenKeys: {
71
+ accessToken: string;
72
+ refreshToken: string;
73
+ tokenExpiry: string;
74
+ };
75
+ }
76
+ /**
77
+ * 默认认证配置
78
+ */
79
+ declare const DEFAULT_AUTH_CONFIG: AuthConfig;
80
+
81
+ /**
82
+ * Token 管理器
83
+ * 提供 token 的存储、获取、验证和清除功能
84
+ *
85
+ * 设计原则:
86
+ * - 依赖注入:支持自定义 storage 和配置
87
+ * - 与 Portal 共享相同的 localStorage key
88
+ * - 支持 SSR(检查 window 对象)
89
+ */
90
+
91
+ /**
92
+ * Storage 接口(依赖注入)
93
+ */
94
+ interface IStorage {
95
+ getItem(key: string): string | null;
96
+ setItem(key: string, value: string): void;
97
+ removeItem(key: string): void;
98
+ }
99
+ /**
100
+ * 浏览器 localStorage 适配器
101
+ */
102
+ declare class LocalStorageAdapter implements IStorage {
103
+ getItem(key: string): string | null;
104
+ setItem(key: string, value: string): void;
105
+ removeItem(key: string): void;
106
+ }
107
+ /**
108
+ * Token 管理器实现
109
+ */
110
+ declare class TokenManager implements ITokenManager {
111
+ private storage;
112
+ private config;
113
+ constructor(storage?: IStorage, config?: Partial<AuthConfig>);
114
+ /**
115
+ * 保存 tokens
116
+ */
117
+ setTokens(accessToken: string, refreshToken: string, expiresIn: number): void;
118
+ /**
119
+ * 获取 Access Token
120
+ */
121
+ getAccessToken(): string | null;
122
+ /**
123
+ * 获取 Refresh Token
124
+ */
125
+ getRefreshToken(): string | null;
126
+ /**
127
+ * 获取 Token 过期时间
128
+ */
129
+ getTokenExpiry(): number | null;
130
+ /**
131
+ * 检查 Access Token 是否过期
132
+ * 提前 60 秒认为过期,留出刷新时间
133
+ */
134
+ isAccessTokenExpired(): boolean;
135
+ /**
136
+ * 清除所有 tokens
137
+ */
138
+ clearTokens(): void;
139
+ /**
140
+ * 检查是否有有效的认证信息
141
+ */
142
+ hasValidAuth(): boolean;
143
+ /**
144
+ * 获取配置
145
+ */
146
+ getConfig(): AuthConfig;
147
+ }
148
+ /**
149
+ * 获取 TokenManager 单例
150
+ */
151
+ declare function getTokenManager(config?: Partial<AuthConfig>): TokenManager;
152
+ /**
153
+ * 重置单例(用于测试)
154
+ */
155
+ declare function resetTokenManager(): void;
156
+
157
+ /**
158
+ * 认证跳转工具
159
+ * 处理跳转到 Portal 登录和回调处理
160
+ */
161
+ /**
162
+ * 认证跳转配置
163
+ */
164
+ interface AuthRedirectConfig {
165
+ portalUrl: string;
166
+ callbackPath: string;
167
+ }
168
+ /**
169
+ * 认证跳转工具类
170
+ */
171
+ declare class AuthRedirect {
172
+ private config;
173
+ constructor(config?: Partial<AuthRedirectConfig>);
174
+ /**
175
+ * 获取当前页面的完整 URL
176
+ */
177
+ private getCurrentUrl;
178
+ /**
179
+ * 获取当前页面的 origin
180
+ */
181
+ private getCurrentOrigin;
182
+ /**
183
+ * 构建回调 URL
184
+ */
185
+ private buildCallbackUrl;
186
+ /**
187
+ * 构建登录跳转 URL
188
+ * @param returnPath 登录成功后返回的路径(默认当前页面)
189
+ */
190
+ buildLoginUrl(returnPath?: string): string;
191
+ /**
192
+ * 跳转到 Portal 登录页
193
+ * @param returnPath 登录成功后返回的路径
194
+ */
195
+ redirectToLogin(returnPath?: string): void;
196
+ /**
197
+ * 从 URL 参数中解析 Token
198
+ */
199
+ parseTokensFromUrl(): {
200
+ accessToken: string | null;
201
+ refreshToken: string | null;
202
+ expiresIn: number | null;
203
+ returnPath: string | null;
204
+ } | null;
205
+ /**
206
+ * 清除 URL 中的 Token 参数
207
+ */
208
+ clearTokensFromUrl(): void;
209
+ /**
210
+ * 验证跳转 URL 是否在白名单中
211
+ */
212
+ isAllowedRedirectUrl(url: string): boolean;
213
+ }
214
+ /**
215
+ * 获取 AuthRedirect 单例
216
+ */
217
+ declare function getAuthRedirect(config?: Partial<AuthRedirectConfig>): AuthRedirect;
218
+ /**
219
+ * 重置单例(用于测试)
220
+ */
221
+ declare function resetAuthRedirect(): void;
222
+
223
+ /**
224
+ * Hook 配置
225
+ */
226
+ interface UseAuthOptions {
227
+ tokenManager?: TokenManager;
228
+ authRedirect?: AuthRedirect;
229
+ config?: Partial<AuthConfig>;
230
+ /**
231
+ * 是否自动获取用户信息
232
+ * @default true
233
+ */
234
+ fetchUser?: boolean;
235
+ }
236
+ /**
237
+ * Hook 返回值
238
+ */
239
+ interface UseAuthReturn extends AuthState {
240
+ /**
241
+ * 登录(跳转到 Portal)
242
+ */
243
+ login: (returnPath?: string) => void;
244
+ /**
245
+ * 登出
246
+ */
247
+ logout: () => void;
248
+ /**
249
+ * 获取 access token
250
+ */
251
+ getAccessToken: () => string | null;
252
+ /**
253
+ * 刷新认证状态
254
+ */
255
+ refresh: () => void;
256
+ /**
257
+ * 刷新用户信息
258
+ */
259
+ refreshUser: () => Promise<void>;
260
+ }
261
+ /**
262
+ * 认证状态 Hook
263
+ */
264
+ declare function useAuth(options?: UseAuthOptions): UseAuthReturn;
265
+ /**
266
+ * 简化版 Hook:只获取 token
267
+ */
268
+ declare function useAccessToken(): string | null;
269
+
270
+ interface AuthGuardProps {
271
+ children: ReactNode;
272
+ /**
273
+ * 未登录时的行为
274
+ * - 'redirect': 自动跳转到登录页
275
+ * - 'prompt': 显示登录提示
276
+ * @default 'prompt'
277
+ */
278
+ fallback?: 'redirect' | 'prompt';
279
+ /**
280
+ * 登录后返回的路径
281
+ */
282
+ returnPath?: string;
283
+ /**
284
+ * 加载中显示的内容
285
+ */
286
+ loadingContent?: ReactNode;
287
+ /**
288
+ * 未登录提示内容
289
+ */
290
+ promptContent?: ReactNode;
291
+ /**
292
+ * 提示标题
293
+ */
294
+ promptTitle?: string;
295
+ /**
296
+ * 提示描述
297
+ */
298
+ promptDescription?: string;
299
+ /**
300
+ * 登录按钮文字
301
+ */
302
+ loginButtonText?: string;
303
+ /**
304
+ * useAuth 配置
305
+ */
306
+ authOptions?: UseAuthOptions;
307
+ }
308
+ /**
309
+ * 认证守卫组件
310
+ */
311
+ declare function AuthGuard({ children, fallback, returnPath, loadingContent, promptContent, promptTitle, promptDescription, loginButtonText, authOptions, }: AuthGuardProps): react_jsx_runtime.JSX.Element;
312
+ /**
313
+ * HOC 版本:withAuthGuard
314
+ */
315
+ declare function withAuthGuard<P extends object>(Component: React.ComponentType<P>, guardProps?: Omit<AuthGuardProps, 'children'>): (props: P) => react_jsx_runtime.JSX.Element;
316
+
317
+ interface CallbackHandlerProps {
318
+ /**
319
+ * 默认跳转路径(如果 URL 中没有 return 参数)
320
+ * @default '/'
321
+ */
322
+ defaultRedirectPath?: string;
323
+ /**
324
+ * 处理中显示的内容
325
+ */
326
+ loadingContent?: ReactNode;
327
+ /**
328
+ * 错误时显示的内容
329
+ */
330
+ errorContent?: ReactNode | ((error: string) => ReactNode);
331
+ /**
332
+ * 认证配置
333
+ */
334
+ config?: Partial<AuthConfig>;
335
+ /**
336
+ * 成功后的回调
337
+ */
338
+ onSuccess?: () => void;
339
+ /**
340
+ * 失败后的回调
341
+ */
342
+ onError?: (error: string) => void;
343
+ }
344
+ /**
345
+ * 认证回调处理组件
346
+ */
347
+ declare function CallbackHandler({ defaultRedirectPath, loadingContent, errorContent, config, onSuccess, onError, }: CallbackHandlerProps): react_jsx_runtime.JSX.Element;
348
+
349
+ export { type AuthResponse as A, CallbackHandler as C, DEFAULT_AUTH_CONFIG as D, type ITokenManager as I, LocalStorageAdapter as L, type StoredTokens as S, type TokenResponse as T, type User as U, type AuthState as a, type AuthConfig as b, TokenManager as c, type IStorage as d, AuthRedirect as e, getAuthRedirect as f, getTokenManager as g, resetAuthRedirect as h, type AuthRedirectConfig as i, useAccessToken as j, type UseAuthOptions as k, type UseAuthReturn as l, AuthGuard as m, type AuthGuardProps as n, type CallbackHandlerProps as o, resetTokenManager as r, useAuth as u, withAuthGuard as w };
@@ -0,0 +1,349 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ReactNode } from 'react';
3
+
4
+ /**
5
+ * 认证相关类型定义
6
+ */
7
+ /**
8
+ * 用户信息(来自 /api/auth/me)
9
+ */
10
+ interface User {
11
+ id: string;
12
+ email?: string;
13
+ username?: string;
14
+ display_name?: string;
15
+ avatar_url?: string;
16
+ is_active: boolean;
17
+ email_verified: boolean;
18
+ sync_enabled: boolean;
19
+ }
20
+ /**
21
+ * Token 响应
22
+ */
23
+ interface TokenResponse {
24
+ access_token: string;
25
+ refresh_token: string;
26
+ expires_in: number;
27
+ token_type: string;
28
+ }
29
+ /**
30
+ * 认证响应(登录/注册)
31
+ */
32
+ interface AuthResponse extends TokenResponse {
33
+ user: User;
34
+ }
35
+ /**
36
+ * Token 存储数据
37
+ */
38
+ interface StoredTokens {
39
+ accessToken: string;
40
+ refreshToken: string;
41
+ expiresAt: number;
42
+ }
43
+ /**
44
+ * 认证状态
45
+ */
46
+ interface AuthState {
47
+ isAuthenticated: boolean;
48
+ isLoading: boolean;
49
+ user: User | null;
50
+ error: string | null;
51
+ }
52
+ /**
53
+ * Token 管理器接口(依赖注入)
54
+ */
55
+ interface ITokenManager {
56
+ setTokens(accessToken: string, refreshToken: string, expiresIn: number): void;
57
+ getAccessToken(): string | null;
58
+ getRefreshToken(): string | null;
59
+ isAccessTokenExpired(): boolean;
60
+ clearTokens(): void;
61
+ hasValidAuth(): boolean;
62
+ }
63
+ /**
64
+ * 认证配置
65
+ */
66
+ interface AuthConfig {
67
+ portalUrl: string;
68
+ apiBaseUrl: string;
69
+ callbackPath: string;
70
+ tokenKeys: {
71
+ accessToken: string;
72
+ refreshToken: string;
73
+ tokenExpiry: string;
74
+ };
75
+ }
76
+ /**
77
+ * 默认认证配置
78
+ */
79
+ declare const DEFAULT_AUTH_CONFIG: AuthConfig;
80
+
81
+ /**
82
+ * Token 管理器
83
+ * 提供 token 的存储、获取、验证和清除功能
84
+ *
85
+ * 设计原则:
86
+ * - 依赖注入:支持自定义 storage 和配置
87
+ * - 与 Portal 共享相同的 localStorage key
88
+ * - 支持 SSR(检查 window 对象)
89
+ */
90
+
91
+ /**
92
+ * Storage 接口(依赖注入)
93
+ */
94
+ interface IStorage {
95
+ getItem(key: string): string | null;
96
+ setItem(key: string, value: string): void;
97
+ removeItem(key: string): void;
98
+ }
99
+ /**
100
+ * 浏览器 localStorage 适配器
101
+ */
102
+ declare class LocalStorageAdapter implements IStorage {
103
+ getItem(key: string): string | null;
104
+ setItem(key: string, value: string): void;
105
+ removeItem(key: string): void;
106
+ }
107
+ /**
108
+ * Token 管理器实现
109
+ */
110
+ declare class TokenManager implements ITokenManager {
111
+ private storage;
112
+ private config;
113
+ constructor(storage?: IStorage, config?: Partial<AuthConfig>);
114
+ /**
115
+ * 保存 tokens
116
+ */
117
+ setTokens(accessToken: string, refreshToken: string, expiresIn: number): void;
118
+ /**
119
+ * 获取 Access Token
120
+ */
121
+ getAccessToken(): string | null;
122
+ /**
123
+ * 获取 Refresh Token
124
+ */
125
+ getRefreshToken(): string | null;
126
+ /**
127
+ * 获取 Token 过期时间
128
+ */
129
+ getTokenExpiry(): number | null;
130
+ /**
131
+ * 检查 Access Token 是否过期
132
+ * 提前 60 秒认为过期,留出刷新时间
133
+ */
134
+ isAccessTokenExpired(): boolean;
135
+ /**
136
+ * 清除所有 tokens
137
+ */
138
+ clearTokens(): void;
139
+ /**
140
+ * 检查是否有有效的认证信息
141
+ */
142
+ hasValidAuth(): boolean;
143
+ /**
144
+ * 获取配置
145
+ */
146
+ getConfig(): AuthConfig;
147
+ }
148
+ /**
149
+ * 获取 TokenManager 单例
150
+ */
151
+ declare function getTokenManager(config?: Partial<AuthConfig>): TokenManager;
152
+ /**
153
+ * 重置单例(用于测试)
154
+ */
155
+ declare function resetTokenManager(): void;
156
+
157
+ /**
158
+ * 认证跳转工具
159
+ * 处理跳转到 Portal 登录和回调处理
160
+ */
161
+ /**
162
+ * 认证跳转配置
163
+ */
164
+ interface AuthRedirectConfig {
165
+ portalUrl: string;
166
+ callbackPath: string;
167
+ }
168
+ /**
169
+ * 认证跳转工具类
170
+ */
171
+ declare class AuthRedirect {
172
+ private config;
173
+ constructor(config?: Partial<AuthRedirectConfig>);
174
+ /**
175
+ * 获取当前页面的完整 URL
176
+ */
177
+ private getCurrentUrl;
178
+ /**
179
+ * 获取当前页面的 origin
180
+ */
181
+ private getCurrentOrigin;
182
+ /**
183
+ * 构建回调 URL
184
+ */
185
+ private buildCallbackUrl;
186
+ /**
187
+ * 构建登录跳转 URL
188
+ * @param returnPath 登录成功后返回的路径(默认当前页面)
189
+ */
190
+ buildLoginUrl(returnPath?: string): string;
191
+ /**
192
+ * 跳转到 Portal 登录页
193
+ * @param returnPath 登录成功后返回的路径
194
+ */
195
+ redirectToLogin(returnPath?: string): void;
196
+ /**
197
+ * 从 URL 参数中解析 Token
198
+ */
199
+ parseTokensFromUrl(): {
200
+ accessToken: string | null;
201
+ refreshToken: string | null;
202
+ expiresIn: number | null;
203
+ returnPath: string | null;
204
+ } | null;
205
+ /**
206
+ * 清除 URL 中的 Token 参数
207
+ */
208
+ clearTokensFromUrl(): void;
209
+ /**
210
+ * 验证跳转 URL 是否在白名单中
211
+ */
212
+ isAllowedRedirectUrl(url: string): boolean;
213
+ }
214
+ /**
215
+ * 获取 AuthRedirect 单例
216
+ */
217
+ declare function getAuthRedirect(config?: Partial<AuthRedirectConfig>): AuthRedirect;
218
+ /**
219
+ * 重置单例(用于测试)
220
+ */
221
+ declare function resetAuthRedirect(): void;
222
+
223
+ /**
224
+ * Hook 配置
225
+ */
226
+ interface UseAuthOptions {
227
+ tokenManager?: TokenManager;
228
+ authRedirect?: AuthRedirect;
229
+ config?: Partial<AuthConfig>;
230
+ /**
231
+ * 是否自动获取用户信息
232
+ * @default true
233
+ */
234
+ fetchUser?: boolean;
235
+ }
236
+ /**
237
+ * Hook 返回值
238
+ */
239
+ interface UseAuthReturn extends AuthState {
240
+ /**
241
+ * 登录(跳转到 Portal)
242
+ */
243
+ login: (returnPath?: string) => void;
244
+ /**
245
+ * 登出
246
+ */
247
+ logout: () => void;
248
+ /**
249
+ * 获取 access token
250
+ */
251
+ getAccessToken: () => string | null;
252
+ /**
253
+ * 刷新认证状态
254
+ */
255
+ refresh: () => void;
256
+ /**
257
+ * 刷新用户信息
258
+ */
259
+ refreshUser: () => Promise<void>;
260
+ }
261
+ /**
262
+ * 认证状态 Hook
263
+ */
264
+ declare function useAuth(options?: UseAuthOptions): UseAuthReturn;
265
+ /**
266
+ * 简化版 Hook:只获取 token
267
+ */
268
+ declare function useAccessToken(): string | null;
269
+
270
+ interface AuthGuardProps {
271
+ children: ReactNode;
272
+ /**
273
+ * 未登录时的行为
274
+ * - 'redirect': 自动跳转到登录页
275
+ * - 'prompt': 显示登录提示
276
+ * @default 'prompt'
277
+ */
278
+ fallback?: 'redirect' | 'prompt';
279
+ /**
280
+ * 登录后返回的路径
281
+ */
282
+ returnPath?: string;
283
+ /**
284
+ * 加载中显示的内容
285
+ */
286
+ loadingContent?: ReactNode;
287
+ /**
288
+ * 未登录提示内容
289
+ */
290
+ promptContent?: ReactNode;
291
+ /**
292
+ * 提示标题
293
+ */
294
+ promptTitle?: string;
295
+ /**
296
+ * 提示描述
297
+ */
298
+ promptDescription?: string;
299
+ /**
300
+ * 登录按钮文字
301
+ */
302
+ loginButtonText?: string;
303
+ /**
304
+ * useAuth 配置
305
+ */
306
+ authOptions?: UseAuthOptions;
307
+ }
308
+ /**
309
+ * 认证守卫组件
310
+ */
311
+ declare function AuthGuard({ children, fallback, returnPath, loadingContent, promptContent, promptTitle, promptDescription, loginButtonText, authOptions, }: AuthGuardProps): react_jsx_runtime.JSX.Element;
312
+ /**
313
+ * HOC 版本:withAuthGuard
314
+ */
315
+ declare function withAuthGuard<P extends object>(Component: React.ComponentType<P>, guardProps?: Omit<AuthGuardProps, 'children'>): (props: P) => react_jsx_runtime.JSX.Element;
316
+
317
+ interface CallbackHandlerProps {
318
+ /**
319
+ * 默认跳转路径(如果 URL 中没有 return 参数)
320
+ * @default '/'
321
+ */
322
+ defaultRedirectPath?: string;
323
+ /**
324
+ * 处理中显示的内容
325
+ */
326
+ loadingContent?: ReactNode;
327
+ /**
328
+ * 错误时显示的内容
329
+ */
330
+ errorContent?: ReactNode | ((error: string) => ReactNode);
331
+ /**
332
+ * 认证配置
333
+ */
334
+ config?: Partial<AuthConfig>;
335
+ /**
336
+ * 成功后的回调
337
+ */
338
+ onSuccess?: () => void;
339
+ /**
340
+ * 失败后的回调
341
+ */
342
+ onError?: (error: string) => void;
343
+ }
344
+ /**
345
+ * 认证回调处理组件
346
+ */
347
+ declare function CallbackHandler({ defaultRedirectPath, loadingContent, errorContent, config, onSuccess, onError, }: CallbackHandlerProps): react_jsx_runtime.JSX.Element;
348
+
349
+ export { type AuthResponse as A, CallbackHandler as C, DEFAULT_AUTH_CONFIG as D, type ITokenManager as I, LocalStorageAdapter as L, type StoredTokens as S, type TokenResponse as T, type User as U, type AuthState as a, type AuthConfig as b, TokenManager as c, type IStorage as d, AuthRedirect as e, getAuthRedirect as f, getTokenManager as g, resetAuthRedirect as h, type AuthRedirectConfig as i, useAccessToken as j, type UseAuthOptions as k, type UseAuthReturn as l, AuthGuard as m, type AuthGuardProps as n, type CallbackHandlerProps as o, resetTokenManager as r, useAuth as u, withAuthGuard as w };