Next.js Discord

Discord Forum

middleware

Unanswered
Alligator mississippiensis posted this in #help-forum
Open in Discord
Alligator mississippiensisOP
import { auth } from "@/server/auth";

const publicPages = ["/", "/login"];

export default auth((req) => {
  if (!req.auth && !publicPages.includes(req.nextUrl.pathname)) {
    const newUrl = new URL("/api/auth/signin", req.nextUrl.origin);
    return Response.redirect(newUrl);
  }
});

export const config = {
  matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};

this is running checks on assets too it seems

4 Replies

@Alligator mississippiensis well i’d rather prefer keeping it in public dir so is there any workaround with that approach
Then you have to edit the matcher regex, or the middleware logic, to detect static files and handle them accordingly. Both are not easy. Regex is never easy.