开发者问题收集

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

2018-05-14
827

我正在使用 VueJS 和 Firebase Auth(NPM - 版本 5.6.0)。我需要将匿名用户转换为以 Google 为提供商的注册用户。我尝试了 Firebase 文档中最近更新的示例

        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)
      })
    }
  }

按钮(使用 VuetifyJS 框架):

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

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

[Vue warn]: Error in event handler for "click": "TypeError: Cannot read property 'getAuthResponse' of undefined"

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.
});
bojeil
2018-05-14