Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server actions redirect issue #408

Open
jericopulvera opened this issue Jun 3, 2024 · 3 comments
Open

server actions redirect issue #408

jericopulvera opened this issue Jun 3, 2024 · 3 comments

Comments

@jericopulvera
Copy link

jericopulvera commented Jun 3, 2024

I have a login page in a [domain]/login/page.tsx that calls a server action.

  revalidatePath("/");
  redirect('/?test=test');
  
  // tried also
  // redirect('http://tenant1.localhost:3000?test=test');

In the login page of a site e.g: http://tenant1.localhost:3000/login

when you successfully logged in it does redirect to http://tenant1.localhost:3000/ but the page shown is the home page of the platform not the tenant but when you refresh manually you get the home page of the tenant.

I have added a console.log in the middleware.

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", `.${clientEnv.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}` : ""
  }`;

  console.log({ hostname, path });
  //...
}  

Same log output for redirect("/?test=test") and
redirect('http://tenant1.localhost:3000?test=test')

{ hostname: 'org1.localhost:3000', path: '/login' }
{ hostname: 'localhost:3000', path: '/?test=test' }
 POST /login 303 in 168ms
{ hostname: 'org1.localhost:3000', path: '/?test=test' }

so it looks like the issue is that redirect in server actions does not care about the subdomain.

Only tested this locally on next v14.2.3

@jericopulvera
Copy link
Author

Looks to be related to this issue vercel/next.js#65893 (comment)

@jericopulvera
Copy link
Author

does not make sense but the solution here amazingly worked for me. vercel/next.js#65893 (comment)

redirect("./");

@jericopulvera
Copy link
Author

does not make sense but the solution here amazingly worked for me. vercel/next.js#65893 (comment)

redirect("./");

This solution is flawed when the server action is triggered from a differently nested path you'll be redirected to the current path which would result in 404.

e.g: logout action has redirect('./login')

if logout action is triggered at /profile or / you're redirected to /login but
if logout action is triggered at /profile/activity you're redirected to /profile/login.

better solution I found is to use .get("x-forwarded-host") instead of .get("host")

// Get hostname of request (e.g. demo.vercel.pub, demo.localhost:3000)
  const hostname = req.headers
    .get("x-forwarded-host")!
    .replace(".localhost:3000", `.${clientEnv.NEXT_PUBLIC_ROOT_DOMAIN}`);

jericopulvera added a commit to jericopulvera/platforms-1 that referenced this issue Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant