[英]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!谢谢你的帮助!
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.