簡體   English   中英

將Facebook鏈接到Firebase Anonymous Auth,無需調用Facebook API

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

我在Firebase應用程序中創建匿名會話,以便在創建帳戶之前保存用戶數據。 我看到Firebase允許將Facebook登錄鏈接到一個聽起來非常整潔的匿名帳戶 ,但是這個過程的一個警告似乎是我必須自己抓住Facebook令牌,除了令人敬畏的Firebase API的溫暖和舒適之外考慮到Firebase似乎代表應用程序執行了多少登錄流程,這是一種奇怪的未開發。

如何從其帳戶鏈接文檔中連接匿名帳戶的代碼示例:

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

當然,我想使用Firebase獲取令牌的方式

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

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

但是,如果我要運行它,我將失去我的匿名會話(以及我的匿名用戶ID,我們希望新的Facebook登錄使用)。

反正有沒有使用Firebase的auth機制獲取Facebook令牌而不記錄用戶並丟失我想要轉換為Facebook登錄帳戶的匿名會話? (目標是不必自己調用Facebook API,特別是因為我將在這里添加Google)

我想你正在尋找#linkWithPopup#linkWithRedirect

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

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

如果由於某種原因不能削減它,你可以隨時選擇自己做:

  • 匿名登錄用戶,並將用戶存儲在某處
  • 使用提供程序登錄用戶並將令牌存儲在某處
  • 刪除提供者用戶,然后將該帳戶與令牌鏈接

快速而骯臟的例子:

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