![](/img/trans.png)
[英]Accessing logged in user email using facebook javascript sdk
[英]Trying to restrict access to 'logged-in' page using JavaScript and Facebook SDK
编辑了JavaScript文件并添加了路由,以建议可能的资产管道干扰。 问题仍然没有解决。
我正在使用Facebook SDK将用户登录到我的Rails应用程序。 截至目前,我已使该应用程序在用户登录时起作用,该应用程序将其重定向到“已登录”页面
我遇到的问题是限制任何人在登录前访问“已登录”页面。
checkLoginState();
的response.status
checkLoginState();
在用户登录后,该函数返回“已连接”,而在用户未登录时,该函数返回“未知”。在我的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以及使用checkLoginState();
logged-in
页面内的主体checkLoginState();
返回'unknown'并指示window.location.href = '/';
被执行。
看起来在testAPI()函数中发生了“成功”(已检查登录状态并返回“已连接”)。 您是否尝试过在此添加条件?
编辑-
您是否尝试过在调用statusChangeCallback()函数的函数中添加条件?
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// etc.
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.