Today I am writing about new steps that how to create a social login in CakePHP with HybridAuth library, what exactly you required for social media integration and what the steps, you can find below and go through the post step by step and complete your CakePHP HybridAuth development study.

A CakePHP plugin which allows using the HybridAuth social sign on library

CakePHP HybridAuth

Required:

CakePHP 3.x. If not then click here to see the installation steps.

Users login

HybridAuth

Installation:

Run this command:

&lt;em&gt;$ composer require --prefer-dest admad/cakephp-hybridauth&lt;/em&gt;

Setup:

Run this command:

$ bin/cake plugin load ADmad/HybridAuth -b -r

Configuration:

Create a file “hybridauth.php” inside of config folder:

config/hybridauth.php:

use Cake\Core\Configure; return [ 'HybridAuth' =&amp;gt; [ 'providers' =&amp;gt; [ 'Google' =&amp;gt; [ 'enabled' =&amp;gt; true, 'keys' =&amp;gt; [ 'id' =&amp;gt; '&amp;lt;google-client-id&amp;gt;', 'secret' =&amp;gt; '&amp;lt;secret-key&amp;gt;' ], "scope" =&amp;gt; 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read',// optional "approval_prompt" =&amp;gt; "force" ], 'Facebook' =&amp;gt; [ 'enabled' =&amp;gt; true, 'keys' =&amp;gt; [ 'id' =&amp;gt; '&amp;lt;facebook-application-id&amp;gt;', 'secret' =&amp;gt; '&amp;lt;secret-key&amp;gt;' ], 'scope' =&amp;gt; 'email, user_about_me, user_hometown' ], 'LinkedIn' =&amp;gt; [ 'enabled' =&amp;gt; true, 'keys' =&amp;gt; [ 'id' =&amp;gt; '&amp;lt;linkedin-key&amp;gt;', 'secret' =&amp;gt; '&amp;lt;secret-key&amp;gt;' ], "scope"&nbsp;&nbsp; =&amp;gt; "r_basicprofile r_emailaddress", // optional ] ], 'debug_mode' =&amp;gt; Configure::read('debug'), 'debug_file' =&amp;gt; LOGS . 'hybridauth.log', ] ];

Database:

Create a table “social_profiles” run below command:

$ bin/cake migrations migrate -p ADmad/HybridAuth

Usage:

In AppController’s initialize() method add below script for allowing social login:

# FOR ALLOWING FACEBOOK LOGIN ALSO

parent::initialize(); $this-&amp;gt;loadComponent('Auth', [ 'authenticate' =&amp;gt; [ 'Form', 'ADmad/HybridAuth.HybridAuth' =&amp;gt; [ // All keys shown below are defaults 'fields' =&amp;gt; [ 'provider' =&amp;gt; 'provider', 'openid_identifier' =&amp;gt; 'openid_identifier', 'email' =&amp;gt; 'email' ],

'profileModel' =&amp;gt; 'ADmad/HybridAuth.SocialProfiles', 'profileModelFkField' =&amp;gt; 'user_id', 'userModel' =&amp;gt; 'Users',

// The URL Hybridauth lib should redirect to after authentication. // If no value is specified you are redirect to this plugin's // HybridAuthController::authenticated() which handles persisting // user info to AuthComponent and redirection. 'hauth_return_to' =&amp;gt; [ 'controller'&nbsp; =&amp;gt; 'Venues', 'action'&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;=&amp;gt; 'update_status', 'plugin' =&amp;gt; false ] ] ], 'loginRedirect' =&amp;gt; array('controller' =&amp;gt; '&amp;lt;controller Name&amp;gt;', 'action' =&amp;gt; '&amp;lt;Method name&amp;gt;'), 'logoutRedirect' =&amp;gt; array('controller' =&amp;gt; '&amp;lt;controller Name&amp;gt;', 'action' =&amp;gt; '&amp;lt;Method name'), ]);

Your UserController’s login method should be similar to this:

public function login() { if ($this-&amp;gt;request-&amp;gt;is('post') || $this-&amp;gt;request-&amp;gt;query('provider')) { $user = $this-&amp;gt;Auth-&amp;gt;identify(); if ($user) { $this-&amp;gt;Auth-&amp;gt;setUser($user); return $this-&amp;gt;redirect($this-&amp;gt;Auth-&amp;gt;redirectUrl()); } $this-&amp;gt;Flash-&amp;gt;error(__('Invalid username or password, try again')); } }

On your login page, you can create links to initiate authentication using required providers. Specify the provider name using the variable named provider in the query string.

echo $this-&amp;gt;Form-&amp;gt;postLink( 'Login with Google', ['controller' =&amp;gt; 'Users', 'action' =&amp;gt; 'login', '?' =&amp;gt; ['provider' =&amp;gt; 'Google']] );

Setup a method of your UsersTable as a callback for the event:

public function initialize(array $config) { parent::initialize($config);

$this-&amp;gt;setTable('users'); $this-&amp;gt;setDisplayField('id'); $this-&amp;gt;setPrimaryKey('id'); $this-&amp;gt;addBehavior('Timestamp'); $this-&amp;gt;hasMany('ADmad/HybridAuth.SocialProfiles'); \Cake\Event\EventManager::instance()-&amp;gt;on('HybridAuth.newUser', [$this, 'createUser']); }

public function createUser(\Cake\Event\Event $event)

{ // Entity representing record in social_profiles table $profile = $event-&amp;gt;data()['profile'];

// Make sure here that all the required fields are actually present

$user = $this-&amp;gt;newEntity(['email' =&amp;gt; $profile-&amp;gt;email]); $user = $this-&amp;gt;save($user);

if (!$user) {

throw new \RuntimeException(‘Unable to save new user’);

}

return $user; }

Create a Model src/Model/SocialProfilesTable.php and add similar to this:

namespace ADmad\HybridAuth\Model\Table; use Cake\ORM\Table; /** * HybridAuth Authenticate * * Licensed under The MIT License * For full copyright and license information, please see the LICENSE.txt */ class SocialProfilesTable extends Table { /** * Initialize table. * * @param array $config Configuration * @return void */ public function initialize(array $config) { parent::initialize($config); $this-&amp;gt;addBehavior('Timestamp'); $this-&amp;gt;belongsTo('Users'); } }

For more information about the CakePHP hybridauth configuration array click here. I hope you will find the article informative about CakePHP HybridAuth Library, If you find any problem to fix it then plz contact us and will help you out. Also, share your thoughts below in the comment box about CakePHP HybridAuth Library.

Author: admin

Tags: Create Social login in CakePHP, Create Social login in CakePHP with HybridAuth Library, Social login in CakePHP