试图限制对'登录'页面使用JavaScript和Facebook SDK
Trying to restrict access to 'logged-in' page using JavaScript and Facebook SDK
编辑了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.
}
});
相关文章:
- 包括用于facebook评论框的JavaScript SDK
- Google/html5语音识别JavaScript SDK Chrome网络工具包SpeechRecognition
- Soundcloud Javascript SDK 3.0-回调无法读取属性'connectCallback
- FB.login访问令牌facebook javascript SDK
- Loopback AngularJS JavaScript SDK - Gruntfile plugin
- 如果设置了cookie,则Facebook登录Javascript SDK自动登录
- 在SoundCloud中,使用JavaScript SDK流式传输曲目是否会增加该曲目的playback_count
- 如何使用Parse.com JavaScript SDK上传图像
- javascript sdk正在打开多个登录对话框
- chrome中的Facebook JavaScript SDK Connect问题
- 如何在aws S3 javascript SDK中配置interval和max_attempts
- Facebook Javascript SDK:发布到提要时验证访问令牌时出错
- 通过Javascript SDK登录Facebook可以在移动设备和本地主机上使用,但不能在台式机上使用
- 是否可以避免使用SoundManager2和SoundCloud JavaScript SDK将歌曲下载到所提供的位置
- 安全地实现用于Facebook身份验证的Javascript SDK
- Facebook JavaScript SDK 检查用户是否为粉丝(错误:指定的版本无效)
- 组合不同 Javascript SDK 回调的最佳实践
- 使用 Javascript SDK 创建 Parse.File
- Facebook javascript sdk 基本登录或初始化错误
- Facebook Javascript SDK提要对话框显示空白窗口