How do I setup App router + TRPC + Lucia Auth? (protected routes)
I'm trying to setup tRPC with Lucia in app router but I struggle to make protected routes work.
I made a trpc context where I put lucia session and a getDefaultSession function to get the session
I wrote a isAuth function to make the protected procedure
and added it in my route.ts handler
But the session is not passed into ctx resulting in always getting the unauthorized error. Does someone know what I did wrong to make this work?
6 Replies
Jack Herrington
YouTube
tRPC + NextJS App Router = Simple Typesafe APIs
Upcoming course: pronextjs.dev
👉 Code : https://github.com/jherr/trpc-on-the-app-router
👉 Don't forget to subscribe to this channel for more updates: https://bit.ly/2E7drfJ
👉 Discord server signup: https://discord.gg/ddMZFtTDa5
👉 VS Code theme and font? Night Wolf [black] and Operator Mono
👉 Terminal Theme and font? oh-my-posh with powerlevel1...
This is the video I initially followed, but it doesn't provide protected procedures. My solution was to change the serverClient into:
Why? Look how protected procedures were implemented by Theo, for example, in his Chirp guide. And do the same.
It was a bit more difficult to get right on the rsc side, but I made it work in almost a similar way, I only miss the useQuery part now
"But the session is not passed into ctx resulting in always getting the unauthorized error. Does someone know what I did wrong to make this work?"
I'm running into the exact same issue trying to get Clerk working with app router (Theo's Chirp video showed it working in pages router, not in app router).
How did you manage to get the ctx to pass the request with the auth info in this line:
const session = await getDefaultSession(req)
With Clerk I'm trying to get the auth session with const auth = getAuth(req)
and it always returns nullWhat I’ve used in a couple projects: