@longzai-intelligence-auth/react 0.0.2
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/components/authenticated.d.ts +7 -0
- package/dist/components/authenticated.js +11 -0
- package/dist/context/auth-provider.d.ts +22 -0
- package/dist/context/auth-provider.js +100 -0
- package/dist/hooks/use-auth.d.ts +7 -0
- package/dist/hooks/use-auth.js +5 -0
- package/dist/hooks/use-permission.d.ts +4 -0
- package/dist/hooks/use-permission.js +7 -0
- package/dist/hooks/use-session.d.ts +1 -0
- package/dist/hooks/use-session.js +5 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +8 -0
- package/package.json +54 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
export type AuthenticatedProps = {
|
|
3
|
+
children: ReactNode;
|
|
4
|
+
fallback?: ReactNode;
|
|
5
|
+
loadingFallback?: ReactNode;
|
|
6
|
+
};
|
|
7
|
+
export declare function Authenticated({ children, fallback, loadingFallback }: AuthenticatedProps): ReactNode;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { useSession } from "../hooks/use-session";
|
|
2
|
+
export function Authenticated({ children, fallback = null, loadingFallback = null, }) {
|
|
3
|
+
const { isAuthenticated, isLoading } = useSession();
|
|
4
|
+
if (isLoading) {
|
|
5
|
+
return loadingFallback;
|
|
6
|
+
}
|
|
7
|
+
if (!isAuthenticated) {
|
|
8
|
+
return fallback;
|
|
9
|
+
}
|
|
10
|
+
return children;
|
|
11
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
import type { BaseAuthClient, AuthStrategyType, TokenStorage, StorageType, SessionState } from "@longzai-intelligence-auth/sdk";
|
|
3
|
+
import type { LoginRequest, RegisterRequest } from "@longzai-intelligence-auth/core";
|
|
4
|
+
export type AuthProviderProps = {
|
|
5
|
+
baseUrl: string;
|
|
6
|
+
apiPrefix?: string;
|
|
7
|
+
strategy?: AuthStrategyType;
|
|
8
|
+
storageType?: StorageType;
|
|
9
|
+
children: ReactNode;
|
|
10
|
+
};
|
|
11
|
+
export type AuthContextValue = {
|
|
12
|
+
session: SessionState;
|
|
13
|
+
client: BaseAuthClient;
|
|
14
|
+
storage: TokenStorage;
|
|
15
|
+
strategy: AuthStrategyType;
|
|
16
|
+
login: (data: LoginRequest) => Promise<void>;
|
|
17
|
+
register: (data: RegisterRequest) => Promise<void>;
|
|
18
|
+
logout: () => Promise<void>;
|
|
19
|
+
refreshSession: () => Promise<void>;
|
|
20
|
+
};
|
|
21
|
+
export declare function AuthProvider({ baseUrl, apiPrefix, strategy, storageType, children }: AuthProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
22
|
+
export declare function useAuthContext(): AuthContextValue;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useContext, useEffect, useState, useCallback, useRef } from "react";
|
|
3
|
+
import { createAuthClient, createTokenStorage } from "@longzai-intelligence-auth/sdk";
|
|
4
|
+
const AuthContext = createContext(null);
|
|
5
|
+
export function AuthProvider({ baseUrl, apiPrefix, strategy = "jwt", storageType = "localStorage", children, }) {
|
|
6
|
+
const clientRef = useRef(createAuthClient({ baseUrl, apiPrefix, strategy }));
|
|
7
|
+
const storageRef = useRef(createTokenStorage(storageType));
|
|
8
|
+
const [session, setSession] = useState({
|
|
9
|
+
user: null,
|
|
10
|
+
isLoading: true,
|
|
11
|
+
isAuthenticated: false,
|
|
12
|
+
error: null,
|
|
13
|
+
});
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
const token = storageRef.current.getToken();
|
|
16
|
+
if (!token) {
|
|
17
|
+
setSession({ user: null, isLoading: false, isAuthenticated: false, error: null });
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
clientRef.current.getProfile(token)
|
|
21
|
+
.then((data) => {
|
|
22
|
+
setSession({ user: data.user, isLoading: false, isAuthenticated: true, error: null });
|
|
23
|
+
})
|
|
24
|
+
.catch(() => {
|
|
25
|
+
storageRef.current.clear();
|
|
26
|
+
setSession({ user: null, isLoading: false, isAuthenticated: false, error: null });
|
|
27
|
+
});
|
|
28
|
+
}, []);
|
|
29
|
+
const login = useCallback(async (data) => {
|
|
30
|
+
setSession((prev) => ({ ...prev, isLoading: true, error: null }));
|
|
31
|
+
try {
|
|
32
|
+
const response = await clientRef.current.login(data);
|
|
33
|
+
storageRef.current.setToken(response.accessToken);
|
|
34
|
+
storageRef.current.setRefreshToken(response.refreshToken);
|
|
35
|
+
const profile = await clientRef.current.getProfile(response.accessToken);
|
|
36
|
+
const user = profile.user ?? null;
|
|
37
|
+
setSession({ user, isLoading: false, isAuthenticated: true, error: null });
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
const message = error instanceof Error ? error.message : "登录失败";
|
|
41
|
+
setSession((prev) => ({ ...prev, isLoading: false, error: message }));
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
}, []);
|
|
45
|
+
const register = useCallback(async (data) => {
|
|
46
|
+
setSession((prev) => ({ ...prev, isLoading: true, error: null }));
|
|
47
|
+
try {
|
|
48
|
+
await clientRef.current.register(data);
|
|
49
|
+
setSession((prev) => ({ ...prev, isLoading: false }));
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
const message = error instanceof Error ? error.message : "注册失败";
|
|
53
|
+
setSession((prev) => ({ ...prev, isLoading: false, error: message }));
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
}, []);
|
|
57
|
+
const logout = useCallback(async () => {
|
|
58
|
+
const token = storageRef.current.getToken();
|
|
59
|
+
if (token) {
|
|
60
|
+
try {
|
|
61
|
+
await clientRef.current.logout(token);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
storageRef.current.clear();
|
|
67
|
+
setSession({ user: null, isLoading: false, isAuthenticated: false, error: null });
|
|
68
|
+
}, []);
|
|
69
|
+
const refreshSession = useCallback(async () => {
|
|
70
|
+
const token = storageRef.current.getToken();
|
|
71
|
+
if (!token)
|
|
72
|
+
return;
|
|
73
|
+
setSession((prev) => ({ ...prev, isLoading: true }));
|
|
74
|
+
try {
|
|
75
|
+
const data = await clientRef.current.getProfile(token);
|
|
76
|
+
setSession({ user: data.user, isLoading: false, isAuthenticated: true, error: null });
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
setSession((prev) => ({ ...prev, isLoading: false }));
|
|
80
|
+
}
|
|
81
|
+
}, []);
|
|
82
|
+
const value = {
|
|
83
|
+
session,
|
|
84
|
+
client: clientRef.current,
|
|
85
|
+
storage: storageRef.current,
|
|
86
|
+
strategy,
|
|
87
|
+
login,
|
|
88
|
+
register,
|
|
89
|
+
logout,
|
|
90
|
+
refreshSession,
|
|
91
|
+
};
|
|
92
|
+
return (_jsx(AuthContext.Provider, { value: value, children: children }));
|
|
93
|
+
}
|
|
94
|
+
export function useAuthContext() {
|
|
95
|
+
const context = useContext(AuthContext);
|
|
96
|
+
if (!context) {
|
|
97
|
+
throw new Error("useAuthContext 必须在 AuthProvider 内使用");
|
|
98
|
+
}
|
|
99
|
+
return context;
|
|
100
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare function useAuth(): {
|
|
2
|
+
login: (data: import("@longzai-intelligence-auth/core").LoginRequest) => Promise<void>;
|
|
3
|
+
register: (data: import("@longzai-intelligence-auth/core").RegisterRequest) => Promise<void>;
|
|
4
|
+
logout: () => Promise<void>;
|
|
5
|
+
refreshSession: () => Promise<void>;
|
|
6
|
+
strategy: import("@longzai-intelligence-auth/sdk").AuthStrategyType;
|
|
7
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { useAuthContext } from "../context/auth-provider";
|
|
2
|
+
export function usePermission() {
|
|
3
|
+
const { session, storage } = useAuthContext();
|
|
4
|
+
const token = storage.getToken();
|
|
5
|
+
const hasPermission = session.isAuthenticated && !!token;
|
|
6
|
+
return { hasPermission, isLoading: session.isLoading };
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useSession(): import("@longzai-intelligence-auth/sdk").SessionState;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { AuthProvider, useAuthContext } from "./context/auth-provider";
|
|
2
|
+
export type { AuthProviderProps, AuthContextValue } from "./context/auth-provider";
|
|
3
|
+
export { useSession } from "./hooks/use-session";
|
|
4
|
+
export { useAuth } from "./hooks/use-auth";
|
|
5
|
+
export { usePermission } from "./hooks/use-permission";
|
|
6
|
+
export { Authenticated } from "./components/authenticated";
|
|
7
|
+
export type { AuthenticatedProps } from "./components/authenticated";
|
|
8
|
+
export { createAuthClient, AuthClientError } from "@longzai-intelligence-auth/sdk";
|
|
9
|
+
export type { AuthClientOptions, AuthStrategyType, BaseAuthClient, JwtAuthClient } from "@longzai-intelligence-auth/sdk";
|
|
10
|
+
export { createTokenStorage } from "@longzai-intelligence-auth/sdk";
|
|
11
|
+
export type { TokenStorage } from "@longzai-intelligence-auth/sdk";
|
|
12
|
+
export { createTokenInterceptor, createRefreshInterceptor } from "@longzai-intelligence-auth/sdk";
|
|
13
|
+
export type { StorageType, AuthUser, SessionState, } from "@longzai-intelligence-auth/sdk";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { AuthProvider, useAuthContext } from "./context/auth-provider";
|
|
2
|
+
export { useSession } from "./hooks/use-session";
|
|
3
|
+
export { useAuth } from "./hooks/use-auth";
|
|
4
|
+
export { usePermission } from "./hooks/use-permission";
|
|
5
|
+
export { Authenticated } from "./components/authenticated";
|
|
6
|
+
export { createAuthClient, AuthClientError } from "@longzai-intelligence-auth/sdk";
|
|
7
|
+
export { createTokenStorage } from "@longzai-intelligence-auth/sdk";
|
|
8
|
+
export { createTokenInterceptor, createRefreshInterceptor } from "@longzai-intelligence-auth/sdk";
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@longzai-intelligence-auth/react",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"license": "UNLICENSED",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "./dist/index.cjs",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"require": {
|
|
17
|
+
"types": "./dist/index.d.cts",
|
|
18
|
+
"default": "./dist/index.cjs"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "bun build src/index.ts --outdir dist --target bun",
|
|
30
|
+
"build:declaration": "tsgo --declaration --emitDeclarationOnly --outDir dist -p tsconfig/app.json",
|
|
31
|
+
"build:prod": "NODE_ENV=production tsdown",
|
|
32
|
+
"prepublishOnly": "bun run build:prod",
|
|
33
|
+
"typecheck": "bun run typecheck:app && bun run typecheck:node && bun run typecheck:test",
|
|
34
|
+
"typecheck:app": "tsgo --noEmit -p tsconfig/app.json",
|
|
35
|
+
"typecheck:node": "tsgo --noEmit -p tsconfig/node.json",
|
|
36
|
+
"typecheck:test": "tsgo --noEmit -p tsconfig/test.json",
|
|
37
|
+
"lint": "oxlint && oxfmt --check",
|
|
38
|
+
"lint:fix": "oxlint --fix && oxfmt",
|
|
39
|
+
"test": "bun test",
|
|
40
|
+
"test:watch": "bun test --watch",
|
|
41
|
+
"test:coverage": "bun test --coverage",
|
|
42
|
+
"clean": "rm -rf dist out .cache"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@longzai-intelligence-auth/core": "0.0.1",
|
|
46
|
+
"@longzai-intelligence-auth/sdk": "0.0.1"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@types/react": "^19.1.0"
|
|
53
|
+
}
|
|
54
|
+
}
|