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.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