Skip to content

Account linking does not work correctly when using JWT #625

@RobertCraigie

Description

@RobertCraigie

Describe the bug
Signing in with email and then signing in with an OAuth provider creates two separate users when using JWT sessions.

It should be noted that I only tested this issue with an OAuth provider that does not grant access to the user's email address

Steps to reproduce
https://github.com/RobertCraigie/next-auth-multiple-user-bug

  • Sign in with email
  • Sign in with Bungie

Expected behavior
One user is created and the OAuth account is linked to said user.

Screenshots or error logs
https://imgur.com/a/sI2pNzp

Additional context
I have traced the root cause of this issue to

if (session && session.user) {

This block of code expects the decoded JWT to have a user property when it does not.

A simple fix is to add the user property in the JWT callback

...
  callbacks: {
    jwt: async (token, user, account, profile, isNewUser) => {
      const isSignIn = user ? true : false;

      if (isSignIn) {
        token.user = { id: user.id }
      }

      return Promise.resolve(token);
    }
  },
...

Feedback

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp-neededThe maintainer needs help due to time constraint/missing knowledge

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions