I've set up clerk (core v2) with next-on

I've set up clerk (core v2) with next-on-pages for tRPC authorization. I'm using the clerk middleware:
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";

const isProtectedRoute = createRouteMatcher(["/start(.*)"]);

export default clerkMiddleware(
(auth, request) => {
if (isProtectedRoute(request)) {
auth().protect();
}

return NextResponse.next();
},
{ debug: true },
);

export const config = {
matcher: ["/((?!.+\\.[\\w]+$|_next)._)", "/", "/(api|trpc)(._)"],
};
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";

const isProtectedRoute = createRouteMatcher(["/start(.*)"]);

export default clerkMiddleware(
(auth, request) => {
if (isProtectedRoute(request)) {
auth().protect();
}

return NextResponse.next();
},
{ debug: true },
);

export const config = {
matcher: ["/((?!.+\\.[\\w]+$|_next)._)", "/", "/(api|trpc)(._)"],
};
and creating a tRPC context like this:
const createContext = async (req: NextRequest) => {
return createTRPCContext({
headers: req.headers,
auth: getAuth(req),
});
};
const createContext = async (req: NextRequest) => {
return createTRPCContext({
headers: req.headers,
auth: getAuth(req),
});
};
but i'm constantly getting this error message in my tRPC requests:
"message": "Clerk: getAuth() was called but Clerk can't detect usage of clerkMiddleware() (or the deprecated authMiddleware()). Please ensure the following:\n- clerkMiddleware() (or the deprecated authMiddleware()) is used in your Next.js Middleware.\n- Your Middleware matcher is configured to match this route or page.\n- If you are using the src directory, make sure the Middleware file is inside of it.\n\nFor more details, see https://clerk.com/docs/quickstarts/nextjs\n",
"message": "Clerk: getAuth() was called but Clerk can't detect usage of clerkMiddleware() (or the deprecated authMiddleware()). Please ensure the following:\n- clerkMiddleware() (or the deprecated authMiddleware()) is used in your Next.js Middleware.\n- Your Middleware matcher is configured to match this route or page.\n- If you are using the src directory, make sure the Middleware file is inside of it.\n\nFor more details, see https://clerk.com/docs/quickstarts/nextjs\n",
anyone had this issue before? Any ideas on how to debug? Any thoughts at all? I'd be really grateful!
1 Reply
f7f7u7f7
f7f7u7f7OP7mo ago
I managed to get it running by reverting to the old middleware (which is deprecated)
import { authMiddleware } from "@clerk/nextjs/server";

const privateRoutes = [/\/start/];

export default authMiddleware({
publicRoutes: (req) => !privateRoutes.some((route) => route.test(req.url)),
});

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

const privateRoutes = [/\/start/];

export default authMiddleware({
publicRoutes: (req) => !privateRoutes.some((route) => route.test(req.url)),
});

export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};
Some other day, I will give it a shot again!
Want results from more Discord servers?
Add your server