UNPKG

kitcn

Version:

kitcn - React Query integration and CLI tools for Convex

30 lines (29 loc) 943 B
//#region src/auth-start/index.ts const startLoaderAuthTokens = /* @__PURE__ */ new WeakMap(); const isStartLoaderConvexQueryClient = (target) => "convexClient" in target; const syncConvexAuthForStartLoader = async ({ convex, getToken }) => { const authClient = isStartLoaderConvexQueryClient(convex) ? convex.convexClient : convex; const serverHttpClient = isStartLoaderConvexQueryClient(convex) ? convex.serverHttpClient : void 0; const token = await getToken() ?? null; if (startLoaderAuthTokens.get(convex) === token) return { isAuthenticated: token !== null, token }; startLoaderAuthTokens.set(convex, token); if (token === null) { authClient.clearAuth(); serverHttpClient?.clearAuth?.(); return { isAuthenticated: false, token }; } authClient.setAuth(async () => token); serverHttpClient?.setAuth(token); return { isAuthenticated: true, token }; }; //#endregion export { syncConvexAuthForStartLoader };