CsarChvz
CsarChvz14mo ago

Error in tRPC with Clerk in t3-app router

Hi guys, im trying to implement clerk auth in t3 app router. But it gives me this error when im trying to query: errorrfile: This is my repository link https://github.com/CsarChvz/t3-approuter-clerk-error I mean even
GitHub
GitHub - CsarChvz/t3-approuter-clerk-error
Contribute to CsarChvz/t3-approuter-clerk-error development by creating an account on GitHub.
7 Replies
siobe
siobe14mo ago
i've also been having a ton of issues getting clerk to work with trpc in t3 app router, taking a quick look through your repo --- in middleware.ts, i found that you have to add your /api/trpc to the publicRoutes of Clerk's authMiddleware method....sorta like this: authMiddleware({publicRoutes: ["/api/trpc/(.*)"]}) Just posting more of my findings below in the hopes we both can find the solution even after you do that though you'll run into additional authentication issues within the src/server/api/trpc.ts file when trying to implement protectedProcedure's. I find the options are: 1. Use Clerk's auth() method which always returns a userId of null 2. Use Clerk's getAuth() method which you need to pass the req object to. But this also just always returns a userId of null Btw if you try to hit trpc from a client component, it'll work fine no issues. The userId will only return null when you try to access trpc from within rsc's. This makes me think the request object we're getting doesn't have everything we need? idk tho
CsarChvz
CsarChvzOP14mo ago
Ty! @siobe I have now the same error about the userId and the userObject:(, null
siobe
siobe14mo ago
Couldn't figure it out tbh, I'm wondering if it's an issue with Clerk's authMiddleware method not fully passing the request through NextResponse to the tRPC route handler in /app/api/trpc/[trpc]/route.ts. But the ppl over at Clerk are definitely smarter than me so it must just be some dumb mistake I can't quite pin down Also some additional info, if you try to use Clerk's getAuth() or auth() methods in an rsc page, it'll work fine there too. Just won't work when trying to use it in the /server/api/trpc file which actually sort of makes sense for the auth() method because it only works when you're in a child of ClerkProvider but with that being said, it should work in the route handler no? Just can't tell if it's my lack of understanding of app router or an actual issue
CsarChvz
CsarChvzOP14mo ago
Maybe an issue c: Idk, im asking in clerks discord because it seems weirdc:
siobe
siobe14mo ago
Please let me know if you find out anything 😭
vibbin
vibbin14mo ago
I'm having the same issues
siobe
siobe14mo ago
This doesn't really solve the issue with Clerk but if anyone wants an alternative I used @supabase/ssr and got auth working. It's a bit more hands on than Clerk in that you have to write your own middleware function

Did you find this page helpful?