platforms icon indicating copy to clipboard operation
platforms copied to clipboard

example redirect to `${ROOT_DOMAIN}/login` to `app.${ROOT_DOMAIN}/login`

Open arafays opened this issue 1 year ago • 1 comments

I have been trying to redirect the login page of the app for a while this is how I got it done. https://github.com/vercel/platforms/blob/7e3355678a56d9368e49cd090d729701ba4eff57/middleware.ts#L62-L69

if (
  hostname === "localhost:3000" ||
  hostname === process.env.NEXT_PUBLIC_ROOT_DOMAIN
) {
  const https = hostname === 'localhost:3000' ? 'http' : 'https';
  if (path === "/login") {
    return NextResponse.redirect(new URL(`${https}://app.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}/login`, req.url));
  }  else if (path === "/settings") {
    return NextResponse.redirect(new URL(`${https}://app.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}/settings`, req.url));
  }

  return NextResponse.rewrite(
    new URL(`/home${path === "/" ? "" : path}`, req.url),
  );
}

am I doing this correctly or is there a better way?

arafays avatar Aug 07 '24 20:08 arafays

I wrote up a response recently in another issue with the same issue: https://github.com/vercel/platforms/issues/329#issuecomment-2182022845. This was the best way I found

CrutchTheClutch avatar Sep 21 '24 04:09 CrutchTheClutch

👋 We've updated this repo to have a more simplified example + a new guide:

  • https://github.com/vercel/platforms/pull/451
  • https://vercel.com/guides/nextjs-multi-tenant-application

leerob avatar May 11 '25 14:05 leerob