From 5c2e8534225a33a175805e543344609ba367d4b6 Mon Sep 17 00:00:00 2001 From: Aleksei Gurianov Date: Mon, 19 Aug 2024 23:46:10 +0300 Subject: [PATCH] fix issue with prefetch protected procedure in auth templates Fixes #1961 Add condition to prefetch `getLatest` only if the user is authenticated. --- .changeset/lucky-parents-invent.md | 5 +++++ cli/template/extras/src/app/page/with-auth-trpc-tw.tsx | 4 +++- cli/template/extras/src/app/page/with-auth-trpc.tsx | 4 +++- .../extras/src/server/api/routers/post/with-auth-drizzle.ts | 2 +- 4 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 .changeset/lucky-parents-invent.md diff --git a/.changeset/lucky-parents-invent.md b/.changeset/lucky-parents-invent.md new file mode 100644 index 0000000000..99e678b28e --- /dev/null +++ b/.changeset/lucky-parents-invent.md @@ -0,0 +1,5 @@ +--- +"create-t3-app": patch +--- + +fix issue with prefetch protected procedure in auth templates diff --git a/cli/template/extras/src/app/page/with-auth-trpc-tw.tsx b/cli/template/extras/src/app/page/with-auth-trpc-tw.tsx index 49c9bbbe20..1d462b4441 100644 --- a/cli/template/extras/src/app/page/with-auth-trpc-tw.tsx +++ b/cli/template/extras/src/app/page/with-auth-trpc-tw.tsx @@ -8,7 +8,9 @@ export default async function Home() { const hello = await api.post.hello({ text: "from tRPC" }); const session = await getServerAuthSession(); - void api.post.getLatest.prefetch(); + if (session?.user) { + void api.post.getLatest.prefetch(); + } return ( diff --git a/cli/template/extras/src/app/page/with-auth-trpc.tsx b/cli/template/extras/src/app/page/with-auth-trpc.tsx index cfeed2f5b5..74cac168f9 100644 --- a/cli/template/extras/src/app/page/with-auth-trpc.tsx +++ b/cli/template/extras/src/app/page/with-auth-trpc.tsx @@ -9,7 +9,9 @@ export default async function Home() { const hello = await api.post.hello({ text: "from tRPC" }); const session = await getServerAuthSession(); - void api.post.getLatest.prefetch(); + if (session?.user) { + void api.post.getLatest.prefetch(); + } return ( diff --git a/cli/template/extras/src/server/api/routers/post/with-auth-drizzle.ts b/cli/template/extras/src/server/api/routers/post/with-auth-drizzle.ts index 71449aeb53..b8954c8762 100644 --- a/cli/template/extras/src/server/api/routers/post/with-auth-drizzle.ts +++ b/cli/template/extras/src/server/api/routers/post/with-auth-drizzle.ts @@ -25,7 +25,7 @@ export const postRouter = createTRPCRouter({ }); }), - getLatest: publicProcedure.query(async ({ ctx }) => { + getLatest: protectedProcedure.query(async ({ ctx }) => { const post = await ctx.db.query.posts.findFirst({ orderBy: (posts, { desc }) => [desc(posts.createdAt)], });