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](https://cdn.answeroverflow.com/1162277629840343180/401.png)
![No description](https://cdn.answeroverflow.com/1162277630167494707/cts.png)
![No description](https://cdn.answeroverflow.com/1162277630469492826/mts.png)
![No description](https://cdn.answeroverflow.com/1162277630830198804/pts.png)
4 Replies
The clerk middleware is protecting your trpc routes.
I see, so what should I change to make those routes accesible for unauthenticated users?
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.
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