简体   繁体   English

firebase auth中如何切换邮箱链接和密码登录方式?

[英]How to switch between emailLink and password sign-in method in firebase auth?

There are two methods a user can sign in via email: "emailLink" and "password" (see https://firebase.google.com/docs/auth/web/email-link-auth#differentiating_emailpassword_from_email_link )用户可以通过 email 登录的方法有两种:“emailLink”和“password”(参见https://firebase.google.com/docs/auth/web/email-link-auth#differentiating_emailpassword_from_email_link

I want to give my users the option to switch between these two methods.我想让我的用户可以选择在这两种方法之间切换。 However, I did not find any documentation to how I could unlink an email sign-in method (eg password).但是,我没有找到任何有关如何取消链接 email 登录方法(例如密码)的文档。

As far as I understood, you can only unlink a sign-in method when the user has multiple provider IDs, but email-link and password share the same providerId (strangely emailLink providerID is called "password").据我了解,您只能在用户拥有多个提供商 ID 时取消链接登录方法,但电子邮件链接和密码共享相同的 providerId(奇怪的是 emailLink providerID 称为“密码”)。

Thanks for your help!谢谢你的帮助!

EDIT编辑

When switching from emailLink to password, a major flaw with the solution below is that after the switch the email won't be verified anymore.当从 emailLink 切换到 password 时,以下解决方案的一个主要缺陷是切换后 email 将不再被验证。 So in this case, it is better to call updatePassword .所以在这种情况下,最好调用updatePassword This will result the user having two email sign ins, "password" and "emailLink" (as returned by fetchSignInMethodsForEmail ).这将导致用户有两个 email 登录,“password”和“emailLink”(由fetchSignInMethodsForEmail返回)。 When the user switches back to "emailLink", you can use the solution below and the result of fetchSignInMethodsForEmail will only include "emailLink".当用户切换回“emailLink”时,您可以使用下面的解决方案, fetchSignInMethodsForEmail的结果将只包含“emailLink”。


So I found the solution: If you switch from emailLink to password, you simply use the following:所以我找到了解决方案:如果您从 emailLink 切换到 password,您只需使用以下命令:


// 1) get credential from use input
const credential = EmailAuthProvider.credential(email, newPassword)
// 2) unlink email link authentication
// https://firebase.google.com/docs/auth/web/account-linking#unlink-an-auth-provider-from-a-user-account
const newUser = await unlink(currentUser, "password")
// 3) link with new credential
const userCredential = await linkWithCredential(newUser, credential)

Now, if we want to switch back from password to emailLink, we first have to send the client an email containing the sign in link.现在,如果我们想从密码切换回电子邮件链接,我们首先必须向客户端发送一个包含登录链接的 email。 Once the user clicks on the sign in link, we again create the credential and proceed in the same manner.一旦用户单击登录链接,我们将再次创建凭据并以相同的方式继续。

Note that if there goes something wrong between step 2 and 3, the user is pretty much screwed and we have to take special care of that scenario.请注意,如果第 2 步和第 3 步之间出现问题,用户就完蛋了,我们必须特别注意这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase 使用email登录后google登录报错:密码无效或用户没有密码 - Firebase Sign-in with email after google sign-in has error : The password is invalid or the user does not have a password Firebase auth - 电话登录方法未呈现 IOS 的正确短信模板 - Firebase auth - Phone sign-in method not rendering correct SMS messages template for IOS 如何在没有 aws-auth configmap 的情况下登录 EKS 集群? - How to sign-in to EKS cluster without aws-auth configmap? Google firebase 首次登录时显示没有 email 选项 - Google firebase Sign-in show no email option on first sign-in Firebase 自助登录问题 - Firebase self sign-in issue 如何使用 python 登录 Firebase Auth - How to sign in with Firebase Auth using python Firebase Auth 登录在将 flutter 项目发布到 Play 商店后不起作用 - Firebase Auth Sign-in does not work after releasing flutter project to Play Store Firebase Auth:如何使用id token登录? - Firebase Auth: How to sign in using id token? 谷歌使用 flutter 和 firebase auth 登录 - google sign in with flutter and firebase auth 如何防止 Firebase 身份验证在首次登录时隐式创建新用户? - How to prevent Firebase Authentication creating a new user implicitly on first sign-in?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM