Add API endpoints at /api/auth/link
and /api/auth/unlink
#3936
Replies: 9 comments 4 replies
-
Does this let users link social OAuth accounts that don't return an email address? |
Beta Was this translation helpful? Give feedback.
-
That's a great question! So (unless there is an additional check somewhere I have missed) yes it does allow this, which was also the case with version 1. Like version 1, version 2 currently requires an email address to create a user account, but is limited in that it only stores one email address per user. Once you have a user account, you can link as many accounts as you like (or unlink them all, which is safe because you can still always log in via email). Not allowing people to unlink all accounts unless an email is specified is good example of an edge case that comes up if you don't require an email address. There is probably a good discussion to be had around how multiple email addresses could be tracked per user. e.g. Perhaps it could even use the existing |
Beta Was this translation helpful? Give feedback.
-
Cool, might be a good idea to add to the each OAuth provider's docs which one returns an email address and can be used for both signup and linking and which ones don't return an email address and can only be used for linking. |
Beta Was this translation helpful? Give feedback.
-
Curious what the status is of this issue? Ideally I'd like the following experience:
|
Beta Was this translation helpful? Give feedback.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
For anyone that wants to allow users to link an OAuth account to a user, but not register with one, try this advanced initialization ( import type { NextApiRequest, NextApiResponse } from 'next'
import type { NextAuthOptions } from 'next-auth'
import NextAuth from 'next-auth'
import EmailProvider from 'next-auth/providers/email'
import GoogleProvider from 'next-auth/providers/google'
import { getSession } from 'next-auth/react'
export function getNextAuthOptions(req?: NextApiRequest): NextAuthOptions {
return {
providers: [
EmailProvider({ ... }),
GoogleProvider({ ... }),
],
callbacks: {
async signIn({ user, account }) {
if (account.provider === 'email') {
// Allow login by email if the User already exists
return Boolean(await getUser({ email: user.email }))
}
if (account.provider === 'google') {
// Allow connecting a Google account to an authenticated User
if (await getSession({ req })) {
return true
}
// Allow login by an already connected Google account
if (await getUser({ id: user.id })) {
return true
}
return false
}
return false
},
},
}
}
export default async function auth(req: NextApiRequest, res: NextApiResponse) {
return await NextAuth(req, res, getNextAuthOptions(req))
} The use case for me here was that users can only register through an email invitation from another user, but should be able to connect their Google account once logged in. The key was being able to access the user session from within the |
Beta Was this translation helpful? Give feedback.
-
To clarify – is there no way to link multiple accounts right now? Linking is mentioned a few times in the documentation but I haven't seen it directly addressed. |
Beta Was this translation helpful? Give feedback.
-
I've just spent this evening implementing this "Auto functionality" in a gracefull way when you are already signed into a custom It's working well but just one clarification to what has already been said. I see it mentioned that the email of the OAuth account need to match up with the signed in user to be auto linked but this doesn't seem to be the case. I can link 2 GitHub accounts with different email addresses to the same user record when logged in to it... Not a problem for me and actually an advantage but thought I'd clarify. As per the queries above from others, is this feature to implement API's for Linking / Unlinking still on a roadmap? Do we have any rough timescales? Would love to take this further and have Unlink capability too! (Guess I could now if I manually manipulate the "Account" table and delete records but for it to be usefull I need to be able to store some extra info like what email address is against each Anyways enough natter, thanks for creating an awesome project for all to use!!! 👍👍🥳 |
Beta Was this translation helpful? Give feedback.
-
Although I think the API endpoints would still be a huge help, I did find a bit of a workaround if you're using an adapter:
All that was left was to figure out how the adapter used the I believe most of the adapters have I'm not exactly a security expert, so please let me know if you see issues with my implementation, but I think it looks safe and straightforward. I don't want to be advertising unsafe code for others to copypasta. Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
Add API endpoints at
/api/auth/link
and/api/auth/unlink
for linking and unlinking OAuth accounts. This was a feature of v1 but did not make the cut for v2.0.Note:
The
/api/auth/signin
endpoint currently lets you link accounts securely (i.e. if signed in and then you sign in with a different account).Once a page at
/api/auth/link
is implemented/api/auth/signin
should probably direct users there if the user is signed in. It would effectively be the same as the sign in page, but would only list OAuth providers, and would have 'Unlink' options for providers that are already linked.Beta Was this translation helpful? Give feedback.
All reactions