首页 > 解决方案 > Firebase 身份验证 - 链接其他身份验证提供程序

问题描述

我正在使用 web 中的 firebase 身份验证并遇到问题。我使用谷歌登录了一个用户,现在我还希望用户创建一个密码,以便他可以使用电子邮件和密码登录。所以基本上我想把他们两个联系起来。

我尝试了 firebase.auth.createUserWithEmailAndPassword(userEmail, newpassword) 但它抛出一个错误“email-already-in-use”这是正确的,因为我先用谷歌登录。

有什么办法可以链接这些吗?

标签: javascriptfirebasefirebase-authentication

解决方案


这是一个如何将电子邮件/密码凭据链接到现有用户的示例(请注意,您也可以只是 user updatePassword

// Email/Password credential. Similar logic can be used for linking
// other credentials.
var cred = firebase.auth.EmailAuthProvider.credential(
    firebase.auth().currentUser.email, 'password');
firebase.auth().currentUser.linkWithCredential(cred)
  .then(function() {
    // Credential successfully linked.
  })
  .catch(function(error) {
    // Some error occurred.
  });

推荐阅读