TarkyT
Convex Community7mo ago
2 replies
Tarky

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>;
}


For some reason, the
user
is always
null


Any ideas?
Was this page helpful?