next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

Access and Refresh tokens not updating

Open sdan opened this issue 6 years ago • 6 comments

Describe the bug Access and refresh tokens aren't updating in the DB if the following scenario happens in twitter:

User revokes application via Twitter settings, user signs in / authorizes the application again, but access/refresh tokens aren't

Steps to reproduce Try it with the example project. Although it doesn't use refresh/access tokens, you can see in the DB it doesn't update.

Expected behavior Access and refresh tokens should update every time a user logs in

Feedback Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.

  • [x] Found the documentation helpful
  • [ ] Found documentation but was incomplete
  • [ ] Could not find relevant documentation
  • [x] Found the example project helpful
  • [ ] Did not find the example project helpful

sdan avatar Nov 16 '20 10:11 sdan

I have this problem with Battlenet also. My solution is to override the signIn callback and use a provided api route to check if the token has expired and if so get a new one and update it. Here is my code:

    signIn: async (user, account, profile) => {
      console.log(account.accessToken);
      const checkRes = await fetch(
        `https://us.battle.net/oauth/check_token?token=${account.accessToken}`,
        {
          method: "POST",
        }
      );
      console.log("Status:", checkRes.status);
      if (checkRes.status === 200) {
        const res = await prisma.account.update({
          where: { id: user.id },
          data: {
            accessToken: account.accessToken,
          },
        });
      }
      return Promise.resolve(true);
    },

My issue now is if the user doesn't already exist (I.E. its a new user) this code bugs and the none of the tables update with the new user. Still have to figure this out, but hopefully this points you in the right direction.

EthanNC avatar Nov 16 '20 20:11 EthanNC

Hi there! It looks like this issue hasn't had any activity for a while. It will be closed if no further activity occurs. If you think your issue is still relevant, feel free to comment on it to keep it open. (Read more at #912) Thanks!

stale[bot] avatar Jan 15 '21 20:01 stale[bot]

Hi there! It looks like this issue hasn't had any activity for a while. To keep things tidy, I am going to close this issue for now. If you think your issue is still relevant, just leave a comment and I will reopen it. (Read more at #912) Thanks!

stale[bot] avatar Jan 22 '21 21:01 stale[bot]

This is my solution to this problem and the problem in #735

const options: InitOptions = {
  providers: [
    Providers.BattleNet({
      clientId: process.env.BATTLENET_CLIENT_ID!,
      clientSecret: process.env.BATTLENET_CLIENT_SECRET!,
      region: 'us',
      scope: 'openid wow.profile',
      authorizationUrl: "https://us.battle.net/oauth/authorize?response_type=code"
    })
  ],
  database: process.env.MONGODB_URI,
  callbacks: {
    signIn: async (user, account, profile) => {
      // Next-Auth does not update the access token if the user has revoked the token then relogs in

      // We manually update the access token
      // BattleNet currenlty does not have a refresh token. No need to update it as well

      await dbConnect();
      await Account.findOneAndUpdate({ providerAccountId: account.id }, { accessToken: account.accessToken });

      return Promise.resolve(true);
    }
  }
};

BrennenRocks avatar Jan 28 '21 16:01 BrennenRocks

Any fix added for this issue?

nachiketAsurion avatar Oct 28 '21 18:10 nachiketAsurion

This is still an issue any updates on it?

glockjt avatar Mar 11 '22 15:03 glockjt