简体   繁体   English

如何查看已经登录Facebook的用户?

[英]How to check user already logged in facebook?

I am using facebook javascript SDK to integrate login with facebook in my app. 我正在使用Facebook javascript SDK将登录信息与Facebook集成到我的应用程序中。 I have 2 JS files in my app. 我的应用程序中有2个JS文件。

  1. FacebookLogin.js FacebookLogin.js
  2. content.js content.js

Now i am loading FacebookLogin.js in content.js. 现在,我正在content.js中加载FacebookLogin.js。 Further I have below code in my FacebookLogin.js 此外,我的FacebookLogin.js中包含以下代码

var userID;
window.fbAsyncInit = function() {
    //SDK loaded, initialize it
    FB.init({
        appId      : 'MY_APP_ID',
        xfbml      : true,
        version    : 'v2.2'
    });

    //check user session and refresh it
    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
            //user is authorized
            userID = response.authResponse.userID;
        } else {
            document.getElementById('status').innerHTML = 'Please log into Facebook.';
        }
    });
};

Now I want to use that userID variable in my content.js file. 现在,我想在我的content.js文件中使用该userID变量。 So that i can check if user is already logged in, like below 这样我就可以检查用户是否已经登录,如下所示

if(userID === undefined && userID === null)
    {
        showSocialLoginPopup(); 
        return false;
    }

here showSocialLoginPopup() displays a popup with different login options like facebook, Twitter, etc. Now if the user is already logged in then i dont want to display that popup. 在这里showSocialLoginPopup()显示带有不同登录选项(如Facebook,Twitter等showSocialLoginPopup()的弹出窗口。现在,如果用户已经登录,则我不想显示该弹出窗口。

This is mostly a question about scope. 这主要是关于范围的问题。

All of your .js files will be loaded into the same DOM, and therefore can access variables and functions and such across files. 您所有的.js文件都将加载到同一DOM中,因此可以跨文件访问变量和函数等。

One option is to make userID a global variable : 一种选择是使userID成为全局变量:

1) make sure that your content.js is loaded AFTER your FacebookLogin.js 1)确保在FacebookLogin.js之后加载content.js
2) declare your variable userID outside of any functions or objects 2)在任何函数或对象之外声明变量userID

this will allow you to access userID anywhere in your javascript 这将允许您在javascript中的任何位置访问userID

Here is a small tutorial on variable scope in javascript: 这是有关javascript中变量作用域的小教程:
http://www.w3schools.com/js/js_scope.asp http://www.w3schools.com/js/js_scope.asp

Edit: Another potential problem could be timing. 编辑:另一个潜在的问题可能是时间。

The FB.getLoginStatus() is an asynchronous call, and will take some time complete, the userID does not get set until after that call is finished. FB.getLoginStatus()是一个异步调用,将花费一些时间来完成,直到该调用完成后才设置userID。

If your function from content.js is not behind some kind of event or timeout then it may well be executing before the FB call finishes which means userID would be undefined still. 如果content.js中的函数没有发生某种事件或超时,则它很可能在FB调用完成之前正在执行,这意味着userID仍将是未定义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM