I have an email auth and after user clicks the button to access my website I have to check whether they have tokens or not. When they are redirected to my page from Gmail they have verification_token=**
searchParam and if they have already been authorized I have to delete it.
As NextJS docs say, I have to use NextResponse.rewrite
function in middleware.ts with deleting that token but next just shows the page with the same search param. I use app router and Next 14
As I mentioned, I used rewrite function with deleting the searchParam with url.searchParams.delete('verification_token');
but it does not help. The param is still there.
I have tried multiple variants with nextResponse.redirect
etc. but nothing helped.
The NextResponse.rewrite
function proxies requests, so the url shouldn't change. Using NextResponse.redirect
worked for me:
import { NextResponse } from "next/server";
export function middleware(req) {
const nextUrl = req.nextUrl;
if (nextUrl.searchParams.has("verification_token")) {
nextUrl.searchParams.delete("verification_token");
return NextResponse.redirect(nextUrl);
} else {
return NextResponse.next();
}
}