Next.js Discord

Discord Forum

Router.push seems to remove search params

Answered
io posted this in #help-forum
Open in Discord
ioOP
I'm trying to redirect users to a page (/auth/verify-request) that asks users to check their inboxes.
However, when I use router.push(/auth/verify-request?email=${email}), Next.js seems to silently remove the search param email and redirect to /auth/verify-request.

Is this expected behavior, and is there a way to use router.push() while retaining search params?

Here's the code for the page that redirects to /auth/verify-request:
export const LoginOptions = () => {
  const [isLoading, setIsLoading] = useState<boolean>(false);
  const [email, setEmail] = useState("");

  const router = useRouter();

  async function onSubmit(event: FormEvent<HTMLFormElement>) {
    event.preventDefault();
    setIsLoading(true);

    const res = await signIn("email", { email, redirect: false });
    if (res?.ok) {
      router.push(`/auth/verify-request?email=${email}`);
    }

    setIsLoading(false);
  }

  return (
    <>
      <div className="grid gap-6">
        <form onSubmit={onSubmit}>
          <div className="grid gap-2">
            <div className="grid gap-1">
              <Label className="sr-only" htmlFor="email">
                Email
              </Label>
              <Input
                id="email" 
                type="email"
                autoCapitalize="none"
                autoComplete="email"
                autoCorrect="off"
                disabled={isLoading}
                value={email}
                onChange={(e) => setEmail(e.target.value)}
              />
            </div>
            <Button disabled={isLoading}>
              {isLoading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
              Login with email
            </Button>
          </div>
          
         // ...

I'm using Next.js 14, for context.
Answered by io
I've resolved the issue with removing the
verifyRequest: "/auth/verify-request", config from the next-auth config and exporting export const dynamic= 'force-dynamic'
View full answer

2 Replies

ioOP
I've resolved the issue with removing the
verifyRequest: "/auth/verify-request", config from the next-auth config and exporting export const dynamic= 'force-dynamic'
Answer
ioOP
not completely sure why this works though