Use the HybridAuth-Library to create an absolute lightweight Authentication-Layer for your ZendFramework2-App
You can login with all supported SocialNetwork-Logins. The network and a user-object holding id, name, mail and language will be stored in the session. If you already have SocialNetwork users in your application you can use these to authorize your users.
- The hybridAuth-library. This lib uses the version 3!
- Zend Framework2 (well, obvious, isn't it?)
-
In your application.conf-file add the Module to the list of modules
-
Copy the file
vendor/org_heigl/hybridauth/config/autoload/module-orgHeiglHybridAuth.local.php
to your applicationsconfig/autoload
-directory and adapt as appropriate. -
Add this snippet to create a login-link
<?php $provider = "Twitter"; echo $this->hybridauthinfo($provider); ?>
-
After login you can access the user-info the following way:
// Need this block to autoload Hybrid_Auth dependencies to unserialize object stored in session use Hybridauth\Hybridauth; $config = $this->getServiceLocator()->get('Config'); $config = $config['OrgHeiglHybridAuth']; $hybridAuth = new Hybridauth($config['hybrid_auth']); // The name of the session-container can be changed in the config file! $container = new \Zend\Session\Container('orgheiglhybridauth'); if (! $container->offsetExists('authenticated')) { echo 'No user logged in'; } /** @var OrgHeiglHybridAuth\UserInterface $user */ $user = $container->offsetGet('user'); echo $user->getName(); // The name of the logged in user echo $user->getUID(); // The internal UID of the used service echo $user->getMail(); // The mail-address the service provides echo $user->getLanguage(); // The language the service provides for the user $service = $container->offsetGet('backend'); echo $service->id // Should print out the Name of the service provider.
This module is best installed using composer. For that, run the following command to add the library to your app:
composer require org_heigl/hybridauth
So you want it the hard way? Sure you don't want to give composer a try?
OK, you wanted it that way. But don't blame me!
I have to assume some things here:
- You have a dedicated
vendor
-Folder where you install all your external libraries. - Inside that vendor-Folder you have a subfolder for each vendor.
- Inside a vendor-subfolder you have subfolders for the actual library
(Yes, that's the way composer organizes the files!)
So your ZF-Library is installed inside
vendor/zendframework/zendframework
. - You have the ZF2-autoloader set up successfully.
So from there you'll have to follow these steps:
- Download the Hybridauth-library to a folder
vendor/hybridauth/hybridauth
- Download this library to a folder
vendor/org_heigl/hybridauth
- Run the script
vendor/org_heigl/hybridauth/bin/createAutoloadSupport.php
Now you should be up and running to follow the steps outlined in the [Usage]-section.
Note that you can either download the zip-files of the libraries or use the git submodule command to clone the
libs into the appropriate folders. You should not simply use git clone <library> <target>
as that might
interfere with your local git-repo (when you use one). The submodule approach makes Lib-updates easier bun can
end in a lot of headaches due to the caveats of the submodule-command! I can not provide you with support in that case!
Alternatively you can fork the project at github.