diff --git a/packages/api/trpc.ts b/packages/api/trpc.ts index d49791e..feac7fa 100644 --- a/packages/api/trpc.ts +++ b/packages/api/trpc.ts @@ -2,11 +2,11 @@ import { initTRPC, TRPCError } from '@trpc/server'; import superjson from 'superjson'; import { ZodError } from 'zod'; -import { auth } from '@formbase/auth'; +import { getSession } from '@formbase/auth/server'; import { db } from '@formbase/db'; export const createTRPCContext = async (opts: { headers: Headers }) => { - const session = await auth.api.getSession({ headers: opts.headers }); + const session = await getSession(); const user = session?.user ?? null; return { diff --git a/packages/auth/server.ts b/packages/auth/server.ts index a6413dd..ce3224a 100644 --- a/packages/auth/server.ts +++ b/packages/auth/server.ts @@ -1,10 +1,16 @@ +import * as React from 'react'; import { headers } from 'next/headers'; import { auth } from './index'; -export async function getSession() { +const cache = + typeof React.cache === 'function' + ? React.cache + : unknown>(fn: T) => fn; + +export const getSession = cache(async () => { return auth.api.getSession({ headers: await headers() }); -} +}); export async function requireAuth() { const session = await getSession();