import { ConvexClient, AuthTokenFetcher } from "convex/browser"; import { api } from "./convex/_generated/api.js"; import * as jose from "jose"; const url = process.env["CONVEX_URL"] const client = new ConvexClient(url!); // 加载测试私钥 const keys = JSON.parse(await Bun.file("./jwt/privateKey.json").text()); const privateKey = await jose.importJWK(keys, "RS256"); // 生成 RS256 JWT const payload = { iss: "https://convex.kevisual.cn", sub: "user:8fa2be73c2229e85", aud: "convex-app", exp: Math.floor(Date.now() / 1000) + 3600, name: "Test User AA", email: "test@example.com", }; const token = await new jose.SignJWT(payload) .setProtectedHeader({ "alg": "RS256", "typ": "JWT", "kid": "kid-key-1" }) .setIssuedAt() .sign(privateKey); console.log("Generated RS256 token:", token); const authTokenFetcher: AuthTokenFetcher = async ({ forceRefreshToken }: { forceRefreshToken: boolean }) => { console.log("AuthTokenFetcher called, forceRefreshToken:", forceRefreshToken); return token; } client.setAuth(authTokenFetcher, (isAuthenticated) => { console.log("Auth isAuthenticated:", isAuthenticated); }); // console.log("Auth set up", auth); await Bun.sleep(1000); // const auth = client.getAuth(); // console.log("Client auth", auth); const unsubscribe = client.onUpdate(api.abcv.get, {}, async (tasks) => { console.log(tasks); }); const list = await client.query(api.abcv.get, {}); console.log("Initial list:", list); // for (let i = 0; i < list.length; i++) { // const a = list[i]; // console.log(`Item ${i}:`, a.title); // } // const list = await client.action(api.abcv.chat, { message: "Hello, 1+1=?" }); // console.log("Chat response:", list); const xiongList = await client.query(api.xiong.get, {}); console.log("Xiong list:", xiongList); // const redisIsConnected = await client.action(api.actions.redis.isConnected, {}); // console.log("Redis isConnected:", redisIsConnected); // const sign = await api.auth.signIn({ provider: "convex" }) // const sign = await httpClient.action("/auth", { method: "POST" }); // console.log("Sign in result:", sign); const res = await fetch("http://convex.kevisual.cn:3211/auth", { method: "POST", }); const data = await res.json(); console.log("Custom endpoint response:", data); await Bun.sleep(1000); unsubscribe(); await client.close();