Linking Multiple Authentication providers to a single user #5827
|
I am trying to enable login with multiple providers. Is there a way I can link multiple auth credentials to a single user account? E.g. User creates account with email and password, but then they want to connect their Twitter account as well so that they can use either email or twitter to login. |
Replies: 5 comments 17 replies
|
Yes, it is supposed to just work as long as email is the same... |
|
Old thread, but is there a way to link multiple providers to a single user if the emails aren't the same? I.e. a user creates an account with email and password then, while they are authenticated, connects their Apple account which is tied to another email. If this is not possible to do in an automated way, how could it be done manually? |
|
Damn, I was really hoping one could do something like This seems to be a very common use case to do... |
|
In my usecase, where what I wanted was to use both phone and email OTP, this was possible simply by authenticating once with email/phone OTP then updating the user object for the other authentication method:
|
|
If you don't want to rely on having the same email to get the accounts linked you can use the "Identity Linking" feature. export async function signInWithFacebook(next?: string) {
const supabase = await createClient();
const redirectTo = `${process.env.NEXT_PUBLIC_URL}/auth/callback?next=${next}`;
const options = {
scopes: "public_profile pages_show_list pages_manage_posts",
redirectTo,
};
const session = await supabase.auth.getSession();
let result;
if (
session.data.session &&
!session.data.session.user.app_metadata.providers.includes("facebook")
) {
result = await supabase.auth.linkIdentity({
provider: "facebook",
options,
});
} else {
result = await supabase.auth.signInWithOAuth({
provider: "facebook",
options,
});
}
const { data, error } = result;
if (error) {
return { error: error.message };
}
return redirect(data.url);
} |
Yes, it is supposed to just work as long as email is the same...
#3098