2017 © Pedro Peláez
 

cakephp-plugin cakephp-hybridauth

A CakePHP plugin for using the HybridAuth social sign on library

image

admad/cakephp-hybridauth

A CakePHP plugin for using the HybridAuth social sign on library

  • Thursday, July 19, 2018
  • by ADmad
  • Repository
  • 15 Watchers
  • 78 Stars
  • 28,012 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 37 Forks
  • 7 Open issues
  • 17 Versions
  • 7 % Grown

The README.md

If you are using CakePHP 3.4+ checkout my ADmad/cakephp-social-auth instead.

CakePHP HybridAuth Plugin

Total Downloads License, (*1)

A CakePHP plugin which allows using the HybridAuth social sign on library., (*2)

Requirements

  • CakePHP 3.1+.

Installation

Run:, (*3)

composer require --prefer-dist admad/cakephp-hybridauth

Setup

Load the plugin by running following command in terminal:, (*4)

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

or by manually adding following line to your app's config/bootstrap.php:, (*5)

Plugin::load('ADmad/HybridAuth', ['bootstrap' => true, 'routes' => true]);

Configuration

Make a config file config/hybridauth.php:, (*6)

use Cake\Core\Configure;

return [
    'HybridAuth' => [
        'providers' => [
            'Google' => [
                'enabled' => true,
                'keys' => [
                    'id' => '<google-client-id>',
                    'secret' => '<secret-key>'
                ]
            ],
            'Facebook' => [
                'enabled' => true,
                'keys' => [
                    'id' => '<facebook-application-id>',
                    'secret' => '<secret-key>'
                ],
                'scope' => 'email, user_about_me, user_birthday, user_hometown'
            ],
            'Twitter' => [
                'enabled' => true,
                'keys' => [
                    'key' => '<twitter-key>',
                    'secret' => '<twitter-secret>'
                ],
                'includeEmail' => true // Only if your app is whitelisted by Twitter Support
            ]
        ],
        'debug_mode' => Configure::read('debug'),
        'debug_file' => LOGS . 'hybridauth.log',
    ]
];

For more information about the hybridauth configuration array check http://hybridauth.github.io/hybridauth/userguide/Configuration.html, (*7)

Database

The plugin expects that you have a users table with at least email field and a social_profiles table. You can run, (*8)

bin/cake migrations migrate -p ADmad/HybridAuth

to generate the social_profiles tabel using a migration file provided with the plugin., (*9)

Usage

Check the CakePHP manual on how to configure and use the AuthComponent with required authentication handler. You would have something like this in your AppController's initialize() method., (*10)

$this->loadComponent('Auth', [
    'authenticate' => [
        'Form',
        'ADmad/HybridAuth.HybridAuth' => [
            // All keys shown below are defaults
            'fields' => [
                'provider' => 'provider',
                'openid_identifier' => 'openid_identifier',
                'email' => 'email'
            ],

            'profileModel' => 'ADmad/HybridAuth.SocialProfiles',
            'profileModelFkField' => 'user_id',

            'userModel' => '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' => null
        ]
    ]
]);

Note: When specifying loginRedirect and loginAction URLs for AuthComponent be sure to add 'plugin' => false (or appropiate plugin name) to the URL array., (*11)

Your controller's login action should be similar to this:, (*12)

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

Note: When your action calls $this->Auth->identify() the method may not return. The authenticator may need to redirect to the provider's site to complete the identification procedure. It's important not to implement any important business logic that depends upon the identify() method returning., (*13)

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

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

We use a POST link here instead of a normal link to prevent search bots and other crawlers from following the link. (Adding "nofollow" attribute to link doesn't suffice as it's often ignored by bots/crawlers.), (*15)

Once a user is authenticated through the provider the authenticator gets the user profile from the identity provider and using that tries to find the corresponding user record in your app's users table. If no user is found emits a HybridAuth.newUser event. You must setup a listener for this event which save new user record to your users table and return an entity for the new user. Here's how you can setup a method of your UsersTable as callback for the event., (*16)

If you also want to monitor all logins - and execute e.g. a login counter - you can listen for the HybridAuth.login event., (*17)

public function initialize(array $config)
{
    $this->hasMany('ADmad/HybridAuth.SocialProfiles');

    \Cake\Event\EventManager::instance()->on('HybridAuth.newUser', [$this, 'createUser']);
    \Cake\Event\EventManager::instance()->on('HybridAuth.login', [$this, 'updateUser']);
}

public function createUser(\Cake\Event\Event $event) 
{
    // Entity representing record in social_profiles table
    $profile = $event->data()['profile'];

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

    $user = $this->newEntity(['email' => $profile->email]);
    $user = $this->save($user);

    if (!$user) {
        throw new \RuntimeException('Unable to save new user');
    }

    return $user;
}

public function updateUser(\Cake\Event\Event $event, array $user) 
{
    $this->updateAll(['logins = logins + 1', 'last_login' => new FrozenTime()], ['id' => $user['id']]);
}

Additionally, you can also get a flash message for login back using the HybridAuth.login event:, (*18)

// In your AppController
    public function initialize()
    {
        EventManager::instance()->on('HybridAuth.login', [$this->MyComponent, 'updateUser']);
    }

