one user, multiple account providers #1702
Replies: 11 comments 24 replies
-
#1002 (comment) might be relevant. I will try to discuss this with @iaincollins when we get the time! Thanks for your suggestion. |
Beta Was this translation helpful? Give feedback.
-
Is there any news about this @balazsorban44 ? It's clear, from this and other issues/discussions/PR, that having the ability to link accounts is a demanded feature that got no answer since more than one year. Is there any plan about this that you can share? |
Beta Was this translation helpful? Give feedback.
-
Would also be interested as I'm using Two login methods: One Social, and one with Crypto wallet. - and logging in to one/other seems to wipe session between the two logins. |
Beta Was this translation helpful? Give feedback.
-
It would be awesome to have this feature added to next-auth! |
Beta Was this translation helpful? Give feedback.
-
Any news about this potential feature? Or has someone found a workaround? |
Beta Was this translation helpful? Give feedback.
-
Oh yes please this would be awesome for users |
Beta Was this translation helpful? Give feedback.
-
Just in case this helps anyone, the |
Beta Was this translation helpful? Give feedback.
-
@tanshunyuan I ran into this issue recently which led me here, and I managed to implement a similar solution for an app I am working on where I needed a user to be able to login with a Web3 wallet, and then subsequently link other accounts such as Spotify to their profile. The solution is rather long, but the key insights are:
Here are some caveats with the process above for my situation:
Another piece of the solution is to make sure to:
Lastly, I want to question one assumption in the logic of this process:
If anyone would like to see a generalized POC/example application for this with some of the functionality mentioned above, reach out and I could build one/pair in building one with someone else. But otherwise, it would also be lovely to see some built-in |
Beta Was this translation helpful? Give feedback.
-
Just faced the same issue. Would be great if this feature gets added. Or at least an option to return to the login screen with a callback, so we can customize the error message. |
Beta Was this translation helpful? Give feedback.
-
This is currently possible by using the JWT callback and parsing the current session cookie. Here's how I do it in an app which allows login via credentials and then link other social accounts: async jwt({ token, user, trigger, account, profile }) {
if (trigger !== 'signIn' || !user) {
return token
}
if (account?.provider === 'credentials') {
// logging in with credentials
return token
}
const provider = account?.provider
if (!provider) {
return token
}
const sessionCookie = cookies().get("authjs.session-token")?.value
if (!sessionCookie) {
console.warn('No session cookie found')
return token
}
const secret = process.env.AUTH_SECRET
// the "verify" function will decrypt/verify the session cookie
const currentJwt = await verify(secret, sessionCookie)
const linkedAccounts = (currentJwt.linkedAccounts ?? {}) as Record<string, string>
switch (provider) {
case 'github':
linkedAccounts[provider] = profile?.login as string
break;
case 'twitter':
linkedAccounts[provider] = (profile?.data as any)?.username as string
break;
default:
console.warn('Unknown provider', provider, profile)
break;
}
return {
...currentJwt,
linkedAccounts
}
} I've put a proposal on how this can be simplified with a small change to jwt callback API: #10785 |
Beta Was this translation helpful? Give feedback.
-
You can link as many user accounts together as you like using multiple sessions. This example links two together https://next-auth-nextjs-git-multi-sessions-demo-gilesbs-projects.vercel.app/ however the next-auth package doesn’t support this client side due to the __NEXTAUTH global variable. Imo this should be removed and the react components changed so they support multiple sessions. (That’s what I did for this example) I’m not sure what the argument is for the global __NEXTAUTH variable. I’m just using the password provider from the example app in this example but it works fine with Facebook, google etc. so you can link Facebook accounts together or Facebook with google etc. all you need is multiple session cookies and you have multiple simultaneous sign ins. |
Beta Was this translation helpful? Give feedback.
-
Hi guys, do you think it is possible to mimic OAuth account linking with nextauth?
Beta Was this translation helpful? Give feedback.
All reactions