@choiceform/shared-auth 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.
Files changed (44) hide show
  1. package/README.md +452 -0
  2. package/dist/components/auth-sync.d.ts +9 -0
  3. package/dist/components/auth-sync.d.ts.map +1 -0
  4. package/dist/components/auth-sync.js +60 -0
  5. package/dist/components/protected-route.d.ts +18 -0
  6. package/dist/components/protected-route.d.ts.map +1 -0
  7. package/dist/components/protected-route.js +28 -0
  8. package/dist/components/sign-in-page.d.ts +49 -0
  9. package/dist/components/sign-in-page.d.ts.map +1 -0
  10. package/dist/components/sign-in-page.js +33 -0
  11. package/dist/config.d.ts +50 -0
  12. package/dist/config.d.ts.map +1 -0
  13. package/dist/config.js +14 -0
  14. package/dist/core.d.ts +2162 -0
  15. package/dist/core.d.ts.map +1 -0
  16. package/dist/core.js +37 -0
  17. package/dist/hooks/use-auth-init.d.ts +7 -0
  18. package/dist/hooks/use-auth-init.d.ts.map +1 -0
  19. package/dist/hooks/use-auth-init.js +41 -0
  20. package/dist/index.d.ts +22 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +24 -0
  23. package/dist/init.d.ts +2167 -0
  24. package/dist/init.d.ts.map +1 -0
  25. package/dist/init.js +17 -0
  26. package/dist/lib/auth-client.d.ts +2120 -0
  27. package/dist/lib/auth-client.d.ts.map +1 -0
  28. package/dist/lib/auth-client.js +11 -0
  29. package/dist/store/actions.d.ts +60 -0
  30. package/dist/store/actions.d.ts.map +1 -0
  31. package/dist/store/actions.js +234 -0
  32. package/dist/store/computed.d.ts +12 -0
  33. package/dist/store/computed.d.ts.map +1 -0
  34. package/dist/store/computed.js +14 -0
  35. package/dist/store/state.d.ts +16 -0
  36. package/dist/store/state.d.ts.map +1 -0
  37. package/dist/store/state.js +52 -0
  38. package/dist/store/utils.d.ts +103 -0
  39. package/dist/store/utils.d.ts.map +1 -0
  40. package/dist/store/utils.js +198 -0
  41. package/dist/types.d.ts +37 -0
  42. package/dist/types.d.ts.map +1 -0
  43. package/dist/types.js +4 -0
  44. package/package.json +65 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAE1C;;GAEG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iCAsC8gf,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;6BAAwz0W,CAAC;mCAAyD,CAAC;oCAA0D,CAAC;iCAAuD,CAAC;;;;;;;;;;;;;;;;;;;;;;;yBAA9K,CAAC;+BAAyD,CAAC;gCAA0D,CAAC;6BAAuD,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;6BAAv+0W,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iCAAD,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iCAAD,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;6BAAD,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;6BAAD,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;6BAAD,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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AC;kBAAqB,CAAC;;iBAAsG,CAAC;wBAAoE,CAAC;kBAAoC,CAAC;uBAAqG,CAAC;6BAA6E,CAAC;;;2BAA0F,CAAC;mHAA6K,CAAC;;;;;;;EAJ19L;AAED,MAAM,MAAM,YAAY,GAAG,UAAU,CAAC,OAAO,UAAU,CAAC,CAAA"}
