hey im using the new next js app router and was se...
# support-questions-legacy
i
hey im using the new next js app router and was setting up my middle ware to direct to correct paths based on the subdomain, i'm unsure what is the correct route to go by when integrating auth if i can get some guidance
Copy code
import { NextRequest, NextResponse } from "next/server";
import { customClients } from "./app/config/customClients";

export const config = {
  matcher: [
    /*
     * Match all paths except for:
     * 1. /api routes
     * 2. /_next (Next.js internals)
     * 3. /_static (inside /public)
     * 4. all root files inside /public (e.g. /favicon.ico)
     */
    "/((?!api/|_next/|_static/|_vercel|[\\w-]+\\.\\w+).*)",
  ],
};

export default async function middleware(req: NextRequest) {
  const url = req.nextUrl;

  // Get hostname of request (e.g. demo.vercel.pub, demo.localhost:3000)
  let hostname = req.headers
    .get("host")!
    .replace(".localhost:3000", `.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`);

  // special case for Vercel preview deployment URLs
  if (
    hostname.includes("---") &&
    hostname.endsWith(`.${process.env.NEXT_PUBLIC_VERCEL_DEPLOYMENT_SUFFIX}`)
  ) {
    hostname = `${hostname.split("---")[0]}.${
      process.env.NEXT_PUBLIC_ROOT_DOMAIN
    }`;
  }

  const searchParams = req.nextUrl.searchParams.toString();
  // Get the pathname of the request (e.g. /, /about, /blog/first-post)
  const path = `${url.pathname}${
    searchParams.length > 0 ? `?${searchParams}` : ""
  }`;

  // rewrites for app pages
  if (hostname == `app.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`) {
    console.log("app");
    // const session = await getToken({ req });
    // if (!session && path !== "/login") {
    //   return NextResponse.redirect(new URL("/login", req.url));
    // } else if (session && path == "/login") {
    //   return NextResponse.redirect(new URL("/", req.url));
    // }
    return NextResponse.rewrite(
      new URL(`/app${path === "/" ? "" : path}`, req.url),
    );
  }

  // rewrite root application to `/home` folder
  if (
    hostname === "localhost:3000" ||
    hostname === process.env.NEXT_PUBLIC_ROOT_DOMAIN
  ) {
    console.log("root");
    return NextResponse.rewrite(
      new URL(`/home${path === "/" ? "" : path}`, req.url),
    );
  }

  // Rewrite custom websites to `/custom/customClients[key].folder`
  if (
    Object.keys(customClients).some(
      (key) => hostname === `${key}.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`,
    )
  ) {
    const clientKey = Object.keys(customClients).find(
      (key) => hostname === `${key}.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`,
    );

    if (clientKey !== undefined) {
      const folder = customClients[clientKey].folder;

      // if (path !== "/login") {
      //   return NextResponse.redirect(new URL("/login", req.url));
      // } else if (path == "/login") {
      //   return NextResponse.redirect(new URL("/", req.url));
      // }

      return NextResponse.rewrite(
        new URL(`/custom/${folder}${path === "/" ? "" : path}`, req.url),
      );
    } else {
      // Rewrite to home route if clientKey is undefined
      return NextResponse.rewrite(
        new URL(`/home${path === "/" ? "" : path}`, req.url),
      );
    }
  }

  // rewrite everything else to `/[domain]/[slug] dynamic route
  return NextResponse.rewrite(new URL(`/${hostname}${path}`, req.url));
}
22 Views