nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
24.37k stars 3.41k forks source link

SvelteKitAuth + Google Provider + DrizzleDB subsequent login error #9103

Open andrewlazenka opened 11 months ago

andrewlazenka commented 11 months ago

Environment

System: OS: macOS 13.3.1 CPU: (8) arm64 Apple M1 Memory: 48.30 MB / 16.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 18.18.0 - ~/.nvm/versions/node/v18.18.0/bin/node npm: 9.8.1 - ~/.nvm/versions/node/v18.18.0/bin/npm Browsers: Chrome: 119.0.6045.123 Safari: 16.4 npmPackages: @auth/core: ^0.18.0 => 0.18.0 @auth/drizzle-adapter: ^0.3.5 => 0.3.5 @auth/sveltekit: ^0.3.11 => 0.3.11

Reproduction URL

https://github.com/andrewlazenka/sveltekitauth-drizzle-google

Describe the issue

Duplicate of https://github.com/nextauthjs/next-auth/issues/9102 (forgot to include repro URL).

I am unable to sign out and back in using SvelteKitAuth, Drizzle adapter targeting SQLite, and Google provider.

How to reproduce

  1. Sign in to app
  2. Session is established, user + associated records written to DB
  3. Sign out
  4. Sign in
  5. 500 error screen shown with message in the console (screenshots below)

Image

Image

Expected behavior

I should be able to log back in given I have account information in my DB.

I have a hypothesis as to where this issue is coming from:

https://github.com/nextauthjs/next-auth/blob/1349ed33da3280162bd8156a80e0a4659cb873dd/packages/core/src/lib/routes/callback.ts#L94-L103

Here we attempt to setup a user object from the adapter is it's available. Let's assume in my case this returns a result from the DB.

https://github.com/nextauthjs/next-auth/blob/1349ed33da3280162bd8156a80e0a4659cb873dd/packages/core/src/lib/routes/callback.ts#L117-L122

Below, when we call to handleLogin, only the user object directly from the provider is passed in.

In my hooks.server.ts I setup a custom signIn callback to inspect user details on the sign in. user comes back null (aka the value being passed to the handleLogin method).

This indicates to me there could be an edge case where the sign in is successful and Auth.js should be using the user record from the DB, but it isn't, leading to an incorrect behaviour where the user is attempted to be logged in using their email instead.

Spikeysanju commented 10 months ago

@andrewlazenka same issue happened with me as well. I'm using SvelteKit, Drizzle, Cloudflare D1.

Brainshack commented 8 months ago

I get the same problem. Also with Github Provider. Tried it with Planetscale and Supabase Databases. The adapter is the issue. When i dont use it it works.

What I noticed is that the providerAccountId in the account table seems to be different with every authentication request. No Idea if thats the problem or not but if the providerAccountId is the account id on the providers side and is used to check if the email is used by the same account, this might be the issue. Just guessing though....

DeveloperTheExplorer commented 6 months ago

Is there anyway we can help expedite this?