nextauthjs / next-auth

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

Discord provider, AuthJs/svelteKit, incorrectly tries to Register user when already "Signed up" using same Discord "Signin" button #6523

Open hgoona opened 1 year ago

hgoona commented 1 year ago

Provider type

Discord

Environment

System: OS: Windows 10 10.0.19045 CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz Memory: 2.35 GB / 15.82 GB Binaries: Node: 16.17.0 - C:\Program Files\nodejs\node.EXE npm: 8.19.2 - C:\Program Files\nodejs\npm.CMD Watchman: 20220918.223204.0 - C:\ProgramData\chocolatey\bin\watchman.EXE Browsers: Chrome: 109.0.5414.120 Edge: Spartan (44.19041.1266.0), Chromium (108.0.1462.76) Internet Explorer: 11.0.19041.1566 npmPackages: @auth/core: ^0.3.0 => 0.3.0 @auth/sveltekit: ^0.2.0 => 0.2.0 @sveltejs/adapter-auto: ^1.0.2 => 1.0.2 @sveltejs/kit: ^1.2.10 => 1.2.10 @typescript-eslint/eslint-plugin: ^5.49.0 => 5.49.0 @typescript-eslint/parser: ^5.49.0 => 5.49.0 autoprefixer: ^10.4.13 => 10.4.13 eslint: ^8.32.0 => 8.32.0 eslint-config-prettier: ^8.6.0 => 8.6.0 eslint-plugin-svelte3: ^4.0.0 => 4.0.0 next-auth: ^4.18.10 => 4.18.10 nodemailer: ^6.9.0 => 6.9.0 postcss: ^8.4.21 => 8.4.21 prettier: ^2.8.3 => 2.8.3 prettier-plugin-svelte: ^2.9.0 => 2.9.0 surrealdb-rest-ts: ^1.0.2 => 1.0.2 surrealdb.js: ^0.5.0 => 0.5.0 svelte: ^3.55.1 => 3.55.1 svelte-check: ^3.0.3 => 3.0.3 tailwindcss: ^3.2.4 => 3.2.4 tslib: ^2.4.1 => 2.4.1 typescript: ^4.9.4 => 4.9.4 vite: ^4.0.4 => 4.0.4

Reproduction URL

https://github.com/hgoona/AppV2_230127_WIP_BUG_Discord_auth

Describe the issue

I'm testing AuthJs with Sveltekit, using Discord provider and a custom Surrealdb Database adapter (derived from PR: https://github.com/nextauthjs/next-auth/pull/6251#issuecomment-1404618493)

I've used the "default" configuration for the SveltekitAuth, but when I signin using the Discord button:

I've tried to recreate this using other providers like Github and Email provider and these do not have any trouble "signing in" an existing user. 🤔

The error on my console: image

How to reproduce

Setup with the above repo + ENV settings.

  1. Signin once with Discord Signin button.
  2. Approve Discord auth
  3. Should arrive at User page.
  4. Signout
  5. Then Signin 2nd time, again using Discord Signin button
  6. Approve Discord auth
  7. this is where error occurs

Expected behavior

At step 7 (above) it should Sign the existing user back into the app (instead of attempting to re-Signup the existing user)

balazsorban44 commented 1 year ago

Is this happening without your custom adapter, or if using one of the built-in ones? It would be good to verify that it's not an adapter bug.

hgoona commented 1 year ago

I'll test this. Can I still use https://www.npmjs.com/package/@next-auth/firebase-adapter with auth/sveltekit?

hgoona commented 1 year ago

I just attempted to mash this tutorial (https://www.youtube.com/watch?v=so9JJ0YFB-s) together with my code, to get Firebase DB adapter working and I'm getting error: FirebaseError: Firebase: Firebase App named '[DEFAULT]' already exists with different options or config (app/duplicate-app). at initializeApp

What i did:

//hooks.server.ts file
...
// FIREBASE ADAPTER
// import GoogleProvider from "next-auth/providers/google"
import { FirestoreAdapter } from "@next-auth/firebase-adapter"
import { db } from "$lib/firebaseConfig";
import * as firestoreFunctions from "firebase/firestore";

export const handle: Handle = sequence(
  SvelteKitAuth({
    // adapter: SurrealDBAdapter(clientPromise),

    // @ts-expect-error issue https://github.com/nextauthjs/next-auth/issues/6174
    adapter: FirestoreAdapter({
      db: db,
      ...firestoreFunctions,
    }),

    providers: [
...
// firebaseConfig.ts file
// Import the functions you need from the SDKs you need
import { initializeApp, getApps, getApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
import { getStorage } from "firebase/storage";

import {
    VITE_FIREBASE_API_KEY,
    VITE_FIREBASE_APP_ID,
    VITE_FIREBASE_AUTH_DOMAIN,
    VITE_FIREBASE_PROJECT_ID,
    VITE_FIREBASE_STORAGE_BUCKET,
    VITE_FIREBASE_MESSAGING_SENDER_ID,
    VITE_FIREBASE_DATABASE_URL,
  } from "$env/static/private"

// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

// Your web app's Firebase configuration
const firebaseConfig = {
    apiKey: VITE_FIREBASE_API_KEY,
    appId: VITE_FIREBASE_APP_ID,
    authDomain: VITE_FIREBASE_AUTH_DOMAIN,
    projectId: VITE_FIREBASE_PROJECT_ID,
    storageBucket: VITE_FIREBASE_STORAGE_BUCKET,
    messagingSenderId: VITE_FIREBASE_MESSAGING_SENDER_ID,
    databaseURL: VITE_FIREBASE_DATABASE_URL,
};

// Initialize Firebase
// const app = initializeApp(firebaseConfig);
const app = getApps.length > 0 ? getApp() : initializeApp(firebaseConfig);
const db = getFirestore(app)
const storage = getStorage(app)

export { app, db, storage }

What am I missing?

balazsorban44 commented 1 year ago

The Firebase Adapter is being refactored (to fix among other things the above issue), so probably not a good candidate to test out with. (#6225)

I would recommend testing without an adapter to see what happens then, and maybe test with the Prisma Adapter with sqlite, which is probably the easiest adapter to set up.

hgoona commented 1 year ago

With zero db adapter and with Email provider disabled, everything performs, of course I get no data persistence to verify much..

@balazsorban44 Is it likely an issue with the SurrealDB Adapter or the Discord provider? - are you able to see any obvious errors in the Surreal Adapter (linked Repo?)?

hgoona commented 1 year ago

BTW @balazsorban44 any idea when Authjs for Sveltekit will step out of Beta? Is it a matter of weeks or months?

hgoona commented 1 year ago

@balazsorban44 It seems I've located the issue of why Discord login is not working as expected.

It's within the Discord's Developer > OAuth2 settings for > Default Authorization Link:

The following needs to be set from "None" to this: ✅applications.commands image