Next.js Discord

Discord Forum

BUG: when basePath is set nextjs does not redirect properly

Unanswered
Australian Freshwater Crocodile posted this in #help-forum
Open in Discord
Australian Freshwater CrocodileOP
Hi, i'm trying to protect everything except one path. I have the following config in middleware.ts

import { withAuth } from "next-auth/middleware";

export default withAuth({
  pages: {
    signIn: "/auth/signin", // this is to force okta auth without showing the button
  },
});

export const config = { matcher: ["/((?!api/applications/add).*)"] };


if i go to /overview then this is protected. But when i go to / then this is not protected. My api/applications/add path is unprotected as expected.

I've tested with pathToRegexp locally with the following code:
const { pathToRegexp, match, parse, compile } = require("path-to-regexp");

const keys = [];
const re = pathToRegexp(["/((?!api/applications/add).*)"], keys);

const match1 = match(re, "/api/applications/add", keys);

console.log(re.test("/api/applications/add"));
console.log(re.test("/overview"));
console.log(re.test("/")); 

The output is
false
true
true


If i set the matcher to
export const config = { matcher: ["/:path*"] };

The root is protected, but of course also /api/applications/add which i don't want.

Please advise?

3 Replies

Australian Freshwater CrocodileOP
Adding some info. In my setup i have a basePath configured in next.config.js. When i remove the basePath and go for localhost:3000 i see that i'm redirected to localhost:3000/ . When i have my basePath enabled and browse to http://localhost:3000/otp/transportation-admin-ui/v1 i'm not redirected to http://localhost:3000/otp/transportation-admin-ui/v1/
Australian Freshwater CrocodileOP
The weird thing is that export const config = { matcher: ["/:path*"] }; secures the /
Oak rough bulletgall wasp
Hey there @Australian Freshwater Crocodile - did you ever figure this out? I am having the same exact issue.