gpt4 book ai didi

javascript - Facebook 登录按钮 - 如何在尝试登录之前检查我们是否已登录?

转载 作者:行者123 更新时间:2023-11-28 17:50:43 25 4
gpt4 key购买 nike

我正在尝试使用 facebook javascript SDK 让我的应用程序连接到用户的 Facebook 帐户。

在运行连接代码之前,有没有一种简单的方法可以检查我的应用程序是否已连接到 Facebook?它必须重新加载页面,以便用户实际上可以看到他们是通过 facebook 登录的,但如果我执行 location.reload();最后它将进入无限循环,因为此代码位于 header 中并且它将再次运行。 header 位于每个页面上,因此重定向到其他位置不会有帮助。

使 Facebook 登录按钮正常工作的 js 代码,主要来自 Facebook 的分步指南:

<script>
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
if (response.status === 'connected') {
testAPI();
} else {
document.getElementById('status').innerHTML = 'Please log ' + 'into this app.';
}
}

window.fbAsyncInit = function () {
FB.init({
appId: '(my app id)',
cookie: true,
xfbml: true,
version: 'v2.8'
});

FB.getLoginStatus(function (response) {
statusChangeCallback(response);
});

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

function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function (response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + '!';
});
get_fb_info();
}

function get_fb_info() {
FB.api('/me', 'GET', {fields: 'first_name,last_name,name,email,id,picture'}, function (response) { //add email,
console.log("FB NAME: " + response.name);
console.log("FB EMAIL: " + response.email);

data_string = 'fb_id=' + response.id + '&f_name=' + response.first_name + '&l_name=' + response.last_name + '&name=' + response.name + '&email=' + response.email + '&picture=' + response.picture;
$.ajax({
type: "POST",
url: "ajax_fb_login.php",
data: data_string,
success: function (data) {
console.log(data);
//location.reload(); only has to happen once
}
})
});
}
</script>

谢谢:)

最佳答案

根据官方文档,类似以下内容应该可以解决问题:

FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});

FB.getLoginStatus() 允许您确定用户是否登录 Facebook 并已验证您的应用程序。用户有三种可能的状态:

  1. 用户已登录 Facebook 并已验证您的身份应用程序(已连接)
  2. 用户已登录 Facebook,但尚未验证您的身份申请(not_authorized)
  3. 用户未登录 Facebook 或明确退出您的应用程序,因此它不会尝试连接到 Facebook 和因此,我们不知道他们是否已经验证了您的申请(未知)

关于javascript - Facebook 登录按钮 - 如何在尝试登录之前检查我们是否已登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45575840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com