The CakeDC Users Plugin provides an easy way to integrate Facebook social login into your application.
Note this is the updated tutorial for the latest version of the plugin 3.1.5.
This is a short how-to tutorial to enable Facebook login. We'll assume you have a brand new CakePHP 3.2+ application already setup.
Setup
Use composer to install the CakeDC Users Plugin
composer require cakedc/users composer require league/oauth2-facebook:@stable
Note we've upgraded to use league/oauth2 lib, and the old and not maintained opauth library was removed.
Load it from your bootstrap.php file
Plugin::load('CakeDC/Users', ['routes' => true, 'bootstrap' => true]);
Run migrations to add 2 new tables: 'users' and 'social_accounts'
bin/cake migrations migrate -p CakeDC/Users
Configuration
Load the Component in your src/Controller/AppController.php
public function initialize() { parent::initialize(); // // ... // $this->loadComponent('CakeDC/Users.UsersAuth'); }
Create a new Facebook application
- Go to Facebook developers and log in
- Create a new Facebook application new Facebook app
- Click "website" and select some awesome name for your brand new app (yeah, some random name would work too)
- Pick a Category, complete the quick start form, etc.
- Once you are done, go to your newly created app and click "settings"
- In settings, you should add your domain to "App domains" and ensure there is at least one login platform = "Website" defined
- Copy your App ID and secret
Setup the Plugin to use your Facebook app for login
Now you have a working Facebook app configured, we are going to link the CakeDC Users Plugin to use the app for login.
Update your bootstrap.php file to customize the CakeDC Users Plugin
Configure::write('Users.config', ['users']); Plugin::load('CakeDC/Users', ['routes' => true, 'bootstrap' => true]);
Create a new "config/users.php" file with contents
return [
'Users.Social.login' => true,
'OAuth.providers.facebook.options.clientId' => 'YOUR APP ID',
'OAuth.providers.facebook.options.clientSecret' => 'YOUR APP SECRET',
//etc
];
This file will override any configuration key present in the Plugin, you can check the configuration options here Configuration.
You are done!
Now the "login with Facebook" link (in "/login" page) will open the Facebook login popup and connect back to your application. If the email is provided by the user, he'll be automatically registered using the default role = 'user'. If no email is provided, the user will be requested to enter an email to complete the registration process in your application. Once his email is validated (link sent via email), he'll be able to login using Facebook.
Read more about CakeDC Users Plugin
Giving back to the community
This Plugin's development has been sponsored by the Cake Development Corporation. Contact us if you are interested in:
- Professional, commercial CakePHP development and consultancy
- Professional CakePHP training
- CakePHP code reviews
We hope you've enjoyed this short tutorial covering the Facebook login, stay tunned for new CakePHP + Users Plugin tutorials coming soon...