Skip to content

Commit

Permalink
fix: conditional prefetch protected procedure in auth templates (t3-o…
Browse files Browse the repository at this point in the history
…ss#1961)

closes t3-oss#1961

Add condition to prefetch `getLatest` only if the user is authenticated.
  • Loading branch information
Guria committed Aug 20, 2024
1 parent 4885a97 commit a13a81f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/lucky-parents-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-t3-app": patch
---

fix issue with prefetch protected procedure in auth templates
4 changes: 3 additions & 1 deletion cli/template/extras/src/app/page/with-auth-trpc-tw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<HydrateClient>
Expand Down
4 changes: 3 additions & 1 deletion cli/template/extras/src/app/page/with-auth-trpc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<HydrateClient>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
});
Expand Down

0 comments on commit a13a81f

Please sign in to comment.