Issue with trpc fetch when trying to pass Clerk auth to context
I'm currently trying to add Clerk auth to my trpc context, but I just keep getting this error:
Error: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
This is my code:
context.ts
trpc.ts
36 Replies
Everything also seems to work fine when I log in and make a call to my protectedProcedure, it's only a problem when I log out.
Kinda seems like you're getting the default 404 html page from next as a response. So maybe my first guess would be that you set up the route handler incorrectly
At least everytime I encountered an error like that it was because I had set up something wrong in the route handler
did you export the methods correctly in the route handler like this?
Yep I did here's my route handler
api/trpc/[trpc]/route.ts
So after a lot of messing around it seems to caused by something with the Clerk Middleware. I'm still not exactly sure what is the problem or why though.here's a link to a repo with the same issue as well:
https://github.com/Entropy-10/clerk-trpc-issue
GitHub
GitHub - Entropy-10/clerk-trpc-issue
Contribute to Entropy-10/clerk-trpc-issue development by creating an account on GitHub.
I implemented this the other week, this is how I added the clerk auth to the context
and for the middleware:
in my case any TRPC calls would happen post auth, so I added the auth check in the middleware, but feel free to remove that if your case differs
I see thank you for the information! It turns out that Clerk will send everything to the sign in page including api's so I just had to add my api route for trpc to my clerk middleware public routes.
cool, glad I was of help 🙂
i'm new here so sorry if i'm asking in the wrong place, but did this code snippet work for you when using the app router or pages router?
been trying to implement Clerk authentication with t3 using the app router and running into issues with Clerk's getAuth() and auth() methods both returning null for auth specifically in this line:
auth: getAuth(req),
in the createInnerTRPCContext
methodsame - any update here? @siobe @koko#1337 @Entropy @CsarChvz
been stuck on this for a whole day now
you just need to add your api to your public routes on the Clerk middleware so:
Though if you are following the Clerk docs and use the matcher they have it should prevent your middleware from running on
/trpc
or /api
endpoints
thanks for getting back to me! @Entropy
okay so i did this
will give it a run
and also just to be clear i'm using
getAuth
to create the context - does that work with your solution?would masively apprecoate any help you can give :)
They actually have a guide that can help you setup tRPC with Clerk here that I'd recommend following.
https://clerk.com/docs/references/nextjs/trpc
Integrate Clerk into your Next.js app with tRPC | Clerk
Learn how to integrate Clerk into your Next.js application using tRPC. tRPC can be used with Clerk, but requires a few tweaks from a traditional Clerk + Next.js setup.
yeah i already setup that whole thing
i've been like banging my head against the wall on this for >1d its so frustrating bc those docs don't work + are old
yeah still not working btw
also it's redundant to have the
/
and /api/(.*)
routes in your publicRoutes
since the matcher already prevents the middleware from running on those routes.
Is that after you go through the sign in flow?yeah
im signed in already
and in the application code you can get the session, but just not on your tRPC context?
yep
ive run out of ideas
is it that hard to migrate to nextauth?
Uh I mean really depends on what you need but I would say compared to Clerk that next-auth is a bit harder to setup. I found this github really quickly if you want to take a peak at their implementation. Otherwise I would wait for someone more knowledgeable to help out.
https://github.com/solaldunckel/next-13-app-router-with-trpc
GitHub
GitHub - solaldunckel/next-13-app-router-with-trpc: Next 13 app dir...
Next 13 app dir with tRPC, Kysely, Planetscale and Turborepo - GitHub - solaldunckel/next-13-app-router-with-trpc: Next 13 app dir with tRPC, Kysely, Planetscale and Turborepo
thanks will take a look
is that a working implementation though? the repo is from 6 months ago
no clue just the first thing I found sorry
do you have a working implementaion of app router + trpc + clerk?
if so could i see your code?
Let me check to see really quick this was from a while ago when I was testing for a tech stack, but decided to not go with tRPC or Clerk
okay! thanks
:)
I'm curious if you did
const auth = getAuth(opt.req)
and then just logged auth
what that returns.null
i already tried them all
hmmm and sorry to ask again, but if you did
auth()
in your server components it returns the correct info?let me try that - thanks for the suggestion!
GitHub
GitHub - isaackoz/t3-app-router-clerk
Contribute to isaackoz/t3-app-router-clerk development by creating an account on GitHub.
here's a working repo but uses cookies from opts to verify the session rather than the getAuth method
i couldn't get it to work when i passed opt.req to auth() or to getAuth()
GitHub
GitHub - isaackoz/t3-app-router-clerk
Contribute to isaackoz/t3-app-router-clerk development by creating an account on GitHub.