0

質問は 、FB認証への適切なアプローチに似ています

ただし、クライアント側のコードのみが必要です。

条件 1. ユーザーが FB にログインしていない可能性があります。この場合、ユーザーは FB と実質的に私のアプリにログインする必要があります。FBログインボタンを表示します。2. ユーザーはすでに FB にログインしています。アプリにログインしていません。FBログインボタンを表示します。3. ユーザーは Fb と私のアプリにログインしています。アプリを表示します。FBログインボタンなし

問題 : ユーザーが既に FB にログインしている場合、FB.login はエラーになります。

コミュニティの助けが必要です。私はそれが単純な論理的な問題であることを知っていますが、私は今何時間も解決策を回っています.

私のコード

    <div id="auth-loggedout" onclick="fbLogin()">
    <span class="buttonText">Login with Facebook</span>
    </div>


<script>

window.fbAsyncInit = function() {
// init the FB JS SDK
    FB.init({
        appId : 'APP ID',   // App ID from the app dashboar
        channelUrl  : '/channel.html',  // Channel file for x-domain comms
            status : true,    // Check Facebook Login status
         xfbml : true    // Look for social plugins on the page
    });

    // this check FB login status on initialization
           fbLogin();
};
        // listen for and handle auth.statusChange events
  var login="false";
  function fbLogin(){
      FB.getLoginStatus(function(response) {
          if (response.status === 'connected') {
              document.getElementById('auth-loggedout').style.display = 'none';
              document.getElementById('appcontainer').style.display = 'block';
              login=true;
              getDataFromFBAndgetSessionToken();<= This also needs FB object
          } else if (response.status === 'not_authorized') {
              // the user is logged in to Facebook,
              // but has not authenticated your app
              document.getElementById('auth-loggedout').style.display = 'block';
              document.getElementById('appcontainer').style.display = 'none';
          } else {
              // the user isn't logged in to Facebook.
              document.getElementById('auth-loggedout').style.display = 'block';
              document.getElementById('appcontainer').style.display = 'none';
          }
      });

      if (login === 'false')
      {
          FB.login();
      }
    };

   function getDataFromFBAndgetSessionToken() {
     FB.api('/me?fields=id,name,first_name,last_name,username', function (response) {

        varAccessToken = FB.getAuthResponse()['accessToken'];

       userFirstName = response.first_name;

       var User = new classUser();
       User.set_access_token(varAccessToken);
       User.set_fb_user_id(response.id);
       User.set_first_name(userFirstName);
       User.set_last_name(response.last_name);
      User.set_user_name(response.username);

    });
 }

 // Load the SDK asynchronously
 (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/all.js";
  fjs.parentNode.insertBefore(js, fjs);
 }(document, 'script', 'facebook-jssdk'));

  </script>

<script>
4

3 に答える 3