Skip to content

A network partition can make it impossible for a new user to sign up #876

@abeluck

Description

@abeluck

Describe the bug

There is a transactional bug in the callback handler, specifically here:

user = await createUser(profile)
await dispatchEvent(events.createUser, user)
await linkAccount(
user.id,
providerAccount.provider,
providerAccount.type,
providerAccount.id,
providerAccount.refreshToken,
providerAccount.accessToken,
providerAccount.accessTokenExpires
)

If createUser succeeds, but then linkAccount fails due to a networking issue for example, then it becomes impossible for a user to later login/sign up.

The user is created, but no linked accounts exist.

On later runs through the flow isSignedIn will be false, but a user with the email address will exist resulting in:

const userByEmail = profile.email ? await getUserByEmail(profile.email) : null
if (userByEmail) {
// We end up here when we don't have an account with the same [provider].id *BUT*
// we do already have an account with the same email address as the one in the
// oAuth profile the user has just tried to sign in with.
//
// We don't want to have two accounts with the same email address, and we don't
// want to link them in case it's not safe to do so, so instead we prompt the user
// to sign in via email to verify their identity and then link the accounts.
throw new AccountNotLinkedError()

Steps to reproduce

You can reproduce this by providing a signIn event that uses setTimeout to "sleep" for a long time. Long enough for you to stop the test database to simulate a network partition.

After the flow fails the first time, start the database again and attempt to sign up again.

Expected behavior

A network partition event shouldn't result in making it impossible for a user to sign up.

Screenshots or error logs

n/a

Additional context

n/a

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

  • Found the documentation helpful

The code comments are very complete, which made understanding this bug easier.

Metadata

Metadata

Assignees

No one assigned

    Labels

    adaptersChanges related to the core code concerning database adaptersbugSomething isn't workingenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions