nextauthjs / next-auth

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

inconistant results in signIn #4739

Open fotoflo opened 2 years ago

fotoflo commented 2 years ago

Environment

Run env: System: OS: macOS 12.1 CPU: (8) arm64 Apple M1 Memory: 108.56 MB / 16.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 16.15.0 - ~/.nvm/versions/node/v16.15.0/bin/node Yarn: 1.22.18 - ~/.nvm/versions/node/v16.15.0/bin/yarn npm: 8.5.5 - ~/.nvm/versions/node/v16.15.0/bin/npm Browsers: Chrome: 102.0.5005.115 Safari: 15.2 npmPackages: next: 12.1.6 => 12.1.6 next-auth: 4.5.0 => 4.5.0 react: 18.2.0 => 18.2.0

Reproduction URL

https://github.com/fotoflo/gapi-firebase-next

Describe the issue

Im trying to link a second account and get the token 1) the user signs up / signs in with google. A user account is created on Firestore and minimal info is passed to the useSession hook

2) The logged in user now signs in to github - im trying to add their token to the database - i use the sign in callback as below. They do it twice. The first time the user.id comes from github (an 8 digit number). the second time it comes from the database (a 20 char string)

Im hoping that we get the 20 char string every time...

    async signIn({ user, account, profile, email, credentials }) {
      if (account.provider === "github") {
        if (user.id.length != "1sVPXkihaEZ8vFjHYfoK".length){
           // the user id is coming from github - this happens the first time they auth, we dont want this WHY GOD WHY?

           return false;
        } 

           // Else the user id is coming from the database, this happens the subsequent times they auth... THIS SHOULD BE THE BEHAVIOR!!!

        const userRef = doc(db, `/users/${user.id}`);
        await updateDoc(userRef, {
          githubData: account,
        });
        return true;
      }

      return true;
    },
Thread

How to reproduce

pull the d20577142408ff3d02db805ce7c78e22b2aa6e70 commit from https://github.com/fotoflo/gapi-firebase-next

Expected behavior

the user from the database should coem through should come through as user, and the account from the oauth provider should come through. both , every time

chrsgrrtt commented 1 year ago

I too am hitting this issue, albeit with the Apple provider.

chrsgrrtt commented 1 year ago

@fotoflo I managed to resolve this by changing the cookies config option to:

sessionToken: {
  name: `__Secure-next-auth.session-token`,
  options: {
    httpOnly: false,
    sameSite: false,
    path: "/",
    secure: true,
  },
},