-
Notifications
You must be signed in to change notification settings - Fork 4k
Description
Describe the bug
There is a transactional bug in the callback handler, specifically here:
next-auth/src/server/lib/callback-handler.js
Lines 194 to 205 in 64084d6
| 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:
next-auth/src/server/lib/callback-handler.js
Lines 177 to 186 in 64084d6
| 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.