使用Firebase转换匿名用户:无法读取未定义的属性“getAuthResponse”

时间:2018-05-14 08:45:17

标签: javascript firebase vue.js firebase-authentication

我正在使用VueJS和Firebase Auth(NPM - 版本5.6.0)。我需要将匿名用户转换为Google作为提供商的注册用户。我尝试了recently updated sample from the Firebase docs

        anonymousToGoogle: function () {
      var googleUser
      var provider
      var credential = firebase.auth.GoogleAuthProvider.credential(googleUser.getAuthResponse().id_token)
      firebase.auth.currentUser.linkWithRedirect(provider)
      firebase.auth().currentUser.linkAndRetrieveDataWithCredential(credential).then(function (usercred) {
        var user = usercred.user
        console.log('Anonymous account successfully upgraded', user)
      }, function (error) {
        console.log('Error upgrading anonymous account', error)
      })
    }
  }

Button(使用VuetifyJS框架):

<v-btn block @click="anonymousToGoogle()">CONVERT - Login with Google</v-btn>

单击按钮后,控制台显示此错误:

  

[Vue警告]:“click”的事件处理程序出错:“TypeError:不能   读取属性'getAuthResponse'未定义“

1 个答案:

答案 0 :(得分:1)

您只需在匿名用户上拨打linkWithRedirect即可。

// Assuming you previously signed in anonymously.
firebase.auth().signInAnonymously().then(function(result) {
  var provider = new firebase.auth.GoogleAuthProvider();
  // Page will redirect to google and then back. You can get back
  // result via getRedirectResult().
  return firebase.auth().currentUser.linkWithRedirect(provider);
}).catch(function(error) {
  // Error occurred.
});