简体   繁体   English

Facebook实施,注册后

[英]Facebook implementation, after registration

I'm using the script below when a user wants to register to my website, so when he approves the application the fields are being automatically filled and he just needs to submit the form. 当用户想要注册到我的网站时,我正在使用以下脚本,因此当他批准该应用程序时,字段将自动填写,而他只需要提交表单即可。

But i'm stuck on the second part of the login mechanism, how do I check if a user is already logged in to his facebook account and he approved my application? 但是我被困在登录机制的第二部分,如何检查用户是否已经登录到他的facebook帐户并且他批准了我的申请?

I keep the facebook id of each user in my users db. 我将每个用户的facebook ID保留在我的用户数据库中。

Note: i'm using the user flow, not the server flow. 注意:我使用的是用户流程,而不是服务器流程。

<script type="text/javascript">
        var appId = "xxxxxxxxx";

        if(window.location.hash.length == 0)
        {
            url = "https://www.facebook.com/dialog/oauth?client_id=" + 
                     appId  + "&redirect_uri=" + window.location +
                     "&response_type=token";
            //window.open(url);

        } else {
            accessToken = window.location.hash.substring(1);
            graphUrl = "https://graph.facebook.com/me?" + accessToken +
                        "&callback=displayUser"

            //use JSON-P to call the graph
            var script = document.createElement("script");
            script.src = graphUrl;
            document.body.appendChild(script);  
        }


        function displayUser(user) {
            myform = document.forms['regform'];
            username = user.name;
            first = username.split(" ");
            myform.elements['fname'].value = first[0];
            myform.elements['lname'].value = first[1];
            myform.elements['username'].value = first[0]+first[1];
            myform.elements['email'].value = user.email;
            myform.elements['fbid'].value = user.id;
        }
</script>

You may want to use the Facebook JS SDK: http://developers.facebook.com/docs/reference/javascript/ 您可能要使用Facebook JS SDK: http : //developers.facebook.com/docs/reference/javascript/

For example to check login status you can do: 例如,要检查登录状态,您可以执行以下操作:

    FB.getLoginStatus(function(response) {
      if (response.session) {
        // logged in and connected user, someone you know
      } else {
        // no user session available, someone you dont know
      }
    });

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

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