Search code examples
oauthgoogle-oauthvercelnext-authnext.js13

callbackUrl not working on production with nextauth google provider


In my use case, not sensitive information is generated before the user signs up, I want to be able to use oauth and tie this information after the signup flow.

When I use the signIn function with the google provider, I want to pass data through search parameters in the callbackUrl, I am able to get it on localhost, but when I deploy on Vercel it does not work, the google provider simply redirects to my home page.

on my custom sign in

...
signIn('google', {
    callbackUrl: MY_ROUTES.linkNewUser.path + "?someData=12345",
});
...

on the "link-new-user" api route

...
const someData = searchParams.someData;
...

Solution

  • As stated in the Google docs for Oauth it is not possible to use a fragment (search parameters) in the redirect_uri.

    Since it is more tolerant with urls matching "localhost", it will work in a dev environment but not on production.

    To be able to achieve my goal I simply saved a cookie before the signup flow, and retrieved it afterwards. There are many strategies to achieve this goal, and to set and get cookies, I won't cover them here.