@choiceform/shared-auth 0.1.17 → 0.1.18
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/README.md +286 -134
- package/dist/__tests__/auth-utils.test.d.ts +5 -0
- package/dist/__tests__/auth-utils.test.d.ts.map +1 -0
- package/dist/__tests__/auth-utils.test.js +96 -0
- package/dist/__tests__/store.test.d.ts +5 -0
- package/dist/__tests__/store.test.d.ts.map +1 -0
- package/dist/__tests__/store.test.js +210 -0
- package/dist/__tests__/user-mapper.test.d.ts +5 -0
- package/dist/__tests__/user-mapper.test.d.ts.map +1 -0
- package/dist/__tests__/user-mapper.test.js +76 -0
- package/dist/api/auth-api.d.ts +93 -9
- package/dist/api/auth-api.d.ts.map +1 -1
- package/dist/api/auth-api.js +219 -80
- package/dist/api/client.d.ts +10 -0
- package/dist/api/client.d.ts.map +1 -1
- package/dist/api/client.js +10 -0
- package/dist/api/organization-api.d.ts +2 -7
- package/dist/api/organization-api.d.ts.map +1 -1
- package/dist/api/organization-api.js +2 -17
- package/dist/api/team-api.d.ts +1 -5
- package/dist/api/team-api.d.ts.map +1 -1
- package/dist/api/team-api.js +5 -11
- package/dist/components/auth-sync.d.ts +27 -0
- package/dist/components/auth-sync.d.ts.map +1 -0
- package/dist/components/auth-sync.js +117 -0
- package/dist/components/protected-route.d.ts +18 -0
- package/dist/components/protected-route.d.ts.map +1 -0
- package/dist/components/protected-route.js +34 -0
- package/dist/components/sign-in-page.d.ts +21 -0
- package/dist/components/sign-in-page.d.ts.map +1 -0
- package/dist/components/sign-in-page.js +31 -0
- package/dist/config.js +1 -1
- package/dist/core.d.ts +148 -71
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +109 -28
- package/dist/hooks/index.d.ts +8 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +7 -0
- package/dist/hooks/use-auth-init.d.ts +4 -0
- package/dist/hooks/use-auth-init.d.ts.map +1 -1
- package/dist/hooks/use-auth-init.js +16 -21
- package/dist/hooks/use-auth-sync.d.ts +60 -0
- package/dist/hooks/use-auth-sync.d.ts.map +1 -0
- package/dist/hooks/use-auth-sync.js +116 -0
- package/dist/hooks/use-email-verification.d.ts +85 -0
- package/dist/hooks/use-email-verification.d.ts.map +1 -0
- package/dist/hooks/use-email-verification.js +145 -0
- package/dist/hooks/use-protected-route.d.ts +67 -0
- package/dist/hooks/use-protected-route.d.ts.map +1 -0
- package/dist/hooks/use-protected-route.js +102 -0
- package/dist/index.d.ts +12 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +43 -13
- package/dist/init.d.ts +127 -70
- package/dist/init.d.ts.map +1 -1
- package/dist/lib/auth-client.d.ts.map +1 -1
- package/dist/lib/auth-client.js +75 -2
- package/dist/services/auth-service.d.ts +101 -0
- package/dist/services/auth-service.d.ts.map +1 -0
- package/dist/services/auth-service.js +356 -0
- package/dist/services/callback-service.d.ts +33 -0
- package/dist/services/callback-service.d.ts.map +1 -0
- package/dist/services/callback-service.js +473 -0
- package/dist/services/companion-team.d.ts.map +1 -1
- package/dist/services/companion-team.js +41 -39
- package/dist/services/index.d.ts +2 -0
- package/dist/services/index.d.ts.map +1 -1
- package/dist/services/index.js +2 -0
- package/dist/store/actions.d.ts +54 -51
- package/dist/store/actions.d.ts.map +1 -1
- package/dist/store/actions.js +111 -243
- package/dist/store/computed.d.ts +72 -1
- package/dist/store/computed.d.ts.map +1 -1
- package/dist/store/computed.js +90 -3
- package/dist/store/index.d.ts +3 -3
- package/dist/store/index.d.ts.map +1 -1
- package/dist/store/index.js +2 -2
- package/dist/store/state.d.ts +10 -0
- package/dist/store/state.d.ts.map +1 -1
- package/dist/store/state.js +11 -1
- package/dist/store/utils.d.ts +3 -34
- package/dist/store/utils.d.ts.map +1 -1
- package/dist/store/utils.js +2 -22
- package/dist/types/auth.d.ts +106 -0
- package/dist/types/auth.d.ts.map +1 -1
- package/dist/types/callback.d.ts +35 -0
- package/dist/types/callback.d.ts.map +1 -0
- package/dist/types/callback.js +1 -0
- package/dist/types/index.d.ts +4 -3
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/organization.d.ts +19 -3
- package/dist/types/organization.d.ts.map +1 -1
- package/dist/types/team.d.ts +6 -2
- package/dist/types/team.d.ts.map +1 -1
- package/dist/types/user.d.ts +7 -3
- package/dist/types/user.d.ts.map +1 -1
- package/dist/utils/auth-utils.d.ts +60 -0
- package/dist/utils/auth-utils.d.ts.map +1 -0
- package/dist/utils/auth-utils.js +146 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -0
- package/dist/utils/user-mapper.d.ts.map +1 -1
- package/dist/utils/user-mapper.js +2 -1
- package/package.json +10 -2
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 邮箱验证 Hook
|
|
3
|
+
*
|
|
4
|
+
* 职责:
|
|
5
|
+
* 1. 重新发送验证邮件
|
|
6
|
+
* 2. 检查邮箱是否已验证
|
|
7
|
+
* 3. 更换邮箱(登出并跳转)
|
|
8
|
+
* 4. 倒计时逻辑
|
|
9
|
+
*/
|
|
10
|
+
import { useCallback, useRef, useState } from "react";
|
|
11
|
+
/**
|
|
12
|
+
* 邮箱验证 Hook
|
|
13
|
+
*
|
|
14
|
+
* 使用示例:
|
|
15
|
+
* ```tsx
|
|
16
|
+
* function VerifyEmailPage({ email, lang }) {
|
|
17
|
+
* const navigate = useNavigate()
|
|
18
|
+
*
|
|
19
|
+
* const {
|
|
20
|
+
* isLoading,
|
|
21
|
+
* isAlreadyVerified,
|
|
22
|
+
* isCountingDown,
|
|
23
|
+
* countdown,
|
|
24
|
+
* resendVerification,
|
|
25
|
+
* changeEmail
|
|
26
|
+
* } = useEmailVerification(auth, {
|
|
27
|
+
* email,
|
|
28
|
+
* lang,
|
|
29
|
+
* onRedirect: (path) => navigate(path),
|
|
30
|
+
* onSendSuccess: (email) => toast.success(`验证邮件已发送到 ${email}`),
|
|
31
|
+
* onSendError: () => toast.error('发送失败'),
|
|
32
|
+
* onAlreadyVerified: () => navigate('/community')
|
|
33
|
+
* })
|
|
34
|
+
*
|
|
35
|
+
* return (
|
|
36
|
+
* <div>
|
|
37
|
+
* {isAlreadyVerified ? (
|
|
38
|
+
* <p>邮箱已验证,正在跳转...</p>
|
|
39
|
+
* ) : (
|
|
40
|
+
* <>
|
|
41
|
+
* <button onClick={resendVerification} disabled={isLoading || isCountingDown}>
|
|
42
|
+
* {isCountingDown ? `${countdown}s 后重试` : '重新发送'}
|
|
43
|
+
* </button>
|
|
44
|
+
* <button onClick={changeEmail}>使用其他邮箱</button>
|
|
45
|
+
* </>
|
|
46
|
+
* )}
|
|
47
|
+
* </div>
|
|
48
|
+
* )
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export function useEmailVerification(auth, config) {
|
|
53
|
+
const { email, lang = "us", countdownDuration = 60, onRedirect, onSendSuccess, onSendError, onAlreadyVerified, } = config;
|
|
54
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
55
|
+
const [isAlreadyVerified, setIsAlreadyVerified] = useState(false);
|
|
56
|
+
const [countdown, setCountdown] = useState(countdownDuration);
|
|
57
|
+
const [isCountingDown, setIsCountingDown] = useState(false);
|
|
58
|
+
const countdownTimerRef = useRef(null);
|
|
59
|
+
// 开始倒计时
|
|
60
|
+
const startCountdown = useCallback(() => {
|
|
61
|
+
setCountdown(countdownDuration);
|
|
62
|
+
setIsCountingDown(true);
|
|
63
|
+
if (countdownTimerRef.current) {
|
|
64
|
+
clearInterval(countdownTimerRef.current);
|
|
65
|
+
}
|
|
66
|
+
countdownTimerRef.current = setInterval(() => {
|
|
67
|
+
setCountdown((prev) => {
|
|
68
|
+
if (prev <= 1) {
|
|
69
|
+
if (countdownTimerRef.current) {
|
|
70
|
+
clearInterval(countdownTimerRef.current);
|
|
71
|
+
countdownTimerRef.current = null;
|
|
72
|
+
}
|
|
73
|
+
setIsCountingDown(false);
|
|
74
|
+
return countdownDuration;
|
|
75
|
+
}
|
|
76
|
+
return prev - 1;
|
|
77
|
+
});
|
|
78
|
+
}, 1000);
|
|
79
|
+
}, [countdownDuration]);
|
|
80
|
+
// 重新发送验证邮件
|
|
81
|
+
const resendVerification = useCallback(async () => {
|
|
82
|
+
if (!email || isCountingDown || isLoading)
|
|
83
|
+
return;
|
|
84
|
+
setIsLoading(true);
|
|
85
|
+
try {
|
|
86
|
+
// 先获取最新 session 状态,确认邮箱是否已验证
|
|
87
|
+
const token = auth.tokenStorage.get();
|
|
88
|
+
if (token) {
|
|
89
|
+
try {
|
|
90
|
+
const session = await auth.authService.fetchAndSetSession(token);
|
|
91
|
+
if (session?.emailVerified === true) {
|
|
92
|
+
// 邮箱已验证,不需要再发送
|
|
93
|
+
setIsAlreadyVerified(true);
|
|
94
|
+
onAlreadyVerified?.();
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
// 获取 session 失败,继续发送验证邮件
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// 发送验证邮件
|
|
103
|
+
await auth.authApi.resendVerificationEmail(email);
|
|
104
|
+
onSendSuccess?.(email);
|
|
105
|
+
startCountdown();
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
onSendError?.(error instanceof Error ? error : new Error("Failed to send verification email"));
|
|
109
|
+
}
|
|
110
|
+
finally {
|
|
111
|
+
setIsLoading(false);
|
|
112
|
+
}
|
|
113
|
+
}, [
|
|
114
|
+
email,
|
|
115
|
+
isCountingDown,
|
|
116
|
+
isLoading,
|
|
117
|
+
auth,
|
|
118
|
+
onSendSuccess,
|
|
119
|
+
onSendError,
|
|
120
|
+
onAlreadyVerified,
|
|
121
|
+
startCountdown,
|
|
122
|
+
]);
|
|
123
|
+
// 更换邮箱(登出并跳转)
|
|
124
|
+
const changeEmail = useCallback(async () => {
|
|
125
|
+
try {
|
|
126
|
+
// 完全清除登录状态(包括 Better Auth session)
|
|
127
|
+
await auth.authClient.signOut().catch(() => { });
|
|
128
|
+
auth.storeActions.clearAuth();
|
|
129
|
+
// 跳转到登录页
|
|
130
|
+
onRedirect?.(`/sign-in?lang=${lang}`);
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
// 静默失败,仍然执行跳转
|
|
134
|
+
onRedirect?.(`/sign-in?lang=${lang}`);
|
|
135
|
+
}
|
|
136
|
+
}, [auth, lang, onRedirect]);
|
|
137
|
+
return {
|
|
138
|
+
isLoading,
|
|
139
|
+
isAlreadyVerified,
|
|
140
|
+
isCountingDown,
|
|
141
|
+
countdown,
|
|
142
|
+
resendVerification,
|
|
143
|
+
changeEmail,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 路由保护 Hook
|
|
3
|
+
*
|
|
4
|
+
* 职责:
|
|
5
|
+
* 1. 等待认证初始化完成
|
|
6
|
+
* 2. 根据认证状态进行路由保护
|
|
7
|
+
* 3. 强制邮箱验证检查
|
|
8
|
+
* 4. 支持公开路由例外
|
|
9
|
+
*/
|
|
10
|
+
import type { AuthInstance } from "../core";
|
|
11
|
+
/** 保护状态 */
|
|
12
|
+
export type ProtectionStatus = "loading" | "unauthenticated" | "unverified" | "authorized" | "public";
|
|
13
|
+
/** useProtectedRoute 配置 */
|
|
14
|
+
export interface UseProtectedRouteConfig {
|
|
15
|
+
/** 语言参数 */
|
|
16
|
+
lang?: string;
|
|
17
|
+
/** 重定向函数(由项目提供) */
|
|
18
|
+
onRedirect?: (path: string) => void;
|
|
19
|
+
/** 当前路径(如果不传则从 window.location 获取) */
|
|
20
|
+
pathname?: string;
|
|
21
|
+
/** 公开路由前缀列表(不需要认证的路径) */
|
|
22
|
+
publicRoutePrefixes?: string[];
|
|
23
|
+
/** 是否要求邮箱已验证(默认 true) */
|
|
24
|
+
requireEmailVerified?: boolean;
|
|
25
|
+
}
|
|
26
|
+
/** useProtectedRoute 返回值 */
|
|
27
|
+
export interface UseProtectedRouteResult {
|
|
28
|
+
/** 是否正在加载 */
|
|
29
|
+
isLoading: boolean;
|
|
30
|
+
/** 重定向路径(如果需要重定向) */
|
|
31
|
+
redirectPath: string | null;
|
|
32
|
+
/** 是否应该渲染子组件 */
|
|
33
|
+
shouldRender: boolean;
|
|
34
|
+
/** 保护状态 */
|
|
35
|
+
status: ProtectionStatus;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 路由保护 Hook
|
|
39
|
+
*
|
|
40
|
+
* 使用示例:
|
|
41
|
+
* ```tsx
|
|
42
|
+
* function ProtectedRoute({ children }) {
|
|
43
|
+
* const navigate = useNavigate()
|
|
44
|
+
* const location = useLocation()
|
|
45
|
+
*
|
|
46
|
+
* const { status, shouldRender, isLoading, redirectPath } = useProtectedRoute(auth, {
|
|
47
|
+
* pathname: location.pathname,
|
|
48
|
+
* publicRoutePrefixes: ['/resources', '/public'],
|
|
49
|
+
* onRedirect: (path) => navigate(path, { replace: true })
|
|
50
|
+
* })
|
|
51
|
+
*
|
|
52
|
+
* useEffect(() => {
|
|
53
|
+
* if (redirectPath) {
|
|
54
|
+
* navigate(redirectPath, { replace: true })
|
|
55
|
+
* }
|
|
56
|
+
* }, [redirectPath, navigate])
|
|
57
|
+
*
|
|
58
|
+
* if (!shouldRender) {
|
|
59
|
+
* return <Loading />
|
|
60
|
+
* }
|
|
61
|
+
*
|
|
62
|
+
* return <>{children}</>
|
|
63
|
+
* }
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export declare function useProtectedRoute(auth: AuthInstance, config?: UseProtectedRouteConfig): UseProtectedRouteResult;
|
|
67
|
+
//# sourceMappingURL=use-protected-route.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-protected-route.d.ts","sourceRoot":"","sources":["../../src/hooks/use-protected-route.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAE3C,WAAW;AACX,MAAM,MAAM,gBAAgB,GACxB,SAAS,GACT,iBAAiB,GACjB,YAAY,GACZ,YAAY,GACZ,QAAQ,CAAA;AAEZ,2BAA2B;AAC3B,MAAM,WAAW,uBAAuB;IACtC,WAAW;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,mBAAmB;IACnB,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IACpC,sCAAsC;IACtC,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,yBAAyB;IACzB,mBAAmB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC/B,yBAAyB;IACzB,oBAAoB,CAAC,EAAE,OAAO,CAAA;CAC/B;AAED,4BAA4B;AAC5B,MAAM,WAAW,uBAAuB;IACtC,aAAa;IACb,SAAS,EAAE,OAAO,CAAC;IACnB,qBAAqB;IACrB,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,gBAAgB;IAChB,YAAY,EAAE,OAAO,CAAC;IACtB,WAAW;IACX,MAAM,EAAE,gBAAgB,CAAA;CACzB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,YAAY,EAClB,MAAM,GAAE,uBAA4B,GACnC,uBAAuB,CA4EzB"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 路由保护 Hook
|
|
3
|
+
*
|
|
4
|
+
* 职责:
|
|
5
|
+
* 1. 等待认证初始化完成
|
|
6
|
+
* 2. 根据认证状态进行路由保护
|
|
7
|
+
* 3. 强制邮箱验证检查
|
|
8
|
+
* 4. 支持公开路由例外
|
|
9
|
+
*/
|
|
10
|
+
import { useEffect, useMemo } from "react";
|
|
11
|
+
import { use$ } from "@legendapp/state/react";
|
|
12
|
+
/**
|
|
13
|
+
* 路由保护 Hook
|
|
14
|
+
*
|
|
15
|
+
* 使用示例:
|
|
16
|
+
* ```tsx
|
|
17
|
+
* function ProtectedRoute({ children }) {
|
|
18
|
+
* const navigate = useNavigate()
|
|
19
|
+
* const location = useLocation()
|
|
20
|
+
*
|
|
21
|
+
* const { status, shouldRender, isLoading, redirectPath } = useProtectedRoute(auth, {
|
|
22
|
+
* pathname: location.pathname,
|
|
23
|
+
* publicRoutePrefixes: ['/resources', '/public'],
|
|
24
|
+
* onRedirect: (path) => navigate(path, { replace: true })
|
|
25
|
+
* })
|
|
26
|
+
*
|
|
27
|
+
* useEffect(() => {
|
|
28
|
+
* if (redirectPath) {
|
|
29
|
+
* navigate(redirectPath, { replace: true })
|
|
30
|
+
* }
|
|
31
|
+
* }, [redirectPath, navigate])
|
|
32
|
+
*
|
|
33
|
+
* if (!shouldRender) {
|
|
34
|
+
* return <Loading />
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* return <>{children}</>
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export function useProtectedRoute(auth, config = {}) {
|
|
42
|
+
const { requireEmailVerified = true, publicRoutePrefixes = [], pathname: propPathname, lang = "us", onRedirect, } = config;
|
|
43
|
+
// 从 authStore 获取状态
|
|
44
|
+
const isLoaded = use$(auth.authStore.isLoaded);
|
|
45
|
+
const isAuthenticated = use$(auth.authStore.isAuthenticated);
|
|
46
|
+
const user = use$(auth.authStore.user);
|
|
47
|
+
// 获取当前路径
|
|
48
|
+
const pathname = useMemo(() => {
|
|
49
|
+
if (propPathname)
|
|
50
|
+
return propPathname;
|
|
51
|
+
if (typeof window === "undefined")
|
|
52
|
+
return "/";
|
|
53
|
+
return window.location.pathname;
|
|
54
|
+
}, [propPathname]);
|
|
55
|
+
// 检查是否是公开路由
|
|
56
|
+
const isPublicRoute = useMemo(() => {
|
|
57
|
+
return publicRoutePrefixes.some((prefix) => pathname === prefix || pathname.startsWith(prefix));
|
|
58
|
+
}, [pathname, publicRoutePrefixes]);
|
|
59
|
+
// 计算保护状态
|
|
60
|
+
const status = useMemo(() => {
|
|
61
|
+
// 公开路由
|
|
62
|
+
if (isPublicRoute)
|
|
63
|
+
return "public";
|
|
64
|
+
// 未加载完成
|
|
65
|
+
if (!isLoaded)
|
|
66
|
+
return "loading";
|
|
67
|
+
// 未认证
|
|
68
|
+
if (!isAuthenticated)
|
|
69
|
+
return "unauthenticated";
|
|
70
|
+
// 邮箱未验证
|
|
71
|
+
if (requireEmailVerified && user && user.emailVerified !== true) {
|
|
72
|
+
return "unverified";
|
|
73
|
+
}
|
|
74
|
+
// 已授权
|
|
75
|
+
return "authorized";
|
|
76
|
+
}, [isPublicRoute, isLoaded, isAuthenticated, user, requireEmailVerified]);
|
|
77
|
+
// 计算重定向路径
|
|
78
|
+
const redirectPath = useMemo(() => {
|
|
79
|
+
switch (status) {
|
|
80
|
+
case "unauthenticated":
|
|
81
|
+
return `/sign-in?lang=${lang}`;
|
|
82
|
+
case "unverified":
|
|
83
|
+
return `/verify-email?lang=${lang}&email=${encodeURIComponent(user?.email || "")}`;
|
|
84
|
+
default:
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
}, [status, lang, user?.email]);
|
|
88
|
+
// 执行重定向
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
if (redirectPath && onRedirect) {
|
|
91
|
+
onRedirect(redirectPath);
|
|
92
|
+
}
|
|
93
|
+
}, [redirectPath, onRedirect]);
|
|
94
|
+
// 是否应该渲染子组件
|
|
95
|
+
const shouldRender = status === "authorized" || status === "public";
|
|
96
|
+
return {
|
|
97
|
+
status,
|
|
98
|
+
shouldRender,
|
|
99
|
+
isLoading: status === "loading",
|
|
100
|
+
redirectPath,
|
|
101
|
+
};
|
|
102
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,19 +1,25 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @choiceform/shared-auth
|
|
3
3
|
* 共享认证包
|
|
4
|
+
*
|
|
5
|
+
* 架构设计:
|
|
6
|
+
* - Store Layer: 响应式状态管理(authStore + storeActions)
|
|
7
|
+
* - Service Layer: 业务逻辑(authService + callbackService)
|
|
8
|
+
* - API Layer: HTTP 请求(apiClient + authApi/organizationApi/teamApi)
|
|
9
|
+
* - Hooks Layer: React 专用 hooks
|
|
4
10
|
*/
|
|
5
11
|
export { createAuth, type AuthInstance } from "./core";
|
|
6
12
|
export { initAuth } from "./init";
|
|
7
13
|
export { defaultAuthConfig } from "./config";
|
|
8
|
-
export type { SessionUser, SessionUserMetadata, Session, AuthState, AuthConfig, BetterAuthPlugin, DefaultAuthConfig, UpdateUserRequest, MagicLinkRequest, SignInWithMagicLink, CompanionTeamOptions, AuthClientMethods, AuthClientSignIn, Organization, FullOrganization, CreateOrganizationRequest, UpdateOrganizationRequest, DeleteOrganizationRequest, SetActiveOrganizationRequest, CheckSlugRequest, MemberRole, Member, MemberUser, MemberWithUser, RemoveMemberRequest, UpdateMemberRoleRequest, InvitationStatus, Invitation, InvitationDetail, InviteMemberRequest, CancelInvitationRequest, AcceptInvitationRequest, RejectInvitationRequest, InvitationResponse, Team, CreateTeamRequest, UpdateTeamRequest, DeleteTeamRequest, SetActiveTeamRequest, TeamMember, AddTeamMemberRequest, RemoveTeamMemberRequest, ListTeamMembersRequest, } from "./types";
|
|
14
|
+
export type { SessionUser, SessionUserMetadata, Session, AuthState, AuthConfig, BetterAuthPlugin, DefaultAuthConfig, UpdateUserRequest, MagicLinkRequest, SignInWithMagicLink, CompanionTeamOptions, AuthClientMethods, AuthClientSignIn, Account, UserSession, RevokeSessionRequest, Organization, OrganizationMetadata, FullOrganization, CreateOrganizationRequest, UpdateOrganizationRequest, DeleteOrganizationRequest, SetActiveOrganizationRequest, CheckSlugRequest, MemberRole, Member, MemberUser, UserMetadata, MemberWithUser, RemoveMemberRequest, UpdateMemberRoleRequest, InvitationStatus, Invitation, InvitationDetail, InviteMemberRequest, CancelInvitationRequest, AcceptInvitationRequest, RejectInvitationRequest, InvitationResponse, Team, TeamMetadata, CreateTeamRequest, UpdateTeamRequest, DeleteTeamRequest, SetActiveTeamRequest, TeamMember, AddTeamMemberRequest, RemoveTeamMemberRequest, ListTeamMembersRequest, CallbackType, CallbackResult, CallbackConfig, } from "./types";
|
|
9
15
|
export { createAuthStore, createTokenStorage } from "./store/state";
|
|
10
|
-
export {
|
|
16
|
+
export { createStoreActions, type StoreActions } from "./store/actions";
|
|
11
17
|
export { createAuthComputed } from "./store/computed";
|
|
12
|
-
export {
|
|
18
|
+
export { getCurrentUser, getCurrentUserId, isAuthenticated, isLoading, isLoaded, waitForAuth, getAuthToken, getAuthTokenSync, getAuthHeaders, getAuthHeadersSync, handle401Response, createUserManager, } from "./store/utils";
|
|
19
|
+
export { createAuthService, type AuthService, type AuthServiceConfig, type BetterAuthClientMethods, setupCompanionTeam, createCallbackService, type CallbackService, } from "./services";
|
|
13
20
|
export { createApiClient, createAuthApi, createOrganizationApi, createTeamApi, parseErrorResponse, } from "./api";
|
|
14
21
|
export type { ApiClient, ApiClientConfig, ApiResponse, TokenStorage, UnauthorizedHandler, AuthApi, OrganizationApi, TeamApi, } from "./api";
|
|
15
|
-
export {
|
|
16
|
-
export { getEnvVar, getAuthBaseUrl, extractSessionUser } from "./utils";
|
|
17
|
-
export { useAuthInit, initializeAuth } from "./hooks/use-auth-init";
|
|
22
|
+
export { useAuthInit, initializeAuth, useAuthSync, type UseAuthSyncConfig, type UseAuthSyncResult, useProtectedRoute, type UseProtectedRouteConfig, type UseProtectedRouteResult, type ProtectionStatus, useEmailVerification, type UseEmailVerificationConfig, type UseEmailVerificationResult, } from "./hooks";
|
|
23
|
+
export { getEnvVar, getAuthBaseUrl, extractSessionUser, isValidEmail, parseAuthError, isTokenExpiredError, AUTH_ERROR_CODES, type AuthErrorCode, type ParsedAuthError, getNameFromEmail, buildAuthUrl, buildAuthPath, clearAuthParams, } from "./utils";
|
|
18
24
|
export { createAuthClientFromConfig } from "./lib/auth-client";
|
|
19
25
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAMH,OAAO,EAAE,UAAU,EAAE,KAAK,YAAY,EAAE,MAAM,QAAQ,CAAA;AACtD,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAA;AACjC,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAA;AAM5C,YAAY,EAEV,WAAW,EACX,mBAAmB,EACnB,OAAO,EACP,SAAS,EACT,UAAU,EACV,gBAAgB,EAChB,iBAAiB,EACjB,iBAAiB,EACjB,gBAAgB,EAChB,mBAAmB,EACnB,oBAAoB,EACpB,iBAAiB,EACjB,gBAAgB,EAEhB,OAAO,EAEP,WAAW,EACX,oBAAoB,EAEpB,YAAY,EACZ,oBAAoB,EACpB,gBAAgB,EAChB,yBAAyB,EACzB,yBAAyB,EACzB,yBAAyB,EACzB,4BAA4B,EAC5B,gBAAgB,EAChB,UAAU,EACV,MAAM,EACN,UAAU,EACV,YAAY,EACZ,cAAc,EACd,mBAAmB,EACnB,uBAAuB,EACvB,gBAAgB,EAChB,UAAU,EACV,gBAAgB,EAChB,mBAAmB,EACnB,uBAAuB,EACvB,uBAAuB,EACvB,uBAAuB,EACvB,kBAAkB,EAElB,IAAI,EACJ,YAAY,EACZ,iBAAiB,EACjB,iBAAiB,EACjB,iBAAiB,EACjB,oBAAoB,EACpB,UAAU,EACV,oBAAoB,EACpB,uBAAuB,EACvB,sBAAsB,EAEtB,YAAY,EACZ,cAAc,EACd,cAAc,GACf,MAAM,SAAS,CAAA;AAMhB,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA;AACnE,OAAO,EAAE,kBAAkB,EAAE,KAAK,YAAY,EAAE,MAAM,iBAAiB,CAAA;AACvE,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAA;AACrD,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,eAAe,EACf,SAAS,EACT,QAAQ,EACR,WAAW,EACX,YAAY,EACZ,gBAAgB,EAChB,cAAc,EACd,kBAAkB,EAClB,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,eAAe,CAAA;AAMtB,OAAO,EAEL,iBAAiB,EACjB,KAAK,WAAW,EAChB,KAAK,iBAAiB,EACtB,KAAK,uBAAuB,EAE5B,kBAAkB,EAElB,qBAAqB,EACrB,KAAK,eAAe,GACrB,MAAM,YAAY,CAAA;AAMnB,OAAO,EACL,eAAe,EACf,aAAa,EACb,qBAAqB,EACrB,aAAa,EACb,kBAAkB,GACnB,MAAM,OAAO,CAAA;AAEd,YAAY,EACV,SAAS,EACT,eAAe,EACf,WAAW,EACX,YAAY,EACZ,mBAAmB,EACnB,OAAO,EACP,eAAe,EACf,OAAO,GACR,MAAM,OAAO,CAAA;AAMd,OAAO,EAEL,WAAW,EACX,cAAc,EAEd,WAAW,EACX,KAAK,iBAAiB,EACtB,KAAK,iBAAiB,EAEtB,iBAAiB,EACjB,KAAK,uBAAuB,EAC5B,KAAK,uBAAuB,EAC5B,KAAK,gBAAgB,EAErB,oBAAoB,EACpB,KAAK,0BAA0B,EAC/B,KAAK,0BAA0B,GAChC,MAAM,SAAS,CAAA;AAMhB,OAAO,EAEL,SAAS,EACT,cAAc,EAEd,kBAAkB,EAElB,YAAY,EAEZ,cAAc,EACd,mBAAmB,EACnB,gBAAgB,EAChB,KAAK,aAAa,EAClB,KAAK,eAAe,EAEpB,gBAAgB,EAChB,YAAY,EACZ,aAAa,EACb,eAAe,GAChB,MAAM,SAAS,CAAA;AAMhB,OAAO,EAAE,0BAA0B,EAAE,MAAM,mBAAmB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,37 +1,67 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @choiceform/shared-auth
|
|
3
3
|
* 共享认证包
|
|
4
|
+
*
|
|
5
|
+
* 架构设计:
|
|
6
|
+
* - Store Layer: 响应式状态管理(authStore + storeActions)
|
|
7
|
+
* - Service Layer: 业务逻辑(authService + callbackService)
|
|
8
|
+
* - API Layer: HTTP 请求(apiClient + authApi/organizationApi/teamApi)
|
|
9
|
+
* - Hooks Layer: React 专用 hooks
|
|
4
10
|
*/
|
|
5
11
|
// ============================================================
|
|
6
|
-
// 核心
|
|
12
|
+
// 核心 - 主入口
|
|
7
13
|
// ============================================================
|
|
8
14
|
export { createAuth } from "./core";
|
|
9
15
|
export { initAuth } from "./init";
|
|
10
16
|
export { defaultAuthConfig } from "./config";
|
|
11
17
|
// ============================================================
|
|
12
|
-
// Store
|
|
18
|
+
// Store Layer - 状态管理
|
|
13
19
|
// ============================================================
|
|
14
20
|
export { createAuthStore, createTokenStorage } from "./store/state";
|
|
15
|
-
export {
|
|
21
|
+
export { createStoreActions } from "./store/actions";
|
|
16
22
|
export { createAuthComputed } from "./store/computed";
|
|
17
|
-
export {
|
|
23
|
+
export { getCurrentUser, getCurrentUserId, isAuthenticated, isLoading, isLoaded, waitForAuth, getAuthToken, getAuthTokenSync, getAuthHeaders, getAuthHeadersSync, handle401Response, createUserManager, } from "./store/utils";
|
|
18
24
|
// ============================================================
|
|
19
|
-
//
|
|
25
|
+
// Service Layer - 业务逻辑
|
|
20
26
|
// ============================================================
|
|
21
|
-
export {
|
|
27
|
+
export {
|
|
28
|
+
// 认证服务
|
|
29
|
+
createAuthService,
|
|
30
|
+
// 伴生团队
|
|
31
|
+
setupCompanionTeam,
|
|
32
|
+
// 回调服务
|
|
33
|
+
createCallbackService, } from "./services";
|
|
22
34
|
// ============================================================
|
|
23
|
-
//
|
|
35
|
+
// API Layer - HTTP 请求
|
|
24
36
|
// ============================================================
|
|
25
|
-
export {
|
|
37
|
+
export { createApiClient, createAuthApi, createOrganizationApi, createTeamApi, parseErrorResponse, } from "./api";
|
|
26
38
|
// ============================================================
|
|
27
|
-
//
|
|
39
|
+
// Hooks Layer - React 专用
|
|
28
40
|
// ============================================================
|
|
29
|
-
export {
|
|
41
|
+
export {
|
|
42
|
+
// 初始化
|
|
43
|
+
useAuthInit, initializeAuth,
|
|
44
|
+
// 认证状态同步
|
|
45
|
+
useAuthSync,
|
|
46
|
+
// 路由保护
|
|
47
|
+
useProtectedRoute,
|
|
48
|
+
// 邮箱验证
|
|
49
|
+
useEmailVerification, } from "./hooks";
|
|
30
50
|
// ============================================================
|
|
31
|
-
//
|
|
51
|
+
// 工具函数
|
|
32
52
|
// ============================================================
|
|
33
|
-
export {
|
|
53
|
+
export {
|
|
54
|
+
// 环境
|
|
55
|
+
getEnvVar, getAuthBaseUrl,
|
|
56
|
+
// 用户映射
|
|
57
|
+
extractSessionUser,
|
|
58
|
+
// 验证
|
|
59
|
+
isValidEmail,
|
|
60
|
+
// 错误处理
|
|
61
|
+
parseAuthError, isTokenExpiredError, AUTH_ERROR_CODES,
|
|
62
|
+
// URL 工具
|
|
63
|
+
getNameFromEmail, buildAuthUrl, buildAuthPath, clearAuthParams, } from "./utils";
|
|
34
64
|
// ============================================================
|
|
35
|
-
// Better Auth Client
|
|
65
|
+
// Better Auth Client(高级用法)
|
|
36
66
|
// ============================================================
|
|
37
67
|
export { createAuthClientFromConfig } from "./lib/auth-client";
|