Facebook OAuth Login Using CakePHP
In this tutorial we are going to see how to implement facebook login in cakephp. In one of my previous tutorial I had implemented Facebook OAuth Login using PHP, where I had berifly explained Facebook app creation and integration. I will helpful for you to refer that tutorials before continuing this.
Here is my previous tutorials on Facebook login.

Create Facebook Application:
In Facebook login integration using CakePHP first and foremost thing we need to is to create Facebook Application to get Facebook App ID and App Secret. I had briefly explained creation of Facebook Application on Facebook OAuth 2 Login Using PHP Tutorial. So please follow the step 1 & step 2 of this tutorial to create Facebook Application. Here is the Link.
Once you completed Step 1 & step 2 of the above tutorial successfully, then you will get Facebook App ID and App Secret to integrate Facebook login in your CakePHP webapplication.
Crate Sample Table For Facebook Login Using CakePHP:
Create following sample users table in your Database for this cakephp login with google.
Facebook Login Integration In CakePHP:
To integrate Facebook Login in your CakePHP Webapplications, we need Facebook PHP SDK to make OAuth request to Facebook. I have downloaded recent version Facebook PHP SDK from Facebook.
Note : It’s best advisable to use Facebook PHP SDK i had used in this tutorial to successfully integrate Facebook login in your CakePHP webapplication. Please download source file using above download link.
https://developers.facebook.com/docs/php/gettingstarted/4.0.0
Now I have created a folder name fb in the app/Vendor directory, then from downloaded Facebook PHP SDK copied only src directory and autoload.php file and pasted it in the app/Vendor/fb directory. Now it is app/Vendor/fb/src..
Include Facebook PHP SDK In Your CakePHP Application:
Create site_config.php file in the app/Config directory to keep all your application wide constants. Here is my site_config.php file. Where please replace your app id and app secret.
Please include this site_config.php file in your bootstrap.php file in the app/Config directory.
Before we are going to make any OAuth API request to Facebook we must include that Facebook PHP SDK in our file. For that I am creating fb.php file in the app/Config directory and including following files. Here is my fb.php file.
Note: If it throughs FacebookSession class not found error, then copy above lines and include in your UsersController.php file at the top.
OR
Use only either one of the method to add Facebook PHP SDK in your CakePHP webapplication.
Making Facebook Oauth API Request:
To make Facebook OAuth Login API request as well as handle Facebook OAuth response I am creating following two functions. where fblogin() function which makes Facebook OAuth login request, and fb_login() function which handles Facebook OAuth login response from the Facebook.
This following two functions I had kept it in UsersController.php file. So here is the my CakePHP scripts of the two functions.
Note : I had directly included Facebook PHP SDK files at the top of the UsersController
CakePHP Facebook Login Auth Link:
Finally we need to give Facebook Login link to user to use Facebook login. Here is my html markup.
CakePHP Routing for Google Login:
I had modified url string for our Facebook login to look nice. So please add the following lines of code in your routes.php file in the app/Config directory.
CakePHP Auth Allow() for Google Login:
Final thing we need to allow user to access following two functions fblogin() and fb_login() without logging in our system in the beforeFilter() of UsersController.
You may also like some of my tutorial on CakePHP
Download Premium Only Scripts & 80+ Demo scripts Instantly at just 1.95 USD per month + 10% discount to all Exclusive Scripts
If you want any of my script need to be customized according to your business requirement,
Please feel free to contact me [at] muni2explore[at]gmail.com
Note: But it will be charged based on your customization requirement