package/dist/core.js ADDED
@@ -0,0 +1,37 @@
1
+ import { createAuthClientFromConfig } from "./lib/auth-client";
2
+ import { createAuthStore } from "./store/state";
3
+ import { createAuthActions } from "./store/actions";
4
+ import { createAuthComputed } from "./store/computed";
5
+ import { createBoundAuthUtils } from "./store/utils";
6
+ /**
7
+ * 创建认证系统实例
8
+ */
9
+ export function createAuth(config) {
10
+ // 创建 auth client
11
+ const authClient = createAuthClientFromConfig(config);
12
+ // 创建 store
13
+ const { authStore, tokenStorage } = createAuthStore({
14
+ tokenStorageKey: config.tokenStorageKey || "auth-token",
15
+ });
16
+ // 创建 actions
17
+ const authActions = createAuthActions(authStore, tokenStorage, config, {
18
+ signIn: authClient.signIn,
19
+ signOut: authClient.signOut,
20
+ });
21
+ // 创建 computed
22
+ const authComputed = createAuthComputed(authStore);
23
+ // 创建基础实例
24
+ const instance = {
25
+ authStore,
26
+ tokenStorage,
27
+ authActions,
28
+ authComputed,
29
+ authClient,
30
+ };
31
+ // 创建绑定好的工具函数
32
+ const utils = createBoundAuthUtils(instance);
33
+ return {
34
+ ...instance,
35
+ ...utils,
36
+ };
37
+ }
@@ -0,0 +1,7 @@
1
+ import type { AuthInstance } from "../core";
2
+ /**
3
+ * 认证初始化 hook
4
+ * 在应用启动时检查 URL 中的 token 或 localStorage 中的 token
5
+ */
6
+ export declare function useAuthInit(auth: AuthInstance): void;
7
+ //# sourceMappingURL=use-auth-init.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-auth-init.d.ts","sourceRoot":"","sources":["../../src/hooks/use-auth-init.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAE3C;;;GAGG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,YAAY,QAqC7C"}
@@ -0,0 +1,41 @@
1
+ import { useEffect } from "react";
2
+ /**
3
+ * 认证初始化 hook
4
+ * 在应用启动时检查 URL 中的 token 或 localStorage 中的 token
5
+ */
6
+ export function useAuthInit(auth) {
7
+ useEffect(() => {
8
+ async function initializeAuth() {
9
+ const { authActions, tokenStorage } = auth;
10
+ // 检查 URL 中的 token
11
+ const urlParams = new URLSearchParams(window.location.search);
12
+ const tokenFromUrl = urlParams.get("token");
13
+ if (tokenFromUrl) {
14
+ // 立即清理 URL 中的 token 参数(避免暴露)
15
+ urlParams.delete("token");
16
+ const newUrl = urlParams.toString()
17
+ ? `${window.location.pathname}?${urlParams.toString()}`
18
+ : window.location.pathname;
19
+ window.history.replaceState({}, "", newUrl);
20
+ // 使用 token 获取 session
21
+ await authActions.fetchSessionWithToken(tokenFromUrl);
22
+ }
23
+ else {
24
+ // 检查 localStorage 中是否有存储的 token
25
+ const storedToken = tokenStorage.get();
26
+ if (storedToken) {
27
+ await authActions.fetchSessionWithToken(storedToken);
28
+ }
29
+ else {
30
+ // 没有 token,标记为已加载
31
+ await authActions.initialize(null, true);
32
+ }
33
+ }
34
+ }
35
+ initializeAuth().catch((error) => {
36
+ console.error("Failed to initialize auth:", error);
37
+ // 即使认证初始化失败,也标记为已加载
38
+ auth.authActions.initialize(null, true);
39
+ });
40
+ }, [auth]);
41
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @choiceform/shared-auth
3
+ * 共享认证包 - 用于 Choiceform 项目的统一认证解决方案
4
+ */
5
+ export { createAuth } from "./core";
6
+ export type { AuthInstance } from "./core";
7
+ export { defaultAuthConfig } from "./config";
8
+ export type { AuthConfig } from "./config";
9
+ export type { SessionUser, Session, AuthState } from "./types";
10
+ export { createAuthStore } from "./store/state";
11
+ export { createAuthActions } from "./store/actions";
12
+ export { createAuthComputed } from "./store/computed";
13
+ export type { TokenStorage } from "./store/state";
14
+ export type { AuthActions } from "./store/actions";
15
+ export { getCurrentUser, getCurrentUserId, getCurrentUserIdSafe, isAuthenticated, isLoading, isLoaded, waitForAuth, getAuthToken, getAuthHeaders, handle401Response, createApiClient, createUserManager, createBoundAuthUtils, } from "./store/utils";
16
+ export { AuthSync } from "./components/auth-sync";
17
+ export { ProtectedRoute } from "./components/protected-route";
18
+ export { SignInPage } from "./components/sign-in-page";
19
+ export { useAuthInit } from "./hooks/use-auth-init";
20
+ export { createAuthClientFromConfig } from "./lib/auth-client";
21
+ export { initAuth } from "./init";
22
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAA;AACnC,YAAY,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAA;AAG1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAA;AAC5C,YAAY,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAG1C,YAAY,EAAE,WAAW,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAG9D,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAA;AAC/C,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAA;AACnD,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAA;AACrD,YAAY,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AACjD,YAAY,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAGlD,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,oBAAoB,EACpB,eAAe,EACf,SAAS,EACT,QAAQ,EACR,WAAW,EACX,YAAY,EACZ,cAAc,EACd,iBAAiB,EACjB,eAAe,EACf,iBAAiB,EACjB,oBAAoB,GACrB,MAAM,eAAe,CAAA;AAGtB,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAA;AACjD,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAA;AAC7D,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAA;AAGtD,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAGnD,OAAO,EAAE,0BAA0B,EAAE,MAAM,mBAAmB,CAAA;AAG9D,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,24 @@
1
+ /**
2
+ * @choiceform/shared-auth
3
+ * 共享认证包 - 用于 Choiceform 项目的统一认证解决方案
4
+ */
5
+ // 核心功能
6
+ export { createAuth } from "./core";
7
+ // 配置
8
+ export { defaultAuthConfig } from "./config";
9
+ // Store
10
+ export { createAuthStore } from "./store/state";
11
+ export { createAuthActions } from "./store/actions";
12
+ export { createAuthComputed } from "./store/computed";
13
+ // Utils
14
+ export { getCurrentUser, getCurrentUserId, getCurrentUserIdSafe, isAuthenticated, isLoading, isLoaded, waitForAuth, getAuthToken, getAuthHeaders, handle401Response, createApiClient, createUserManager, createBoundAuthUtils, } from "./store/utils";
15
+ // 组件
16
+ export { AuthSync } from "./components/auth-sync";
17
+ export { ProtectedRoute } from "./components/protected-route";
18
+ export { SignInPage } from "./components/sign-in-page";
19
+ // Hooks
20
+ export { useAuthInit } from "./hooks/use-auth-init";
21
+ // Better Auth Client
22
+ export { createAuthClientFromConfig } from "./lib/auth-client";
23
+ // 快速初始化
24
+ export { initAuth } from "./init";