Clerk middleware public routes

BBootesVoid9/11/2023
I have an ecommerce website with the / route being public, and I'm fetching categories from a publicProcedure, but it doesn't work if I'm not logged with clerk.
SSturlen9/11/2023
make sure you Clerk middleware isn't matching for public routes. not much more to go on unless you can post the code
BBootesVoid9/11/2023
So I fixed it by adding the specific trpc routes to publicRoutes,
import { authMiddleware } from "@clerk/nextjs";
import { redirect } from "next/navigation";
import { prisma } from "./server/db";

// This example protects all routes including api/trpc routes
// Please edit this to allow other routes to be public as needed.
// See https://clerk.com/docs/references/nextjs/auth-middleware for more information about configuring your middleware
export default authMiddleware({
publicRoutes: ["/api/webhooks/user", "/", "/api/trpc/category.getCategories", "/api/trpc/category.getSubCategories", "/api/trpc/category.getAllSubCategories"],
});

export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};
import { authMiddleware } from "@clerk/nextjs";
import { redirect } from "next/navigation";
import { prisma } from "./server/db";

// This example protects all routes including api/trpc routes
// Please edit this to allow other routes to be public as needed.
// See https://clerk.com/docs/references/nextjs/auth-middleware for more information about configuring your middleware
export default authMiddleware({
publicRoutes: ["/api/webhooks/user", "/", "/api/trpc/category.getCategories", "/api/trpc/category.getSubCategories", "/api/trpc/category.getAllSubCategories"],
});

export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};
but I dont want to do this for every route, why is this happening even though im using a publicProcedure. So I added these three, "/api/trpc/category.getCategories", "/api/trpc/category.getSubCategories", "/api/trpc/category.getAllSubCategories"
SSturlen9/11/2023
a better solution would proably to let tRPC handle the route protection by itself and not running a seperate clerk middleware. create T3 app with next-auth handles this by default. I don't have an example at hand but the docs might have one. just replace the next-auth enforceUserIsAuthed with clerk's
JJosip9/12/2023
Any idea how to let tRPC handle route protection without Clerk middleware? As soon as middleware is removed, hitting tRPC routes errors out: tRPC failed on <no-path>: You need to use "authMiddleware" (or the deprecated "withClerkMiddleware") in your Next.js middleware file. You also need to make sure that your middleware matcher is configured correctly and matches this route or page.
SSturlen9/13/2023
according to the docs, using /api/trpc/* as a public route in your middleware should let tRPC handle it instead of Clerk. https://clerk.com/docs/references/nextjs/auth-middleware#making-pages-public-using-public-routes
BBootesVoid9/13/2023
thanks!
BBootesVoid9/18/2023
I just got a chance to try this
No description
BBootesVoid9/18/2023
any idea what might be the problem
Tteos9/18/2023
Without the code, it'll be difficult to help you
Xxkenzy9/18/2023
are u using ?
export default authMiddleware({
publicRoutes: ["/", "/api/trpc(.*)"],
});
export default authMiddleware({
publicRoutes: ["/", "/api/trpc(.*)"],
});
it creashes if you use directly /api/trpc/*
BBootesVoid9/19/2023
ah right I did this. Thanks

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Nextjs middleware being ignoredHey, this is driving me crazy, using next, why I'm not being redirected to "/pending" by the middlewError: Invariant: headers() expects to have requestAsyncStorage, none available.Created a fresh create-t3-turbo and added my .env variables but getting the following error. - erT3 Tutorial - clerk errors out on a different machine?Strange.. So, I followed Theo's t3 tutorial, everything worked fine last Friday. Today I cloned the Handling different platforms with git hooks?I'm trying to set up something like `commitizen` with git hooks (either traditional or Husky) and ruDrizzle / PlanetScale Table RelationsDoes anyone possibly have a link to a repo I can take a look at to see how relations are setup with How to handle FE requests > 1MB?Hi all, I am sending some large polygons from my frontend for evaluation in the backend but when thshadcn/ui components are not rendering.Hi, I've recently started testing T3 stack and I'm trying to implement some shadcn ui's however whenHow do i learn correctly?I'm currently trying to learn react. I know building small projects is the best way to learn. But saTemplate SuggestionsAre there any T3 apps that petty functional out of the box? I’m interested in building a quick adminRead replica from MSSQL to cloud for T3 app?Hi! I plan to build a webstore with T3 stack, but the inventory data is in an old MS SQL 2014 serverWhat kind of db/storage do I need?I am trying to make an app but I am confuse by all the DB options and idk what is the best option foWeb Framework Suggestions! (Senior project)Looking for web framework recommendations on a big upcoming group project. Main requirement is it haCreating internal tools for company usageHi, I would like to develop a tool that will only be used by people at my company. I have looked evezustand + next trpc/client. With slightly more complex feature. Vanilla client?The problem i'm having is that trpc are hooks and not async functions that i can call in my zustand deep questions t3 prisma client doesn’t seem to updateMy prisms client doesn’t seem to show the array relationships which are present in the prisms schemaWhat is a solution for inserted html with styling that overrides it's enclosure?I have a NextJS app where I receive emails in HTML. Most of the time, they have a custom style, thesNext.js AppRouter: not a valid export fieldI've hit a wall when trying to add my own exports in a `route.ts`, side to side with `GET`, `POST`, First component with DaisyUI and Nextjs gives runtime errorNew to nextjs 13. Have only been using Nextjs 12 and less in production. Initialized a nextjs 13 fortype errors even after disabling```json 22:32 Error: Unsafe assignment of an `any` value. @typescript-eslint/no-unsafe-assignment Error: <rejected> TypeError: Body is unusableI am using the server api utapi for file upload. Even though file uploading and deleting work just f