Tarky
Tarky2mo ago

Accessing Clerk logged in user in Next Server Components

Hello, I just added Clerk to my Nextjs + Convex app, I also followed the guide to store users in tthe convex database (https://docs.convex.dev/auth/database-auth) The user is being stored in the database correctly, how can I access the logged in user in a server component? Here is what I've tried:
import { fetchQuery } from "convex/nextjs";
import { api } from "@/convex/_generated/api";
import { redirect } from "next/navigation";

export default async function DashboardPage() {
const user = await fetchQuery(api.users.current);

if (!user) {
return redirect("/sign-in");
}

return <div>Welcome, {user?.name}!</div>;
}
import { fetchQuery } from "convex/nextjs";
import { api } from "@/convex/_generated/api";
import { redirect } from "next/navigation";

export default async function DashboardPage() {
const user = await fetchQuery(api.users.current);

if (!user) {
return redirect("/sign-in");
}

return <div>Welcome, {user?.name}!</div>;
}
For some reason, the user is always null Any ideas?
2 Replies
Convex Bot
Convex Bot2mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
ian
ian2mo ago
you need to pass along the user token in fetchQuery. I think there's some docs on it, maybe in the nextjs section, or you could check out the clerk docs / demo / template

Did you find this page helpful?