简体   繁体   English

将Facebook链接到Firebase Anonymous Auth,无需调用Facebook API

[英]Link Facebook to Firebase Anonymous Auth without calling Facebook API

I am creating anonymous sessions in my Firebase application to save user data before they create their accounts. 我在Firebase应用程序中创建匿名会话,以便在创建帐户之前保存用户数据。 I saw that Firebase allows linking a Facebook login to an anonymous account which sounds really neat, but a caveat of this process seems to be that I have to grab the Facebook token myself, outside the warmth and comfort of the awesome Firebase API, which seems strangely un-developed given how much of the login flow Firebase seems to do on behalf of apps. 我看到Firebase允许将Facebook登录链接到一个听起来非常整洁的匿名帐户 ,但是这个过程的一个警告似乎是我必须自己抓住Facebook令牌,除了令人敬畏的Firebase API的温暖和舒适之外考虑到Firebase似乎代表应用程序执行了多少登录流程,这是一种奇怪的未开发。

A code sample of how to connect an anonymous account from their account linking docs : 如何从其帐户链接文档中连接匿名帐户的代码示例:

var credential = firebase.auth.FacebookAuthProvider.credential(
    response.authResponse.accessToken);

Naturally, I want to use Firebase's way of getting a token 当然,我想使用Firebase获取令牌的方式

var provider = new firebase.auth.FacebookAuthProvider();

firebase.auth().signInWithPopup(provider).then(function(result) {
     // result.token or whatever would appear here
});

But if I were to run that, I would lose my anonymous session (and my anonymous user ID, which we want the new Facebook login to use). 但是,如果我要运行它,我将失去我的匿名会话(以及我的匿名用户ID,我们希望新的Facebook登录使用)。

Is there anyway to get a Facebook Token out of Firebase's auth mechanism without logging the user in and losing the anonymous session that I'm trying to convert into a Facebook Login-able account? 反正有没有使用Firebase的auth机制获取Facebook令牌而不记录用户并丢失我想要转换为Facebook登录帐户的匿名会话? (The goal is to not have to call the Facebook API myself, especially as I'll be adding Google here as well) (目标是不必自己调用Facebook API,特别是因为我将在这里添加Google)

I think you're looking to #linkWithPopup or #linkWithRedirect : 我想你正在寻找#linkWithPopup#linkWithRedirect

var provider = new firebase.auth.FacebookAuthProvider();

user.linkWithPopup(provider).then(function(result) {
   console.log('Party 🎉');
});

If for some reason that doesn't cut it, you could always opt to do yourself: 如果由于某种原因不能削减它,你可以随时选择自己做:

  • Sign in the user anonymously, and store the user somewhere 匿名登录用户,并将用户存储在某处
  • Sign in the user with the provider and store the token somewhere 使用提供程序登录用户并将令牌存储在某处
  • Delete the provider user and then link the account with the token 删除提供者用户,然后将该帐户与令牌链接

Quick and dirty example: 快速而肮脏的例子:

var googleToken;
var anonUser;

firebase.auth().signInAnonymously().then(function(user) {
  anonUser = user;
}).catch(function(error) {
  console.error("Anon sign in failed", error);
});

function signInWithGoogle() {
  var provider = new firebase.auth.GoogleAuthProvider();
  firebase.auth().signInWithPopup(provider).then(function(result) {
    googleToken = result.credential.idToken;
  }).catch(function(error) {
    console.error("Google sign in failed", error);
  })
}

function deleteAndLink() {
  firebase.auth().currentUser.delete().then(function() {
    var credential = 
      firebase.auth.GoogleAuthProvider.credential(googleToken);
    anonUser.link(googleCredential);
  }).then(function() {
    console.log("Link succeeded");
  }).catch(function(error) {
    console.error("Something went wrong", error);
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM