tRPC + Clerk: user & org info

I'm switching from NextAuth to Clerk. With NextAuth, I got the user info like:
import type { Session } from 'next-auth'

type CreateContextOptions = {
  session: Session | null
}


With Clerk's getAuth, I only get userId and nothing else about the user. It exists in the SignedInAuthObject type but it's always undefined, so I can't read the user's email or orgSlug in my tRPC context.

I can retrieve it like this, but it'd be called too many times and retrieve too much data
export const createTRPCContext = async (opts: CreateNextContextOptions) => {
    // Get the session from the server using the getServerSession wrapper function
    const session = getAuth(opts.req)
    const { userId } = session
    if (!userId) return createInnerTRPCContext({ session })

    const user = await clerkClient.users.getUser(userId)
    const orgs = await clerkClient.users.getOrganizationMembershipList({ userId })

    return createInnerTRPCContext({
        session,
        user,
        orgSlug: orgs[0]?.organization?.slug,
    })
}


tRPC setup: t3 then https://clerk.com/docs/nextjs/trpc
Learn how to integrate Clerk into your Next.js with TRPC
Was this page helpful?