Ultime Notizie

Login with facebook in FudForum

 

This is a short tutorial to implement login with facebook in FudForum

First of all, you need a Facebook application id, so you have to go to:

https://developers.facebook.com

create your facebook application and get the application id to be used in your forum

 

then, you need to modify a couple of files in your template:

 

login.tmpl

you have to add a script, provided by facebook (the script will contain you app id) 

<script>
window.fbAsyncInit = function() {
  FB.init({
  appId : '<your app id>',
  cookie : true,
  xfbml : true,
  version : 'v7.0'
});
FB.AppEvents.logPageView();
};
(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 = "https://connect.facebook.net/en_US/sdk.js";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

function checkLoginState() {
  FB.getLoginStatus(function(response) {
  if(response.status == "connected") {
    $('input[name="fbtoken"]').val(response.authResponse.accessToken);
    $('#login').submit();
  }
});
}
</script>

add an hidden field in login form:

    <input type="hidden" name="fbtoken" />

(this field is populated by script)

and the login with Facebook button :

<div class="fb-login-button" data-size="large" data-button-type="login_with" data-layout="default"
data-auto-logout-link="false" data-use-continue-as="false" data-width=""
role="button" class="btn btn-primary"
scope="public_profile,email"
onlogin="checkLoginState();"></div>

 

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

login.php.t

this is the file where we will process the login form with new file fbtoken, posted by the script:

in this file, we will add the following piece of code:

 

// login with facebook
if (!$usr_d && (isset($_POST['fbtoken']) && $_POST['fbtoken']!="")) {
 
   $url = "https://graph.facebook.com/me?fields=id,name,email&access_token=".$_POST['fbtoken'];

   //Make the API call
    if ($result = @file_get_contents($url)) {

      //Decode the JSON.
      $decoded = json_decode($result, true);

      if (isset($decoded['error'])) {
         login_php_set_err('facebook', $decoded['error']['message']);
      } else {

         $login = $decoded["name"];
         $fbid = $decoded["id"];
         $email = $decoded["email"];

         if (!($usr_d = db_sab('SELECT last_login, id, passwd, salt, login, email, users_opt, ban_expiry, ban_reason FROM {SQL_TABLE_PREFIX}users WHERE email='. _esc($email)))) {
              /* Cannot login: user not in DB. */
              $GLOBALS['_ERROR_'] = 1;
              login_php_set_err('login', 'Email not found, please register');
         } else {
              // provided dat will be checked by standard fudforum login code
         }
      }
   }
}

 

you can find a fully working example here:

https://fidonet.3bsoft.com/index.php?t=login&