// In your MyComponent
    public $components = [
        'Flash'
    ];

    public function updateUser(Event $event, array $user)
    {
        $this->Flash->success(__('You are now logged in'));
    }

Twitter & email addresses

If you are trying to achieve a 'Sign in using Twitter' functionality, and you require the users email address, you need to specifically get your application white-listed by Twitter Support using this form and selecting 'I need access to special permissions'. Then you can use the 'includeEmail' => true configuration option., (*19)

Copyright 2016 ADmad, (*20)

License

See LICENSE, (*21)

The Versions

19/07 2018

dev-master

9999999-dev http://github.com/ADmad/CakePHP-HybridAuth

A CakePHP plugin for using the HybridAuth social sign on library

  Sources   Download

MIT

The Requires

 

social cakephp hybridauth social authentication social signon multi provider

11/07 2018

4.2.0

4.2.0.0 http://github.com/ADmad/CakePHP-HybridAuth

A CakePHP plugin for using the HybridAuth social sign on library

  Sources   Download

MIT

The Requires

 

social cakephp hybridauth social authentication social signon multi provider

30/03 2017

4.1.1

4.1.1.0 http://github.com/ADmad/CakePHP-HybridAuth

A CakePHP plugin for using the HybridAuth social sign on library

  Sources   Download

MIT

The Requires

 

social cakephp hybridauth social authentication social signon multi provider

19/02 2017

4.1.0

4.1.0.0 http://github.com/ADmad/CakePHP-HybridAuth

A CakePHP plugin for using the HybridAuth social sign on library

  Sources   Download

MIT

The Requires

 

social cakephp hybridauth social authentication social signon multi provider

28/11 2016

4.0.5

4.0.5.0 http://github.com/ADmad/CakePHP-HybridAuth

A CakePHP plugin for using the HybridAuth social sign on library

  Sources   Download

MIT

The Requires

 

social cakephp hybridauth social authentication social signon multi provider

28/11 2016

dev-cake2

dev-cake2 http://github.com/ADmad/CakePHP-HybridAuth

A CakePHP plugin for using the HybridAuth social sign on library

  Sources   Download

MIT

The Requires

 

cakephp hybridauth social signon multi provider

28/11 2016

1.0.1

1.0.1.0 http://github.com/ADmad/CakePHP-HybridAuth

A CakePHP plugin for using the HybridAuth social sign on library

  Sources   Download

MIT

The Requires

 

cakephp hybridauth social signon multi provider

03/10 2016

4.0.4

4.0.4.0 http://github.com/ADmad/CakePHP-HybridAuth

A CakePHP plugin for using the HybridAuth social sign on library

  Sources   Download

MIT

The Requires

 

social cakephp hybridauth social authentication social signon multi provider

06/07 2016

4.0.3

4.0.3.0 http://github.com/ADmad/CakePHP-HybridAuth

A CakePHP plugin for using the HybridAuth social sign on library

  Sources   Download

MIT

The Requires

 

social cakephp hybridauth social authentication social signon multi provider

30/06 2016

4.0.2

4.0.2.0 http://github.com/ADmad/CakePHP-HybridAuth

A CakePHP plugin for using the HybridAuth social sign on library

  Sources   Download

MIT

The Requires

 

social cakephp hybridauth social authentication social signon multi provider

14/05 2016

4.0.1

4.0.1.0 http://github.com/ADmad/CakePHP-HybridAuth

A CakePHP plugin for using the HybridAuth social sign on library

  Sources   Download

MIT

The Requires

 

social cakephp hybridauth social authentication social signon multi provider

24/02 2016

4.0.0

4.0.0.0 http://github.com/ADmad/CakePHP-HybridAuth

A CakePHP plugin for using the HybridAuth social sign on library

  Sources   Download

MIT

The Requires

 

social cakephp hybridauth social authentication social signon multi provider

24/02 2016

3.0.2

3.0.2.0 http://github.com/ADmad/CakePHP-HybridAuth

A CakePHP plugin for using the HybridAuth social sign on library

  Sources   Download

MIT

The Requires

 

cakephp hybridauth social signon multi provider

02/08 2015

3.0.x-dev

3.0.9999999.9999999-dev http://github.com/ADmad/CakePHP-HybridAuth

A CakePHP plugin for using the HybridAuth social sign on library

  Sources   Download

MIT

The Requires

 

cakephp hybridauth social signon multi provider

02/08 2015

3.0.1

3.0.1.0 http://github.com/ADmad/CakePHP-HybridAuth

A CakePHP plugin for using the HybridAuth social sign on library

  Sources   Download

MIT

The Requires

 

cakephp hybridauth social signon multi provider

10/04 2015

3.0.0

3.0.0.0 http://github.com/ADmad/CakePHP-HybridAuth

A CakePHP plugin for using the HybridAuth social sign on library

  Sources   Download

MIT

The Requires

 

cakephp hybridauth social signon multi provider

10/04 2015

1.0.0

1.0.0.0 http://github.com/ADmad/CakePHP-HybridAuth

A CakePHP plugin for using the HybridAuth social sign on library

  Sources   Download

MIT

The Requires

 

cakephp hybridauth social signon multi provider