Getting 401 error code with public procedure

Hi, I'm working on a project using t3 stack with clerk. I'm trying to get posts from prisma to show in the frontend whether the user is logged in or not, but it only works whenever I'm logged in. I'm using public procedure and the page where I'm trying to show my posts is accesible to users who are not logged in (it's listed as a public route), but when I look at the client console I see a 401 error code on my request. Does anyone know why? Thanks in advance.
No description
No description
No description
No description
4 Replies
Josh Grant
Josh Grant12mo ago
The clerk middleware is protecting your trpc routes.
𝓐𝓷𝓭𝔂
𝓐𝓷𝓭𝔂12mo ago
I see, so what should I change to make those routes accesible for unauthenticated users?
Josh Grant
Josh Grant11mo ago
You may want to look at handling autherization in trpc for the trpc routes. You would modify the middleware for nextjs so the trpc routes are not protected by Clerk directly. https://trpc.io/docs/server/authorization
Authorization | tRPC
The createContext function is called for each incoming request, so here you can add contextual information about the calling user from the request object.
𝓐𝓷𝓭𝔂
𝓐𝓷𝓭𝔂11mo ago
I added "/api/trpc(.*)" on the public routes in the middleware file and I guess I will handle the private/protected procedures with trpc directly, getting the auth state from clerk
Want results from more Discord servers?
Add your server