Social Logins in PHP with HybridAuth

Originally published at: http://www.sitepoint.com/social-logins-php-hybridauth/

A trend in many of today’s websites is a feature that allows users to sign in via their social network accounts. A classic example is the SitePoint community where users have the option to use their Facebook, Twitter, Google, Yahoo or GitHub account to log in without having to register an account.

In this tutorial, we will be learning about HybridAuth – a PHP library that takes the pain out of building a social login feature.

HybridAuth acts as an abstract API between your application and the various social APIs and identity providers.

Installation

The recommended way to install HybridAuth is via Composer. We’ll also use Slim as a foundation for our sample app.

{
    "require": {
        "slim/slim": "2.*",
        "hybridauth/hybridauth": "2.3.0"
    }
}

Using HybridAuth for Social Logins

To use HybridAuth, copy the config.php and index.php (HybridAuth Endpoint) files in /vendor/hybridauth/hybridauth/hybridauth to your project’s root folder.

Rename the index.php file to hybrid.php because index.php will be used by Slim framework for our demo application logic.

Populate the config.php file with your application’s (e.g. Facebook, Twitter application) credentials.

For example, If you want users to sign in to your website via Facebook, Google, and Twitter; your config file would look pretty much like this. My application URL is http://slim.local.

Continue reading this article on SitePoint

Very useful article!

There’s one thing which confuses me. In you code I see hybrid.php with
Hybrid_Endpoint::process();

and configs base_url points to this file. But in the text, nor in any other examples I could not find reference to this code.
Is this needed or not?

Thanks

Hybrid_Endpoint::process(); is a core hybrid method that calls the appropriate methods to process a social login authentication.

Since we are routing the auth request to hybrid.php, the method need to be in the file. For instance, when you download the hybridauth library, all examples are routend to index.php. open the file and you will see it inside.

Fatal error: Uncaught exception ‘Hybrid_Exception’ with message ‘Oophs. Error!’ in /home1/ericele/public_html/sitename.com/aaaaaslim/vendor/hybridauth/hybridauth/hybridauth/Hybrid/Endpoint.php:221 Stack trace: #0 /home1/ericele/public_html/sitename.com/aaaaaslim/vendor/hybridauth/hybridauth/hybridauth/Hybrid/Endpoint.php(124): Hybrid_Endpoint->authInit() #1 /home1/ericele/public_html/sitename.com/aaaaaslim/vendor/hybridauth/hybridauth/hybridauth/Hybrid/Endpoint.php(51): Hybrid_Endpoint->processAuthStart() #2 /home1/ericele/public_html/sitename.com/aaaaaslim/vendor/hybridauth/hybridauth/hybridauth/Hybrid/Endpoint.php(71): Hybrid_Endpoint->__construct(NULL) #3 /home1/ericele/public_html/sitename.com/aaaaaslim/hybrid.php(11): Hybrid_Endpoint::process() #4 {main} thrown in /home1/ericele/public_html/sitename.com/aaaaaslim/vendor/hybridauth/hybridauth/hybridauth/Hybrid/Endpoint.php on line 221

I liked this article however I dont use slim framework. Can you provide how to use hybridauth with simple PHP code ?

It’s almost the same - it’s identical up until the “Application logic” part which will differ for every non-Slim app. That’s where your own router and your own controller logic will be taking over, so we can’t help with that part

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.