Liam
Liam3mo ago

I switched to directly querying against

I switched to directly querying against the api routes (so it goes client http req -> vercel server -> convex http routes) which has improved speed, but it does not use the user table in my db (instead the one in the component) which is not ideal :/
5 Replies
erquhart
erquhart3mo ago
when you say hitting convex http routes, what do you mean or rather, what you mean is clear, but how are you going about it
Liam
LiamOP3mo ago
Using the better auth client (authClient.getSession()) to hit the next js api routes, which as far as I can tell looking under the hood fwds those requests to the convex site routes.
No description
erquhart
erquhart3mo ago
interesting I just assumed authClient wouldn't work server side so you're using the user from getSession to server render?
Liam
LiamOP3mo ago
Oh yeah no, this is in the client, not on the server. This is ran for trying to get user data to display in the UI via a hook. Its just that this is much faster to get user data then the convex query for some reason. Nearly instant vs a very noticable loading time if I use this convex query:
export const getCurrentUser = query({
args: {},
handler: async (ctx) => {
// Get user data from Better Auth - email, name, image, etc.
const userMetadata = await betterAuthComponent.getAuthUser(ctx);
if (!userMetadata) {
return null;
}
// Get user data from your application's database
// (skip this if you have no fields in your users table schema)
const user = await ctx.db.get(userMetadata.userId as Id<"users">);
return {
...user,
...userMetadata,
};
},
});
export const getCurrentUser = query({
args: {},
handler: async (ctx) => {
// Get user data from Better Auth - email, name, image, etc.
const userMetadata = await betterAuthComponent.getAuthUser(ctx);
if (!userMetadata) {
return null;
}
// Get user data from your application's database
// (skip this if you have no fields in your users table schema)
const user = await ctx.db.get(userMetadata.userId as Id<"users">);
return {
...user,
...userMetadata,
};
},
});
erquhart
erquhart3mo ago
Yeah the token is already there via cookies and no websockets involved, would be faster Server rendering will get it going faster, you can use preloadQuery for that Example: https://github.com/get-convex/better-auth/blob/642be72bc799c827042edac050c03acc48346f8a/examples/next/app/(auth)/dashboard/server/page.tsx#L22

Did you find this page helpful?