Next.js middleware gets different url from request then desired

1k views Asked by At

I am developing a module for our web application. The user can access a module in the following way: After the credentials were checked and they are correct the user is redirected to the requested URL. The URL has an acct query param which is a JWT and I need to extract this variable to check if the JWT is not expired. The problem is that if I want to extract it using req.nextUrl.searchParams.get("acct") I get undefined. I tried to troubleshoot and it turns out the middleware sees a different URL than the actual one. The application runs on the company's server and I access it with ssh in vscode or terminal. So the actual URL that I see in the browser's search bar is statdev.smarti.me/?acct={jwt} and the URL that I get in the middleware is http://localhost:6550/_next/webpack-hmr so it might have something to do with the server's internal paths. Also if I try to get search params in the home page component with useRouter() hook it works. Is this some kind of server configuration problem? Is there any way to get the correct URL?

import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";

const { URL_OLOGIN, APP_CLIENT_ID } = process.env;

export function middleware(req: NextRequest) {
  const acct = req.nextUrl.searchParams.get("acct");
  if (acct) {
    console.log(acct);
  } else {
    return NextResponse.redirect(`${URL_OLOGIN}/?cli=${APP_CLIENT_ID}`);
  }
  return NextResponse.next();
}
0

There are 0 answers