This article is inspired by this question in Stack Overflow and belongs to a series of articles describing the step by step tutorial to configure CakeDC Users Plugin with the most commonly used Oauth2 providers, in this case we'll configure Google login.
We'll assume you have a working CakePHP application with no Auth configured yet.
Setup
Use composer to install the CakeDC Users Plugin and the required oauth2 providers
To be able to configure the callbacks in Google dashboard, you'll need to create a virtual host for you application. You don't need a working domain name, you could use something like "mydomain.dev" but Google requires a domain name (no localhost).
composer require cakedc/users:@stable composer require league/oauth2-google:@stable
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 Google application
- Go to https://console.developers.google.com/apis/dashboard and create a new google+ application
- You can follow the datailed instructions (screenshots included) here: Step by step create google app for social login Oauth2
- Once you get your client id and client secret strings, add the following configuration file under /config/users.php, and paste your client id and secret. Trim any blank space in the codes before paste.
<?php // /config/users.php file contents $config = [ 'Users.Social.login' => true, 'OAuth.providers.google.options.clientId' => 'CLIENT_ID_HERE', 'OAuth.providers.google.options.clientSecret' => 'SECRET_HERE', ]; return $config;
- Modify your bootstrap.php file to ensure the config file is loaded this way
Configure::write('Users.config', ['users']); //add this line before Plugin::load('CakeDC/Users... Plugin::load('CakeDC/Users', ['routes' => true, 'bootstrap' => true]);
This file will override any configuration key present in the Plugin, you can check the configuration options here Configuration.
If you want to use a different page as homepage, and this page requires authorization, don't forget to add a rule to permissions.php file to allow users with role 'user' to read your homepage, for example, add this content to your config/permissions.php file to enable access to your homepage
<?php return [ 'Users.SimpleRbac.permissions' => [ [ 'role' => 'user', 'controller' => 'YOUR_HOMEPAGE_CONTROLLER_NAME', 'action' => 'YOUR_HOMEPAGE_ACTION_NAME', ],
// ... more rules here
]];
Now you are ready to go to your login page and click "Sign up with Google".
Upon successful login, a new user will be created in your users table and related oauth2 tokens will be saved in the social_accounts table. The new user created will have the "user" role (by default, but customizable). And based on your Auth rules, this user will be able to access your site.
You are done!
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 CakePHP consulting
- Professional CakePHP training
- CakePHP code review
We hope you've enjoyed this short tutorial covering the Google login, stay tunned for new CakePHP + Users Plugin tutorials coming soon...