使用Firebase Auth for Google将匿名用户转换为注册用户

Tom*_*Tom 11 javascript firebase vue.js firebase-authentication

我使用火力地堡验证与VueJS,我需要一个转换的匿名auth用户注册一个与谷歌.

我从一个例子中使用这段代码:

  fromAnonymousToGoogle: function () {
  // Authenticate with the first user then save the currentUser to a local variable
    var previousUser = Firebase.auth().currentUser

  // Authenticate with a second method and get a credential
    var credential = Firebase.auth.GoogleAuthProvider()

    previousUser.link(credential)
    .catch(function (error) {
     // Linking will often fail if the account has already been linked. Handle these cases manually.
      alert(error)
    })

    // OAuth providers authenticate in an asynchronous manner, so you’ll want to perform the link account link in the callback.
    // previousUser = Firebase.auth().currentUser;
    Firebase.auth().signInWithPopup(new Firebase.auth.GoogleAuthProvider())
     .then(function (result) {
       return previousUser.link(result.credential)
     })
     .catch(function (err) {
       // Handle error
       alert(err)
     })
  },
Run Code Online (Sandbox Code Playgroud)

我尝试将帐户关联到Google时收到此错误:

[Vue警告]:"click"的事件处理程序出错:"TypeError:this.ta不是函数"

我的代码中没有一个名为this.ta的函数.如何解决这个错误?

moo*_*ard 5

为了回答您的上一个问题,您遇到此错误的原因是因为您实际上并未获得var credential = Firebase.auth.GoogleAuthProvider()具有提供者ID 的凭据.因此,当您尝试link()使用提供程序ID时,它根本无法正常工作(由于代码的这一部分,我验证了运行相同的错误).

您实际上不想使用Google凭据登录用户,因为这会签署您的匿名用户并使用Google登录.您只想将当前用户与某些Google凭据相关联,您只需使用该linkWithPopup方法即可完成(我重命名变量以使其更有意义).

fromAnonymousToGoogle: function () {
            // Authenticate with the first user then save the currentUser to a local variable
            var anonUser = Firebase.auth().currentUser

            // Authenticate with a second method and get a credential
            var provider = new Firebase.auth.GoogleAuthProvider();

            anonUser.linkWithPopup(provider).then(function(result) {
                googleToken = result.credential;
                console.log(googleToken);
            }).catch(function(error) {
                console.error("Google sign in failed", error);
            });
    },
Run Code Online (Sandbox Code Playgroud)

在我自己测试了这个之后,这似乎是使用弹出窗口链接它们的方法,与您最初提供的代码最匹配.