nextauthjs / next-auth

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

Signin in with a "used" account should call linkAccount (and let us choose what to do) instead of entirely redirecting to an error #9338

Closed Khaaz closed 9 months ago

Khaaz commented 9 months ago

Adapter type

Custom adapter

Environment

System: OS: Windows 10 10.0.19045 CPU: (12) x64 AMD Ryzen 5 5600X 6-Core Processor Memory: 6.29 GB / 15.95 GB Binaries: Node: 20.9.0 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.11 - ~\AppData\Roaming\npm\yarn.CMD npm: 10.1.0 - C:\Program Files\nodejs\npm.CMD Browsers: Edge: Spartan (44.19041.3636.0), Chromium (119.0.2151.97)
Internet Explorer: 11.0.19041.3636 npmPackages: next: ^14.0.3 => 14.0.3 next-auth: ^4.24.5 => 4.23.1 react: ^18.2.0 => 18.2.0

Reproduction URL

https://owle.bot

Describe the issue

I use next auth to handle signin and linking account with user. I want to be able to link several account to the same user. It works fine when I login with one account, link another account etc. The issue occurs when I try to link an account that I already signed with before. Right now there are no way to override the current process see the code here: https://github.com/nextauthjs/next-auth/blob/b5de00016f37047b3b84700757556f71c982f76d/packages/core/src/lib/actions/callback/handle-login.ts#L139

Here the code for my custom adapter:

export const Adapter = () => {
    return {
        async createUser(user) {
            console.log("+ CREATE USER");
            const { data: createdUser } = await BackRequester.post(ENDPOINTS.ACCOUNT.CREATE_USER(), {}, { cache: "no-store" } );
            return {
                id: createdUser.id,
                name: user.name,
                image: user.image,
                type: user.provider,
                accountId: user.providerAccountId,
            };
        },

        async getUser(id) {
            console.log("+ GET USER");

            const user = await BackRequester.get(ENDPOINTS.IDENTIFICATION.GET_USER_BY_ID(id), {}, { cache: "no-store" } );
            if (!user.ok || !user.data) {
                return null;
            }
            const providerAccount = user.data.accounts.find(a => a.id === user.data.defaultAccountId);
            return {
                id: user.data.id,
                name: providerAccount.pseudo,
                image: providerAccount.image,
                accountId: providerAccount.id,
                type: providerAccount.type,
            };
        },

        async getUserByEmail(email) {
            console.log("+ GET USER BY MAIL", email);
            return null;
        },

        async getUserByAccount( { providerAccountId } ) {
            console.log("+ GET USER BY ACCOUNT");

            const user = await BackRequester.get(ENDPOINTS.IDENTIFICATION.GET_USER_BY_ACCOUNT(providerAccountId), {}, { cache: "no-store" } );
            if (!user.ok || !user.data) {
                return null;
            }
            const providerAccount = user.data.accounts.find(a => a.id === providerAccountId);
            return {
                id: user.data.id,
                name: providerAccount.pseudo,
                image: providerAccount.image,
                accountId: providerAccountId,
                type: providerAccount.type,
            };
        },

        async updateUser(user) {
            console.log("+ UPDATE USER", user);
            return;
        },

        async linkAccount(account) {
            console.log("+ LINK ACCOUNT");

            await BackRequester.post(ENDPOINTS.ACCOUNT.LINK_ACCOUNT(account.userId), {
                id: account.providerAccountId,
                type: account.provider,
                pseudo: account.user.name,
                image: account.user.image,
            }, {
                session: { user: { id: account.userId } },
                cache: "no-store",
            } );

            return {
                id: account.userId,
                name: account.user.name,
                image: account.user.image,
                accountId: account.providerAccountId,
                type: account.provider,
            };
        },
    };
};

How to reproduce

To confirm your identity, sign in with the same account you used originally.

Expected behavior

The expected behaviour is to be able to either:

We should be able to handle ourself this case, and decide whether we want to override the link process or not.

In my case I want to be able to link the new account to the user that requester it, no matter if it already was linked to a user before.

This is a huge blocking issue for me right now. Could that be implemented or extended easily on my side? Or are there any workaround allowing me to achieve a similar result?

github-actions[bot] commented 9 months ago

We could not detect a valid reproduction link. Make sure to follow the bug report template carefully.

Why was this issue closed?

To be able to investigate, we need access to a reproduction to identify what triggered the issue. We need a link to a public GitHub repository. Example: (NextAuth.js example repository).

The bug template that you filled out has a section called "Reproduction URL", which is where you should provide the link to the reproduction.

What should I do?

Depending on the reason the issue was closed, you can do the following:

In general, assume that we should not go through a lengthy onboarding process at your company code only to be able to verify an issue.

My repository is private and cannot make it public

In most cases, a private repo will not be a sufficient minimal reproduction, as this codebase might contain a lot of unrelated parts that would make our investigation take longer. Please do not make it public. Instead, create a new repository using the templates above, adding the relevant code to reproduce the issue. Common things to look out for:

I did not open this issue, but it is relevant to me, what can I do to help?

Anyone experiencing the same issue is welcome to provide a minimal reproduction following the above steps by opening a new issue.

I think my reproduction is good enough, why aren't you looking into it quickly?

We look into every issue and monitor open issues for new comments.

However, sometimes we might miss a few due to the popularity/high traffic of the repository. We apologize, and kindly ask you to refrain from tagging core maintainers, as that will usually not result in increased priority.

Upvoting issues to show your interest will help us prioritize and address them as quickly as possible. That said, every issue is important to us, and if an issue gets closed by accident, we encourage you to open a new one linking to the old issue and we will look into it.

Useful Resources