简体   繁体   English

如果用户已登录FB,如何阻止我的Facebook登录弹出窗口出现?

[英]How do I stop my Facebook login popup from appearing if user is already logged into FB?

I have a 'Sign in with Facebook' button that generates a popup which asks the user to enter FB credentials. 我有一个“登录Facebook”按钮,生成一个弹出窗口,要求用户输入FB凭据。

In the case where the user has already joined my app, left the app and then comes back (while still logged into Facebook), I'd like the user to be able to click 'sign in with Facebook' and not show the popup. 在用户已经加入我的应用程序的情况下,离开应用程序然后返回(同时仍然登录到Facebook),我希望用户能够点击“使用Facebook登录”而不显示弹出窗口。

At the moment, given the situation in the paragraph above, the popup opens for a second and then the page redirects to the logged in state of the app. 目前,鉴于上面段落中的情况,弹出窗口打开一秒钟,然后页面重定向到应用程序的登录状态。

I've implemented the following code below - I'm a complete noob when it comes to Javascript so whilst the answer might be obvious I don't know what to do! 我已经在下面实现了以下代码 - 当谈到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' });
     });
   });

When the page loads, after you then load and initialize the fb sdk use the FB.getLoginStatus method to check the user status. 页面加载后,然后加载并初始化fb sdk后,使用FB.getLoginStatus方法检查用户状态。 If the user is already logged in and authorized your app then the response should have the access token, user id, etc. 如果用户已经登录并授权您的应用程序,则响应应具有访问令牌,用户ID等。

For example: 例如:

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.

相关问题 如何检测用户使用我的登录按钮登录还是从Facebook登录? - how do I detect if user logged in using my login button or login from facebook? 如果用户已经登录,则隐藏“ FB登录”按钮 - Hide the FB Login button if user already logged in 如何阻止我的Facebook共享窗口被弹出窗口阻止程序阻止? - How do I stop my Facebook share window from being blocked by popup-blocker? FB.login总是询问我的密码(即使我已经登录) - FB.login is always asking my password (even if I'm already logged in) 如何查看已经登录Facebook的用户? - How to check user already logged in facebook? 每当用户尝试从我的网站使用Facebook登录时,都会获得facebook登录弹出窗口 - Get facebook login popup every time user try to Login using facebook from my website 如何检测用户是否已登录 Firebase? - How do I detect if a user is already logged in Firebase? 在Facebook应用程序的FB.login()之后如何停止重定向? - How to stop redirection after FB.login() for Facebook application? 用户登录后,如何将导航栏从“登录”更改为“用户名”? - How do i change the navigation bar from Login to the Username after the user logged in? 用户打开弹出窗口时如何停止轮询? - How do i stop polling when user opens a popup?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM