Search code examples
twitterjwttwitter-oauthnext-auth

`account.access_token` & `account.refresh_token` is `undefined` when using Twitter Provider with `next-auth`?


I am following this tutorial & am unable to get the value of access_token & refresh_token from next-auth. My callback function looks like:

callbacks: {
    async jwt({ token, account, user }) {
      if (account) {
        token[account.provider] = {
          accessToken: account.access_token,
          refreshToken: account.refresh_token,
        }
        console.log(token[account.provider])
      }

      return token
    },
  },

It currently logs { accessToken: undefined, refreshToken: undefined } to the console.

This is how it's shown in the docs when using jwt callbacks but it's not working for me.

I have made a complete reproduction on branch next-auth-with-twitter-api-v2https://github.com/deadcoder0904/twitter-api-v2-3-legged-login-using-next-connect/tree/next-auth-with-twitter-api-v2

Edit:

It looks like it works on v3. See branch next-auth-v3-with-twitter-api-v2https://github.com/deadcoder0904/twitter-api-v2-3-legged-login-using-next-connect/tree/next-auth-v3-with-twitter-api-v2


Solution

  • So the access_token got renamed to oauth_token & refresh_token got renamed to oauth_token_secret:

    callbacks: {
        async jwt({ token, account, user }) {
          if (account) {
            token[account.provider] = {
              accessToken: account.oauth_token,
              refreshToken: account.oauth_token_secret,
            }
          }
    
          return token
        },
      },