Next.js Discord

Discord Forum

vercel not redirecting to previous page

Unanswered
Giant Angora posted this in #help-forum
Open in Discord
Giant AngoraOP
user redirected to login page after a trying to access a protected page. I am storing the protected page url in search params and after login redirect to that page. (my approach works on localhost just fine but after hosting or vercel it always redirect to homepage)

//middleware.ts
  const { pathname } = request.nextUrl;

  if (!token && isLoginProtectedPath) {
    const redirectUrl = pathname;
    (await cookies()).set('redirect_url', redirectUrl);
    return NextResponse.redirect(new URL('/auth/login', request.url));
  }

//login page

const searchParams = useSearchParams();

    startTransition(async () => {
      const res = await login(payload);

      if (res.success) {
        setIsLogged(true);
        console.log('login - redirectUrl', redirectUrl);
        router.push(searchParams.get('redirect_url') || redirectUrl);
        return;
      }
      toast.error(res.message);
    });

5 Replies

can you send console logs, like the stuff you logged
Giant AngoraOP
login - redirectUrl %2Fsubscriptions
Whats your nextjs version?
Giant AngoraOP
nextjs 15
@Giant Angora login - redirectUrl %2Fsubscriptions
Oh I just noticed that you might need to encode Uri component