Next.js Discord

Discord Forum

How can I keep the callBackUrl when I need to add ?view=otp after I see the /login page

Unanswered
Asiatic Lion posted this in #help-forum
Open in Discord
Asiatic LionOP
My problem :
I have a login page which only works with otp and phone number, it looks like this when we try to go to dashboard for example :
/login?callbackUrl=http%3A%2F%2Flocalhost%3A3000%2Fdashboard

So it is correct till now, but when I enter the phone number I need to recieve an otp so that callback url clears out and this otp replaces it :
/login?view=otp&phone=09123456789

What should I do to handle this? so It won't break the OTP page and redirection

3 Replies

Asiatic LionOP
# The related part of the code :

    try {
      setLoading(true);
      const data = await loginUser(phoneNumber)
      console.log('Login response:', data)
      if (data.result) {

        router.push(`?view=otp&phone=${phoneNumber}`)
        setTimer(120)
        setIsResendDisabled(true)
        ...
      } else {
        ...
      }
    } catch (error: any) {...}
  }
Asiatic LionOP
Any thoughts on this? I'd appreciate
@Asiatic Lion # The related part of the code : tsx try { setLoading(true); const data = await loginUser(phoneNumber) console.log('Login response:', data) if (data.result) { router.push(`?view=otp&phone=${phoneNumber}`) setTimer(120) setIsResendDisabled(true) ... } else { ... } } catch (error: any) {...} }
const sp = new URL(window.location.href).searchParams;
console.log(sp.toString());
// foo=bar
sp.set("view", "otp");
sp.set("phone", "123456");
console.log(sp.toString());
// foo=bar&view=otp&phone=123456
router.push(`?${sp.toString()}`);