试图限制对'登录'页面使用JavaScript和Facebook SDK

Trying to restrict access to 'logged-in' page using JavaScript and Facebook SDK

本文关键字:JavaScript SDK Facebook 登录      更新时间:2023-09-26

编辑了JavaScript文件并添加了路由,以建议可能的资产管道干扰。问题仍未解决

我正在使用Facebook SDK将用户登录到我的Rails应用程序。到目前为止,我已经让应用程序在用户登录时运行,应用程序会将他们重定向到"登录"页面

我遇到的问题是限制任何人在登录前访问"已登录"页面。

checkLoginState();函数的response.status在用户登录时返回'connected',在用户未登录时返回'sunknown'。在我的checkLoginStatus();函数中,只有包含逻辑if (response.status === 'connected')的第一个条件语句在执行,而else && else if (response.status === 'unknown')逻辑被忽略

JavaScript

function statusChangeCallback(response) {
    console.log('statusChangeCallback');
    console.log(response);
    // The response object is returned with a status field that lets the
    // app know the current login status of the person.
    // Full docs on the response object can be found in the documentation
    // for FB.getLoginStatus().
    if (response.status === 'connected') {
      // Logged into your app and Facebook.
      testAPI();
    } else if (response.status === 'not_authorized') {
      // The person is logged into Facebook, but not your app.
      document.getElementById('status').innerHTML = 'Please log ' +
        'into this app.';
    } else {
      // The person is not logged into Facebook, so we're not sure if
      // they are logged into this app or not.
      document.getElementById('status').innerHTML = 'Please log ' +
        'into Facebook.';
    }
  }
  // This function is called when someone finishes with the Login
  // Button.  See the onlogin handler attached to it in the sample
  // code below.
  function checkLoginState() {
    FB.getLoginStatus(function(response) {
      if (response.status === 'connected'){
        window.location.href = '/logged_in'
      } else {
        window.location.href = '/';
      }
    });
  }
  window.fbAsyncInit = function() {
  FB.init({
    appId      : '<MyApp-ID>',
    cookie     : true,  // enable cookies to allow the server to access
                        // the session
    xfbml      : true,  // parse social plugins on this page
    version    : 'v2.2' // use version 2.2
  });
  // Now that we've initialized the JavaScript SDK, we call
  // FB.getLoginStatus().  This function gets the state of the
  // person visiting this page and can return one of three states to
  // the callback you provide.  They can be:
  //
  // 1. Logged into your app ('connected')
  // 2. Logged into Facebook, but not your app ('not_authorized')
  // 3. Not logged into Facebook and can't tell if they are logged into
  //    your app or not.
  //
  // These three cases are handled in the callback function.
  FB.getLoginStatus(function(response) {
    statusChangeCallback(response);
  });
  };
  // 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/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));
  // Here we run a very simple test of the Graph API after login is
  // successful.  See statusChangeCallback() for when this call is made.
  function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log("NAME: " + response.name);
      console.log('Successful login for: ' + response.name);
      document.getElementById('status').innerHTML =
        'Thanks for logging in, ' + response.name + '!';
    });
  }
  function logout(){
    FB.logout(function(response) {
      window.location.href = '/';
    });
  }

HTML索引/登录页面

<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">
</fb:login-button>
<div id="status">
</div>

HTML登录页面

<div onload="checkLoginState();">
  <h1>Logged In</h1>
  <button onclick="logout();">Logout</button>
</div>

路线

Rails.application.routes.draw do
  get '/' => 'pages#signup'
  get 'logged_in' => 'pages#logged_in'
end

我试图做的是将onload='checkLoginState();'添加到div以及logged-in页面内的正文中,checkLoginState();返回"unknown"和指示执行window.location.href = '/';的逻辑

看起来"成功"(检查登录状态并返回"已连接")发生在testAPI()函数中。你试过在那里添加条件吗?

编辑-

您是否尝试在调用statusChangeCallback()函数的函数中添加条件?

FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
        // etc.
    }
});