Facebook登录网站

时间:2013-11-21 22:02:55

标签: javascript php facebook facebook-javascript-sdk

我正在尝试在我的网站上实施Facebook登录。我有一些问题。 当用户允许我在我的数据库中创建一个新帐户时(我可以使用我的功能来检查用户是否已登录以及更多员工),我会被堆叠起来。

我的问题是当用户(已在我的网站上注册Facebook)我如何登录他?仅使用他的Facebook ID?但如果有人知道我的Facebook ID,他可以登录吗?

一些代码段HTML:

<a href="#facebook" id="f_in" class="log">Log In With Facebook</a>

JS

window.fbAsyncInit = function() {
   FB.init({
     appId      : 'APPID', // App ID
     channelURL : '', // Channel File, not required so leave empty
     status     : true, // check login status
     cookie     : true, // enable cookies to allow the server to access the session
     oauth      : true, // enable OAuth 2.0
     xfbml      : false  // parse XFBML
   });
};
(function() {
   var e = document.createElement('script'); e.async = true;
   e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';                
   document.getElementById('fb-root').appendChild(e);
}());
//login Function
function login(){
    FB.getLoginStatus(function(response){
        if(response.status === 'connected'){
                getCurrentUserInfo(response);
        }else{
            FB.login(function(response) {
                if(response.authResponse) {
                  //if (response.perms)
                      getCurrentUserInfo(response);
                } else {
                  console.log('Auth cancelled.')
                }
            },{scope:'email, user_birthday'}); // which data to access from user profile
        }
    });
}
function getCurrentUserInfo() {
      FB.api('/me', function(userInfo) {
        do_reg(userInfo);
    });
} 
function do_reg(userInfo){
    var dataString = "username="+userInfo.first_name+"&id="+userInfo.userID"&email="+userInfo.email;
     $.ajax({
    type:"POST",
    url: "do_reg.php",
    data: dataString,
           success:function(data){
         console.log(data);     
       },
    error:function(){

    }
    });
}

还有一些PHP:

    $username = $_POST['username'];
    $email = $_POST['email'];
    $id = $_POST['id'];
    $pass = pass_gen(10);

    $userData['uName']  = $username;
    $userData['uEmail'] = $email;
    $userData['uFid']   =   $id; //facebook ID
    $userData['uPassword'] = $pass;
    $userData['uPasswordConfirm'] = $pass;

    $create_user_row = UserInfo::register($userData); //create user in my DB
    if (is_object($create_user_row)) {
                  $u = new User; 
                  $u->loginByUserID($create_user_row->getUserID()); //login to my website system using created user object if success.
}
    //password generator
    function pass_gen($length)
        {
            $random= "";

            srand((double)microtime()*1000000);

            $data = "AbcDE123IJKLMN67QRSTUVWXYZ";
            $data .= "aBCdefghijklmn123opq45rs67tuv89wxyz";
            $data .= "0FGH45OP89";

            for($i = 0; $i < $length; $i++)
            {
                $random .= substr($data, (rand()%(strlen($data))), 1);
            }

            return $random;
        } 

流速: 1.用户点击Facebook登录  1.1检查用户是否已登录Facebook  2.2检查权限问题  1.获取用户数据并通过ajax发送到do_reg.php  2.根据POST数据在DB中创建新用户  3.使用创建的对象将用户登录到网站系统。

所以问题是如果用户没有登录我的网站,但是他已经通过网站上的Facebook创建了帐户当我点击使用现有帐户登录Facebook时如何登录他?

通过Facebook ID? 我想我正在失去某种流动。

2 个答案:

答案 0 :(得分:0)

实际上,您不必手动处理用户的登录状态。 Facebook SDK for JavaScript自动处理访问令牌存储和跟踪登录状态,因此使用它的应用程序不需要创建自己的机制,这就是使用Facebook SDK for JavaScript的重点。 login documentation将帮助您了解实际应该如何完成。

但是,您可以在数据库中存储一些用户信息供您自己参考。

答案 1 :(得分:0)

  

我的问题是当用户(已经在我的网站上注册时)   Facebook)我该如何登录他?仅使用他的Facebook ID?但如果是的话   是否有人知道我可以登录我的脸书ID?

正如Rahil所说,由于您使用的是FBLogin,因此您无需在最终重建登录机制。一旦用户单击应用程序中的FB登录按钮,JS代码将检查用户的状态,FB将返回状态值,如下所示,

**connected**. The person is logged into Facebook, and has logged into your app.
**not_authorized**. The person is logged into Facebook, but has not logged into your app.
**unknown**. The person is not logged into Facebook, so you don't know if they've logged into your app

用户流程将是这样的,

1)首次用户访问您的网站并点击FB按钮

2)现在FB检查用户是否登录到了脸书

  If the user is already logged in then FB check user already authorised or not

  If not then FB shows the auth dialog 

     User accepts your permissions then Fb direct to your website

     Now FB will return "connected" status. During this time add on entry in your table about the new user

3)用户第二次访问您的网站并点击FB登录时,

 Now you will get the connected response from FB, then check your db table, If the user is a new user or already a member by checking into your table for an entry.
相关问题