I’v recently posted an article mentioning Facebook Ion Auth, and a visitor from France asked me how did I installed it.
Please notice that you are at your own risk using this!
Please post a comment if you find any mistake, error, problem, issue, etc, etc!
Autor: Me!
The basic – Codeigniter, Ion Auth and Facebook Ion Auth
These are the steps.
- Install CodeIgniter on your server;
- Install Ion Auth in this CodeIgniter installation (Ion Auth’s installation tutorial);
- Copy the php of Facebook Ion Auth library;
- On your application/libraries/ create Facebook_ion_auth.php
- Past the copied php from Facebook Ion Auth, to the file created on the previous step and save it!
- On your application/config/autoload.php on $autoload[‘libraries’] add Facebook_ion_auth
-
$autoload['libraries'] = array('database', 'ion_auth', 'Facebook_ion_auth');
Create a app on Facebook
You need to create a app on Facebook!
On the top menu, go to Apps > Create a New App.
You will have now to go to Settings and Add a Platform and there, select Website.
Set now the Site URL and after it the App Domains.
Setting up Facebook Ion Auth
$this->app_id = ""; // your app id $this->app_secret = ""; // your app secret key $this->my_url = site_url(''); // url to redirect back from facebook $this->scope = 'email'; // custom permissions check - http://developers.facebook.com/docs/reference/login/#permissions
Enter the values that you get from Facebook on the right places!
I’v left the my_url blank and changed the original scope (read more about the scope) to:
$this->scope = 'email, user_birthday, user_location'; // custom permissions check - http://developers.facebook.com/docs/reference/login/#permissions
Search for ‘check if this user is already registered‘.
I’ve replaced this entire code
// check if this user is already registered if(!$this->CI->ion_auth_model->identity_check($user->email)){ $name = explode(" ", $user->name); $register = $this->CI->ion_auth->register($user->username, 'facebookdoesnothavepass123^&*%', $user->email, array('first_name' => $name[0], 'last_name' => $name[1])); } else { $login = $this->CI->ion_auth->login($user->email, 'facebookdoesnothavepass123^&*%', 1);
with this one
// check if this user is already registered if(!$this->CI->ion_auth_model->identity_check($user->email)){ $name = explode(" ", $user->name); if ($user->gender=='male') { $gender = 1; } else { $user->gender = 0; } // convert birthdate from m/d/Y to Y-m-d date mysql $date = date("Y-m-d", strtotime($user->birthday)); $register = $this->CI->ion_auth->register($user->username, 'facebookdoesnothavepass123^&*%', $user->email, array('first_name' => $name[0], 'last_name' => $name[1], 'gender' => $gender, 'bio' => $user->bio, 'url' => $user->link, 'birthday' => $date, 'location' => $user->location->name)); } else { $login = $this->CI->ion_auth->login($user->email, 'facebookdoesnothavepass123^&*%', 1);
This code registe the user with some other details that we get from regular Facebook Ion Auth, like
- gender,
- user bio,
- facebook link,
- birthday,
- location.
To have this working you will need to change the users table, created with Ion Auth sql script!
You will need to add the following columns:
- gender int(1)
- url varchar(256)
- bio text
- birthday date
- location varchar(56)
Okay!
Now the tricky part! Since Facebook redirects us to the URL set up on the Facebook_ion_auth.php in $this->my_url = site_url(”);
On the ‘default’ controller of the we have to call facebook_ion_auth and handle with the data sent from Facebook!
This is what I have on /application/controllers/frontpage.php – this is the controller that handles with the root of your CI.
class Frontpage extends Main_Controller { public function index($type = null) { if (isset($_GET['code'])) { $this->facebook_ion_auth->login(); if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin()) { header('Location:/?alert=facebooklogin'); exit(); } header('Location:/'); }
On application/controllers/auth.php at the end and before the last } , I’v added
function loginfacebook() { $this->facebook_ion_auth->login(); }
This way when I hit the url http://mydomain.com/auth/loginfacebook/ I got the Facebook Ion Auth working for me!
Or I get registered on the App and on my website or I get logged it.
I leave the url empty in $this->my_url = site_url(”) ??
Yes. I did.
This way Facebook will redirect the user to the root of CodeIgniter.
First, thanks for this tutorial,I followed all steps that you have given.
When I click on the login with facebook button, I will be redirected to the authentification interface of the facebook, I enter my email and my password, after that I have this message “MyApplication will receive the following info: ….”. I click Ok and I will be redirected to the root of codeigniter. Unfortunately, I will not be registred into the database 🙁
Well…
I’v tried it now logged out from FB… and it work perfectly!
Do you have the right domain set up on FB?
The table has the right fields?
try some debugs on the SQL insert of ion auth model… etc…
Please see what I have on my /application/controllers/frontpage.php… this controller is the one that handles with your root.
how is your facebook ion auth installation?
I have the same issue. No data gets registered in to the database. I just get re-directed to header(‘Location:/?alert=facebooklogin’);
thank you for this tutorial..
i have followed all the instruction but i couldn’t connect with facebook login page..
please give me more details so I can try to figure out where u got wrong..
when i click on button , facebook login page is not displayed..
i shows 404 Page not found error…
can you post there the URL of the facebook login?…
what you get in the browser…?
when i click on button, the url is
http://localhost/travelwebsite/auth/loginfacebook
what i should write on button click?
Do you have any video tutorial for this facebook ion auth?
hi .. it’s working now.. it was my silly mistake ..thank you for this tutorial..
I’m happy to know that! 🙂
its a pleasure to help anyone with this tutorial.
hey i want to know that what is the use of Frontpage controller??
its the controller for you *domain index*.
take a look at this
http://blog.stevenlu.com/2012/04/28/getting-started-with-codeigniter-bootstrap/
OK.. i got it.. thank you..:)
This is an awesome tutorial. Though I have one question. I have multiple user groups in my application and the group option is selected during registration. How can I assign a user it’s group during registration/login with facebook? Thanks.
Dunno!
Sorry.
I would use issset to check if the groups have been selected, if not redirect the user once logged in to a page that allows them to select the groups and then move them along to your home controller or wherever it is you would like them to go once logged in.
Are the groups default groups? As in all new members are given the same group?
If so you could just have your db assign a default value when populating the table, should the value have not been sent as part of registration.
Hi quick question for you.
What do you do with the call back code value you $_GET from the URL from facebook? did you store it? Also could you inform me how I can for example display the users name after they have logged in?
Thank you. Great Tutorial helped a lot!
Hi,
I am using this code. However, I am facing two problems a) After executing the sql queries in ion_auth all my existing tables are being referred to as “users.[old table name]” and b) No data is being added in my database. Not sure if both are connected. Can you please let me know what you think?
Thanks
Solve part (a). Was a mistake on my part. any suggestion on b?
Hi, I have the same problem as pointed out in this post below
http://stackoverflow.com/questions/19455665/ion-auth-facebook
can you please let me know where I am going wrong. The user gets logged in but is not recognised as logged in the ion auth.
Hello sir. this code is awesome . however do you have a work around for the errors found on the facebook login page of my site and can I put active 1 on the users table so the people with facebook doesn’t need to register thru email anymore.
http://www.kelloids.com
The process works and facebook notes that the user is logged in. However, CI does not. when i check for sessions, CI still notes as anonymous and i am guesing this is because the user is not added to the database. Kindly walk me through how i can do this. am guesing the issues comes with ion_auth library?
Hello you all!
I will make a new installation of CI and do the process from the beginning and record it to update this post.
tnk u all
thank you. Waiting for the update. my config(ion_auth) seems to be set ok…including the model of course but no data gets to the DB. Must be missing something very minor…help out
Works like champ. But a little clear documentation needed. Perfect Work. 🙂
tnks for the reply….
Yah! I know 🙁
I set up facebook plugin and when i open in browser http://localhost/auth/loginfacebook it nothing not happening. Redirect occurs on facebook and return back to the site, but there is no authorization. Only in my the address bar i have this http://i.gyazo.com/8b9b100c6e5983c6c8e1cbf2f21f21ef.png
The same here. Help us please 🙂
id id driver is not correct facebook
why test http://facebook.com/profile.php?id= not exist
Hi,
thank you very much for your tutorial !!
i would like to implement that in my website but i don’t find “check if this user is already registered”. Where is it ? In the library ? in the controller ?
Thanks for your reply 🙂
Thibaut
I found it !! Sorry… i looked for in Ion Auth controller or Library and not in facebook ion auth…
I have a question, the $user->username is null? I set up this aproach and I codeigniter told me that username is not define
Me too, facebook username is deprecated so you can’t get username from facebook. try alternative using an id as username
check this out:
https://developers.facebook.com/docs/facebook-login/permissions
Hello, awesome tutorial. For some reason I can only get back Name and ID in the response object. I don’t believe login review is necessary to get email correct?
I got the same error
For anyone who only get the Name an ID back, you should add the “fields” parameter to the graph request. Facebook_ion_auth.php library file at line 65. right after the $params[‘access_token’]
Dokumentation: https://developers.facebook.com/docs/graph-api/reference/user
you can just add “&fields=id,email,birthday,….” and you are good. After that the new User will also be added to the Database.