Next.js Discord

Discord Forum

Middleware ERROR

Unanswered
North Pacific hake posted this in #help-forum
Open in Discord
North Pacific hakeOP
I'm getting this error: Error: URL is malformed "null/dashboard". Please use only absolute URLs - https://nextjs.org/docs/messages/middleware-relative-urls

My middleware.ts:
import { auth } from "./auth";
import { NextResponse } from "next/server";
import { PUBLIC_ROUTES, AUTH_ROUTES, DEFAULT_REDIRECT} from "./routes";

export default auth((req) => {
    
    const isLogged = req.auth;
    const currentRoute = req.nextUrl.pathname;
    let redirectAfterLogin = "";

    // to show public routes
    if(!isLogged && AUTH_ROUTES.includes(currentRoute)) return;

    // to show auth routes
    if (!isLogged && !PUBLIC_ROUTES.includes(currentRoute)) {
        redirectAfterLogin = `?redirect=${currentRoute}`;
        return NextResponse.redirect(new URL(`${req.nextUrl.origin}/login${redirectAfterLogin}`));
    }

    // to show private routes
    if(isLogged && AUTH_ROUTES.includes(currentRoute)){
        return NextResponse.redirect(new URL(`${req.nextUrl.origin}${DEFAULT_REDIRECT}`))
    }

    return;
});

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


I've tried everything but still getting this error. But the exact same code works in others projects. The exact same code

0 Replies