is it possible to set seachParams and keep them through a Nextresponse.next()??
Unanswered
! AlexNotTheLion posted this in #help-forum
Im trying to follow the next js docs and set a viewport search param from middleware, however supabase my Auth provider, sets up middleware to revalidate cookies from the following video https://www.youtube.com/watch?v=v6UvgfSIjQ0
However this causes the searchParams to be removed, how can I get around or fix it??
However this causes the searchParams to be removed, how can I get around or fix it??
18 Replies
@! AlexNotTheLion Im trying to follow the next js docs and set a viewport search param from middleware, however supabase my Auth provider, sets up middleware to revalidate cookies from the following video https://www.youtube.com/watch?v=v6UvgfSIjQ0
However this causes the searchParams to be removed, how can I get around or fix it??
you need to somehow add your searchparams to the url where you redirect your user. So first: build the correct path (with searchparams):
And then redirect to the correct location
const searchParams = request.nextUrl.searchParams.toString();
const path = `${url.pathname}${
searchParams.length > 0 ? `?${searchParams}` : ""
}`;And then redirect to the correct location
im not sure thats possible, im adding the search params when the user is unauthenticated to redirect them to the login dialog on the homepage which works, however the createClient method creates a new nextresponse.next with just the headers of the request so it can update cookies, i dont think i can redirect with that
well... you modify your response here as well. So why not also like the method that I shared?
thats a new response and authentication and headers dont matter, the createClient method sets the cookies which are important to keep users logged in
@! AlexNotTheLion thats a new response and authentication and headers dont matter, the createClient method sets the cookies which are important to keep users logged in
yea and you are not removing them. You are just modifing the path. Not the headers. Not the cookies. Only the path
okay ive tried what you suggested however the params are still not appearing in the url ?
although i might be misunderstanding ?
@! AlexNotTheLion okay ive tried what you suggested however the params are still not appearing in the url ?
oh, you need to directly redirect to your path like that:
I am not 100% into supabase. After redirecting, check if the cookies are set correctly
return NextResponse.redirect(new URL(path, request.url));I am not 100% into supabase. After redirecting, check if the cookies are set correctly
heres the contents of the createclient method, incase it might help, for the most part i think you can ignore the supabase specifics
@! AlexNotTheLion heres the contents of the createclient method, incase it might help, for the most part i think you can ignore the supabase specifics
seems like you can easily add, because it's just an unmodified response. And the supabase object itself is not passend inside the response. So the response, that you receive from your createClient method, is also just an unmodified response. So you should be able do this: https://nextjs-forum.com/post/1253624092616032326#message-1253657187683926059
the page crashes entirely due to too many redirects ?
it does have the params in the url though
ah makes sense, because we rediect in middleware everytime... I guess I can't help you, because I don't know enought how supabase works. Sorry
ah alright bud, no worries thanks for trying though 🙂
bump