![](/img/trans.png)
[英]how do I detect if user logged in using my login button or login from facebook?
[英]How do I stop my Facebook login popup from appearing if user is already logged into FB?
我有一個“登錄Facebook”按鈕,生成一個彈出窗口,要求用戶輸入FB憑據。
在用戶已經加入我的應用程序的情況下,離開應用程序然后返回(同時仍然登錄到Facebook),我希望用戶能夠點擊“使用Facebook登錄”而不顯示彈出窗口。
目前,鑒於上面段落中的情況,彈出窗口打開一秒鍾,然后頁面重定向到應用程序的登錄狀態。
我已經在下面實現了以下代碼 - 當談到Javascript時我是一個完整的菜鳥,所以答案可能很明顯,我不知道該怎么辦!
window.fbAsyncInit = function() {
FB.init({
appId : '372191216164729',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
};
(function(d) {
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
$(function() {
$('#fb_connect').click(function(e) {
e.preventDefault();
FB.login(function(response) {
if (response.authResponse) {
// $('#connect').html('Connected! Hitting OmniAuth callback (GET /auth/facebook/callback)...');
//this might not be the best way to do this ...
$('#welcome_form').submit();
// since we have cookies enabled, this request will allow omniauth to parse
// out the auth code from the signed request in the fbsr_XXX cookie
// $.getJSON('/auth/facebook/callback', function(json) {
// $('#connect').html('Connected! Callback complete.');
// $('#results').html(JSON.stringify(json));
// });
}
}, { scope: 'email, read_stream' });
});
});
頁面加載后,然后加載並初始化fb sdk后,使用FB.getLoginStatus方法檢查用戶狀態。 如果用戶已經登錄並授權您的應用程序,則響應應具有訪問令牌,用戶ID等。
例如:
FB.getLoginStatus(function(response) {
if (response.status === "connected") {
// use the response.authResponse
}
else if (response.status === "not_authorized") {
FB.login(function(response) {
...
}, { scope: "email, read_stream" });
}
else {
// user not logged in to facebook
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.