Facebook登录JS - FB.Event.subscribe('auth.login')触发器没有登录按钮点击 [英] Facebook login JS - FB.Event.subscribe('auth.login') triggers without login button click

查看:92
本文介绍了Facebook登录JS - FB.Event.subscribe('auth.login')触发器没有登录按钮点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的需要你帮助登录我的网站功能。

really need your help with log-in-with-facebook feature I'm trying to implement on my website.

基本上,我正在努力实现以下:

Basically, I'm trying to achieve the following:


  • 如果用户之前已确认该应用并点击我的网站上的FB登录按钮
    ,他们会得到登录到网站(网站的用户
    帐户与Facebook用户ID关联)

  • if user has acknowledged the app before and clicks FB Log in button on my website, they get logged into the website (with website's user account associated with the Facebook user ID)

如果用户之前未确认该应用,请在FB日志中登录在(和订阅应用程序)他们被重定向到网站的注册页面。此表格预先填入了用户数据
来自Facebook并且注册过程变得更加容易和快捷。

if user has not acknowledged the app before, on FB log in (and subscription to app) they get redirected to website's registration page. Here the form is pre-filled with user data coming through Facebook and registration process becomes easier and faster.

我正在使用下面的代码。

I'm using the code below.

<body>

<div id="fb-root"></div>
<script>
    window.fbAsyncInit = function() {
        FB.init({
          appId: 'xxxxxxxx',
          status: true,
          cookie: true, 
          xfbml: true,
          oauth: true
        });

        FB.Event.subscribe('auth.login', function(response) {
              if (response.status === 'connected') {
                    var uid = response.authResponse.userID;
                    var accessToken = response.authResponse.accessToken;

                    if ((parseFloat(uid) == parseInt(uid)) && !isNaN(uid)) {
                        $.ajax({
                            url: '/user_actions/prep_facebook_registration',
                            cache: false,
                            type: 'POST',
                            data: { 'uid': uid, 'token': accessToken },
                            dataType: 'json',
                            success: function(data) {
                                if (data.success=='true') {
                                    if ((typeof(data.redirect) != 'undefined')) {
                                        window.location=data.redirect;
                                    }
                                }
                            }
                        });
                    }
              }
        });

        FB.Event.subscribe('auth.logout', function(response) {
            FB.getLoginStatus(function(res) {
                if (!res.authResponse) {
                    window.location='/access/logout';
                }
            });
        });

        FB.getLoginStatus(function(response) {
          if (response.status === 'connected') {
            var uid = response.authResponse.userID;
            var accessToken = response.authResponse.accessToken;
            //not doing anything so far
          }
        });
      };
            (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/all.js#xfbml=1";
          fjs.parentNode.insertBefore(js, fjs);
      }(document, 'script', 'facebook-jssdk'));
</script>

登录按钮在网站中进一步向下:

LogIn button lays further down in a website:

<div class="fb-login-button" autologoutlink="true" data-show-faces="false" data-width="166" data-max-rows="1" style="position: absolute; top: 5px; right: 0;" scope="email"></div>

这里的问题是FB.Event.subscribe('auth.login')不仅会触发FB登录按钮单击,但如果用户已登录到Facebook并且只有在访问我的网站后。这导致重定向到注册页面或当前页面重新加载,即使用户没有点击登录按钮!

The problem here is that FB.Event.subscribe('auth.login') triggers not only on FB log in button click, but also if user has logged to Facebook and only after comes to my website. This causing a redirection to registration page or current page reload even if user hasn't clicked the login button!

'/ user_actions / prep_facebook_registration'包含一个脚本,它正在检查如果该成员必须重定向到注册页面或应该使用本地帐户登录。它返回重定向的URL。

'/user_actions/prep_facebook_registration' holds a script, which is checking if the member has to be redirected to registration page or should be loggedin with local account. It returns the URL to redirect.

我做错了什么?如何避免在登录按钮单击之外触发FB.Event.subscribe('auth.login')?

What am I doing wrong? How can I avoid FB.Event.subscribe('auth.login') being triggered outside the login button click?

推荐答案

= ====================

=====================

编辑:好的,我认为有一点进步这里:)

我用 onlogin 事件扩展了FB登录按钮,该事件调用JS函数Facebook_login()

I've extended the FB login button with onlogin event which calls a JS function Facebook_login()

<div class="fb-login-button" onlogin="Facebook_login()" autologoutlink="true" [parameters]></div>

我已导出 uid accessToken window.fbAsyncInit函数之外的变量,所以我可以从其他地方访问它们:

I've exported uid and accessToken variables outside window.fbAsyncInit function so I could access them from anywhere else:

var uid;
var accessToken;

window.fbAsyncInit = function() {
   FB.init({
      your parameters
   });
});

然后,我得到FB.Event.subscribe('auth.login')只分配值给我的 uid accessToken 变量,但不执行任何重定向。

Then, I got FB.Event.subscribe('auth.login') to only assign values to my uid and accessToken variables, but not perform any redirection.

FB.Event.subscribe('auth.login', function(response) {
  if (response.status === 'connected') {
    uid = response.authResponse.userID;
    accessToken = response.authResponse.accessToken;
  }
});

最后,我的自定义函数Facebook_login()获取uid和accessToken值并执行其余的操作。这次只有在单击登录按钮时才会发生。请注意,此函数在window.fbAsyncInit()之外。

And finally, my custom function Facebook_login () fetches the uid and accessToken values and does the rest of the operation. This time it happens only when login button is clicked. Please note, this function is outside window.fbAsyncInit()

function Facebook_login () {
          if ((parseFloat(uid) == parseInt(uid)) && !isNaN(uid)) {
                $.ajax({
                    url: '/user_actions/prep_facebook_registration',
                    cache: false,
                    type: 'POST',
                    data: { 'uid': uid, 'token': accessToken },
                    dataType: 'json',
                    success: function(data) {
                        if (data.success=='true') {
                            if ((typeof(data.redirect) != 'undefined')) {
                                if (data.redirect=='current') {
                                    location.reload();
                                } else {
                                    window.location=data.redirect;
                                }
                            }
                        }
                    }
                });
            }
      }

这似乎对我有用。如果有人有任何更好的解决方案,您的意见将得到赞赏:)

This seems to do a job for me. If anyone has any better solutions, your input would be appreciated :)

这篇关于Facebook登录JS - FB.Event.subscribe('auth.login')触发器没有登录按钮点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