0

facebookアカウントと既存のfirebase電子メール/パスワードアカウントをリンクする方法を理解するのが難しいです。私はそれをリンクすることができますが、どのようにFB APIの呼び出しを行う手掛かりがありません。または私はそれをリンクさせることはできませんし、FBのAPI自体を使用してログインします。私は本当にアカウントを既存の電子メール/パスワードアカウントにリンクさせたいと思っています。ファイアベース付きFB API

var facebook = document.getElementById('facebook'); 
 
//var twitter = document.getElementById('twitter'); 
 
//var google = document.getElementById('google'); 
 
var fbunlink = document.getElementById('fbunlink'); 
 
//var twitunlink = document.getElementById('twitunlink'); 
 
//var googleunlink = document.getElementById('googleunlink'); 
 
var fbList = document.getElementById('fbList'); 
 
var provider = ""; 
 
var fbToken = ""; 
 
var fbId = []; 
 
var fbName = []; 
 
var fbLength = 0; 
 
var fbAccessToken =""; 
 
var fbUID = ""; 
 
var auth = firebase.auth(); 
 
var user = firebase.auth().currentUser; 
 

 

 
    window.fbAsyncInit = function() { 
 
    FB.init({ 
 
     appId  : '1899992333573742', 
 
     xfbml  : true, 
 
     version : 'v2.10' 
 
     }); 
 
     FB.AppEvents.logPageView(); 
 
     
 
     FB.getLoginStatus(function(response) { 
 
      if (response.status === 'connected') { 
 
       
 
      var uid = response.authResponse.userID; 
 
      var accessToken = response.authResponse.accessToken; 
 
      
 
      } else if (response.status === 'not_authorized') { 
 
      } else { 
 
     } 
 
    }); 
 
    }; 
 
     
 
    (function(d, s, id){ 
 
    var js, fjs = d.getElementsByTagName(s)[0]; 
 
    if (d.getElementById(id)) {return;} 
 
    js = d.createElement(s); js.id = id; 
 
    js.src = "//connect.facebook.net/en_US/sdk.js"; 
 
    fjs.parentNode.insertBefore(js, fjs); 
 
    }(document, 'script', 'facebook-jssdk')); 
 
     
 
     
 
    facebook.addEventListener('click', e => { 
 
     provider = new firebase.auth.FacebookAuthProvider(); 
 
     provider.addScope('email'); 
 
     provider.addScope('user_photos'); 
 
     provider.addScope('publish_pages'); 
 
     provider.addScope('business_management'); 
 
     provider.addScope('read_insights'); 
 
     provider.addScope('manage_pages'); 
 
     provider.addScope('public_profile'); 
 
     
 
     auth.currentUser.linkWithRedirect(provider); 
 
     
 
     firebase.auth().getRedirectResult().then(function(result) { 
 
      if (result.credential) { 
 
      // This gives you a Facebook Access Token. You can use it to access the Facebook API. 
 
      var token = result.credential.accessToken; 
 
      var credential = result.credential; 
 
      var user = result.user; 
 
      // ... 
 
      } 
 
      
 
      document.getElementById('fbunlink').style.visibility = 'visible'; 
 
      document.getElementById('fbList').style.visibility = 'visible'; 
 
      document.getElementById('facebook').style.visibility = 'hidden'; 
 
      document.getElementById('status').innerHTML = 'You are logged in.'; 
 
      
 
      getInfo(); 
 
     }).catch(function(error) { 
 
      // Handle Errors here. 
 
      var errorCode = error.code; 
 
      var errorMessage = error.message; 
 
      // The email of the user's account used. 
 
      var email = error.email; 
 
      // The firebase.auth.AuthCredential type that was used. 
 
      var credential = error.credential; 
 
      // ... 
 
     }); 
 
    }); 
 
    /* 
 
    FB.login(function(response) { 
 
     if (response.status === 'connected') { 
 
     document.getElementById('fbunlink').style.visibility = 'visible'; 
 
     document.getElementById('fbList').style.visibility = 'visible'; 
 
     document.getElementById('facebook').style.visibility = 'hidden'; 
 
     document.getElementById('status').innerHTML = 'You are logged in.'; 
 
     var uid = response.authResponse.userID; 
 
     var accessToken = response.authResponse.accessToken; 
 
     fbAccessToken = uid; 
 
     fbUID = accessToken; 
 
     getInfo(); 
 
     linkInfo(); 
 
     } else if (response.status === 'not_authorized') { 
 
     document.getElementById('fbunlink').style.visibility = 'hidden'; 
 
     document.getElementById('facebook').style.visibility = 'visible'; 
 
     document.getElementById('fbList').style.visibility = 'hidden'; 
 
     document.getElementById('status').innerHTML = 'You have logged out of Facebook.'; 
 
     } else { 
 
     document.getElementById('status').innerHTML = 'You are not logged in.'; 
 
     document.getElementById('facebook').style.visibility = 'visible'; 
 
     document.getElementById('fbunlink').style.visibility = 'hidden'; 
 
     document.getElementById('fbList').style.visibility = 'hidden'; 
 
      } 
 
     }, {scope: 'email, user_photos, publish_pages, business_management, read_insights, manage_pages, public_profile'}); 
 
    });*/ 
 

 
    fbunlink.addEventListener('click', e => { 
 
     var user = firebase.auth().currentUser; 
 
     user.unlink("facebook.com"); 
 
     document.getElementById('picture').innerHTML = ""; 
 
     document.getElementById('status').innerHTML = 'You are not logged in.'; 
 
     document.getElementById('facebook').style.visibility = 'visible'; 
 
     document.getElementById('fbunlink').style.visibility = 'hidden'; 
 
     document.getElementById('fbList').style.visibility = 'hidden'; 
 
    }); 
 
     
 
    function getInfo(){ 
 
     FB.api('/me/accounts?limit=200', 'GET', {fields: 'business, name, data, id, picture', access_token : fbToken}, function(response){ 
 
      document.getElementById('status').innerHTML = ""; 
 
      console.log(response.data); 
 
      fbLength = response.data.length; 
 
      for(var i = 0; i < fbLength; i++){ 
 
      fbId.push(response.data[i].id); 
 
      fbName.push(response.data[i].name); 
 
      document.getElementById('status').innerHTML += "<img src='https://graph.facebook.com/" + fbId[i] + "/picture?type=square'>" + " " + "<input type=" + "'checkbox'" + " id=" + "'" + fbId[i] + "'" + " value=" + "'" + fbId[i] + "'" + ">" + fbName[i] + "<br>"; 
 
      } 
 
     }); 
 
    } 
 

 
    /*function linkInfo(){ 
 
     var credential = firebase.auth.FacebookAuthProvider.credential(fbUID, fbAccessToken); 
 
     auth.currentUser.link(credential).then(function(user) { 
 
      console.log("Account linking success", user); 
 
     }, function(error) { 
 
      console.log("Account linking error", error); 
 
     }); 
 
    }*/ 
 

 
    fbList.addEventListener('click', e => { 
 
      document.getElementById('picture').innerHTML = ""; 
 
      for(var i = 0; i < fbLength; i++){ 
 
      var temp = document.getElementById(fbId[i]); 
 
      if(temp.checked){ 
 
      document.getElementById('picture').innerHTML += "<img src='https://graph.facebook.com/" + fbId[i] + "/picture?type=square'>" + " " + fbName[i] + "<br>"; 
 
       } 
 
      } 
 
     });

答えて

1

だから、あなたがFacebookのAPIへの呼び出しを行うので、Firebase認証はOAuthのトーク​​ンを管理していないとして、FacebookのAPIを引き続き使用する必要があり、あなたがそれを行うためにFBのAPIを必要とするようです君は。 FacebookアカウントをEメール/パスワードアカウントにリンクするには、Facebook APIを使用してFBユーザーのOAuthアクセストークンを取得する必要があります。 これをcurrentUserにリンクします。 firebase.auth().currentUser.linkWithCredential(firebase.auth.FacebookAuthProvider.credential(fbAccessToken)) これは、Facebookユーザーを電子メール/パスワードcurrentUserにリンクします。

関連する問題