nextauthjs / next-auth

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

OAuthAccountNotLinked Error on Subsequent Google Sign-Ins [Drizzle ORM] #11261

Open 4rch1m3d35 opened 3 weeks ago

4rch1m3d35 commented 3 weeks ago

Adapter type

@auth/drizzle-adapter

Environment

corepack@0.28.2 next@14.2.4 next-auth@5.0.0-beta.19

Reproduction URL

https://github.com/wpcodevo/nextauth-nextjs14-drizzle/tree/main

Describe the issue

I have the same issue with OAuthAccountNotLinked with Google and the Drizzle Adapter(mysql2) as shown in this issue. I have tried all the solutions suggested in the thread, it's been almost a week, so I am starting a new thread just in case that issue thread is too old, I realize that all of you are probably busy with the beta currently in progress and I am just making sure that the issue is seen.

10062

I went from next-auth 4 directly to beta.19 and the user was not made in next-auth 4

How to reproduce

I used authjs to sign into Google, signed out and I got the error when I attempted to sign back in.

Expected behavior

Authjs should just let me log in without spitting out an error.

4rch1m3d35 commented 1 week ago

Image of the error that I'm getting when I try to login oautherror

4rch1m3d35 commented 1 week ago

From what I can tell, the Provider Account ID keeps changing so it can't pull the account provider info from the database, causing it to continually try creating a new account. Please let me know if there is a solution to this problem.

CodeWithAlvin commented 1 week ago

adding a notNull unique feild to users table that have a default function produce this error for me If I remove that it works fine Purticularly the feild below

referralCode: varchar('referralCode', {length:10}).notNull().unique().$defaultFn(()=>genrateReferralCode(8))

Tried to update feild from auth.ts inside providers -> Google() Still got the same error

CodeWithAlvin commented 5 days ago

adding a notNull unique feild to users table that have a default function produce this error for me If I remove that it works fine Purticularly the feild below

referralCode: varchar('referralCode', {length:10}).notNull().unique().$defaultFn(()=>genrateReferralCode(8))

Tried to update feild from auth.ts inside providers -> Google() Still got the same error

For some Reason Replacing genrateReferralCode(8) to genrateReferralCode worked for me. I make default value of parameter to 8 in function defination

 referralCode: varchar('referralCode', {length:10}).notNull().unique().$defaultFn(genrateReferralCode)
4rch1m3d35 commented 5 days ago

I did some investigation on my own and this function in the internals is not pulling anything that contains the Provider Account ID from the database, there by creating a new one every time you register/login

export async function getUserAndAccount(OAuthProfile, provider, tokens, logger) {
    try {
        // console.log('provider: ', provider);
        // console.log('profile: ', OAuthProfile);
        const userFromProfile = await provider.profile(OAuthProfile, tokens);
        // console.log('user: ', userFromProfile);
        const user = {
            ...userFromProfile,
            id: crypto.randomUUID(),
            email: userFromProfile.email?.toLowerCase(),
        };
        return {
            user,
            account: {
                ...tokens,
                provider: provider.id,
                type: provider.type,
                providerAccountId: userFromProfile.id ?? crypto.randomUUID(),
            },
        };
    }
    catch (e) {
        // If we didn't get a response either there was a problem with the provider
        // response *or* the user cancelled the action with the provider.
        //
        // Unfortunately, we can't tell which - at least not in a way that works for
        // all providers, so we return an empty object; the user should then be
        // redirected back to the sign up page. We log the error to help developers
        // who might be trying to debug this when configuring a new provider.
        logger.debug("getProfile error details", OAuthProfile);
        logger.error(new OAuthProfileParseError(e, { provider: provider.id }));
    }
}
4rch1m3d35 commented 5 days ago

by function I mean await provider.profile(OAuthProfile, tokens);