Add Convex client setup and authentication handling

- Introduced a new module for Convex client configuration in `src/modules/convex.ts`.
- Implemented an authentication token fetcher to manage user tokens.
- Integrated the Convex client into the AuthProvider component in `src/pages/auth/index.tsx`.
This commit is contained in:
2026-03-06 19:00:24 +08:00
parent 700f8177fe
commit 91abdac399
5 changed files with 575 additions and 22 deletions

32
src/modules/convex.ts Normal file
View File

@@ -0,0 +1,32 @@
import { api } from "@kevisual/convex";
import { ConvexClient, AuthTokenFetcher, ConvexHttpClient } from "convex/browser";
const url = localStorage.getItem("CONVEX_URL") || 'https://convex.kevisual.cn'
const client = new ConvexClient(url!);
const httpClient = new ConvexHttpClient(url!);
export const initConvex = async () => {
const getToken = async () => {
const token = localStorage.getItem("token");
if (!token) {
return null;
}
const res = await httpClient.action(api.token.create, { token: token! });
if (res.code === 200) {
return res.data.accessToken;
}
return null;
}
const authTokenFetcher: AuthTokenFetcher = async ({ forceRefreshToken }: { forceRefreshToken: boolean }) => {
console.log("AuthTokenFetcher called, forceRefreshToken:", forceRefreshToken);
if (forceRefreshToken) {
const token = await getToken();
// console.log("fetch got token:", token);
return token;
}
return null;
}
client.setAuth(authTokenFetcher, (isAuthenticated) => {
console.log("Auth isAuthenticated:", isAuthenticated);
});
}
export { client, httpClient };