Previously, we have learned how to integrate Facebook login using a plugin called Facebook Login. A lot of things have changed since then, include Facebook SDK and the maintenance of Facebook Login plugin. It has become obviously that we need a new way to integrate Facebook login with CakePHP application.

In this tutorial, we will show you how to integrate Facebook login with CakePHP 3 application using another CakePHP plugin.

Table Of Content

  1. Setup Users Table
  2. Setup CakePHP HybridAuth Plugin
  3. Setup Controller
  4. Setup Event Listener
  5. Setup View
  6. The End
1. Setup Users Table

A database table is required to store users' information such as email, password and so on. To keep it simple, we will create a very simple users table. It contains fields required to demonstrate Facebook login only.

If your users table has fields, as shown above, it should work for this tutorial. You can use your own users table in that case.

2. Setup CakePHP HybridAuth Plugin

CakePHP HybridAuth Plugin is a wrapper around HybridAuth library. HybridAuth library builds an abstraction layer on top of various social login. Which makes the task of integrating social login a breeze.


Install CakePHP HybridAuth using Composer:


Load the plugin using Bake command:

This will basically append a line of code as shown below to your app's config/bootstrap.php. So you can do it manually if you wish to.


Make a config file config/hybridauth.php:

Replace "facebook-id" and "facebook-secret-key" with your own. If you do not know where to find them, refer to this link.

In some case, you might wonder if you could place this configure values in other config files. The answer is yes. As long as you make sure HybridAuth config values exist. This plugin will happily pick them up.

3. Setup Controller

Next, we need a controller action to allow users to log in.

Create src/Controller/UsersController:

A caveat here. Do not place any special logic after the $this->Auth->identify() call. Because this method is not guaranteed to return, it redirects users away to the social site for authentication.

One more step for the controller work. Add CakePHP HybridAuth's authentication handlers to our application's auth component.

Add the handler in app/AppController.php

4. Setup Event Listener

The event HybridAuth.newUser will be fired if CakePHP HybridAuth plugin could not find a user record from the users table. So an event listener is needed to create a brand new user record.

Add an event listener in app/AppController.php

Note that we created a random password using current timestamp. This way, we can avoid having an empty password.

5. Setup View

There is not much to setup in a view, besides understanding how to create a login link.

In HybridAuth library, a social login URL follows current format:


In CakePHP, we can generate the link above with HtmlHelper:

For now, your application should be integrated with Facebook login. Try it out and enjoy the benefits it brings for your users.

6. The End

Hopefully this simple tutorial helped you with your development.
If you like our post, please follow us on Twitter and help spread the word. We need your support to continue.
If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know.