create-t3-app: bug: `useSuspenseQuery` will get "UNAUTHORIZED" tRPC error
Provide environment information
System: OS: macOS 14.2.1 CPU: (8) arm64 Apple M1 Pro Memory: 196.13 MB / 16.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 20.9.0 - ~/.nvm/versions/node/v20.9.0/bin/node Yarn: 1.22.21 - ~/.nvm/versions/node/v20.9.0/bin/yarn npm: 10.1.0 - ~/.nvm/versions/node/v20.9.0/bin/npm pnpm: 8.14.0 - ~/Library/pnpm/pnpm
Describe the bug
If we have a protected api like:
hello: protectedProcedure
.input(z.object({ text: z.string() }))
.query(async ({ input }) => {
// wait for 1 second
await new Promise((resolve) => setTimeout(resolve, 1000));
return {
greeting: `Hello ${input.text}`,
};
}),
Then we use useSuspenseQuery
"use client";
import { api } from "@/trpc/react";
import { Suspense } from "react";
export default function TestSuspense() {
return (
<div>
<h1>Test Suspense</h1>
<Suspense fallback={<div>Loading...</div>}>
<SayHi />
</Suspense>
</div>
);
}
function SayHi() {
const [greet, getGreet] = api.post.hello.useSuspenseQuery({
text: "Suspense user",
});
return <div>{greet.greeting}</div>;
}
We will get the error UNAUTHORIZED from
export const protectedProcedure = t.procedure.use(({ ctx, next }) => {
if (!ctx.session || !ctx.session.user) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return next({
ctx: {
// infers the `session` as non-nullable
session: { ...ctx.session, user: ctx.session.user },
},
});
});
https://github.com/t3-oss/create-t3-app/assets/38809606/ee22bdae-d241-49cd-ac41-f002433b9d6e
You can check the detail of my code in this commit: https://github.com/Crayon-ShinChan/t3-trpc-suspense-bug/commit/b747bbac672c541a62925f35d2ddc9636cb1e316
Reproduction repo
https://github.com/Crayon-ShinChan/t3-trpc-suspense-bug
To reproduce
- clone the repo
- Go to Discord Portal to create an oauth app for this repo and copy paste the
DISCORD_CLIENT_ID,DISCORD_CLIENT_SECRETto .env file pnpm dev
Additional information
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 12
- Comments: 16 (1 by maintainers)
Is this issue tracked upstream somewhere (either tRPC or Next.js)? It’s a critical issue, but this is basically the only thread I could find about it.
Using this package phryneas/ssr-only-secrets seems to be working great to pass the cookie to the headers on the SSR piece
Add a new env variable:
I used the code provided to create the key above:
Then in the layout file, access the cookie, encrypt it and pass it to the TRPCReactProvider
Then decrypt the value and pass it to the headers on the client side. Reading the value on the browser always returns
undefinedso you won’t be able to see it there.I didn’t use
useSuspenseQueryand usedisPendingto determine if I need render skeleton insteadI am coming back here and I realise I’ve had this exact issue in july. I thought I’ll just use useQuery. But not this time.
Let’s understand the problem first: SSR wants to use the TRPC react client to prefetch data. But it has no authorization, it only has authorization for the server client of TRPC. The solution is to install the ssr-only-secrets package which solves this issue.
The code from @sbkl is the solution and I just made it work perfectly.
So it is solvable. I am not sure if it’s implemented into create-t3-app, but this repo is not really plug and play. You have to do some work here and there to unlock it’s potential. Which is fine because we’re web devs 👍
Is there any update on this issue?
I ended up using useQuery instead of useSuspenseQuery and setting throwOnError to true in the defaultOptions of QueryClient. It was too much work for me to prefetch every suspense query.