Error when using twitter social sign in with multiple callback URLs

I have configured 2 possible callback URLs in x API developer portal:

https://mydomain.app/api/auth/callback/twitter
http://localhost:3001/api/auth/callback/twitter

if I don't specific the callbackURL in the signin function, it seems to always use localhost:3001 even when the app is deployed to prod to https://mydomain.app


However when I specify the callback URL like this

await authClient.signIn.social({
      provider: 'twitter',
      callbackURL:
        process.env.NODE_ENV === 'development'
          ? 'http://localhost:3001/api/auth/callback/twitter'
          : 'https://mydomain.app/api/auth/callback/twitter',
    })


It does not work.

on localhost I get this error: Error Code: state_not_found but after clicking "Return to application" my user was logged in successfully.

In production, however it doesn't even redirect to x.com I just get this error response to a request to https://mydomain.app/api/auth/sign-in/social:

{
    "code": "INVALID_CALLBACKURL",
    "message": "Invalid callbackURL"
}


I have triple checked the callback URL and it matches the one configured in X API Developer portal.

What am I doing wrong?
Was this page helpful?