PHP code example of mxmz / lfj-opauth

1. Go to this page and download the library: Download mxmz/lfj-opauth library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mxmz / lfj-opauth example snippets



return array(
    'modules' => array(
        'LfjOpauth',
        'Application',
    ),
    // more code
);

$settings = array(
    'security_salt' => 'Some random text',
    'Strategy' => array(
        'Facebook' => array(
            'app_id' => 'facebook application id',
            'app_secret' => 'facebook application secret',
            'scope' => 'email,user_relationships',
        ),
        'second_facebook_app' => array(
            'app_id' => 'another facebook application id',
            'app_secret' => 'another facebook application secret',
            'scope' => 'email,user_relationships',
            'strategy_class' => 'Facebook',
            'strategy_url_name' => 'second_facebook_app'
        )
    ),
    'check_controller_enabled' => false
);

return array('lfjopauth' => $settings);

namespace Application;

use Zend\EventManager\EventInterface;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);

        $sharedEventManager = $eventManager->getSharedManager();

        $sharedEventManager->attach('LfjOpauth\Service\OpauthService', \LfjOpauth\LfjOpauthEvent::EVENT_LOGIN_CALLBACK, function(EventInterface $e) {

            /** @var \Zend\Authentication\Result $result */
            $authenticationResult = $e->getParam('authenticationResult');

            /** @var \Zend\Authentication\AuthenticationService $authenticationService */
            $authenticationService = $e->getParam('authenticationService');

            /** @var \LfjOpauth\Service\OpauthService $target */
            $target = $e->getTarget();

            $provider = $e->getParam('provider');

            /*
            var_dump(get_class($e->getTarget()));
            var_dump($e->getParam('provider'));
            var_dump('$authenticationResult->isValid()', $authenticationResult->isValid());
            var_dump('$authenticationService->hasIdentity()', $authenticationService->hasIdentity());
            var_dump('$authenticationService->getIdentity()', $authenticationService->getIdentity());
            var_dump('$authenticationResult->getCode()', $authenticationResult->getCode());
            var_dump('$authenticationResult->getIdentity()', $authenticationResult->getIdentity());
            var_dump('$authenticationResult->getMessages()', $authenticationResult->getMessages());
            */

        }, 100);
    }

return array(
    'router' => array(
        'routes' => array(
            'custom_lfjopauth_login' => array(
                'type'    => 'Segment',
                'options' => array(
                    'route'    => '/custom/login/[:provider[/:oauth_callback]]',
                    'constraints' => array(
                        'provider'       => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'oauth_callback' => '[a-zA-Z][a-zA-Z0-9_-]*'
                    ),
                    'defaults' => array(
                        'controller'    => 'custom-auth',
                        'action'        => 'redirectAndReturn'
                    )
                )
            ),
            'custom_lfjopauth_callback' => array(
                'type'    => 'Segment',
                'options' => array(
                    'route'    => '/custom/callback/[:provider]',
                    'constraints' => array(
                        'provider'  => '[a-zA-Z][a-zA-Z0-9_-]*'
                    ),
                    'defaults' => array(
                        'controller'    => 'custom-auth',
                        'action'        => 'callback'
                    )
                )
            ),
            // more code
        )
    )
);

// [...]
class AuthController extends AbstractActionController
{
    public function redirectAndReturnAction()
    {
        // if user is not logged in
        if (!$this->auth()->hasIdentity())
        {
       	    $provider = $this->params()->fromRoute('provider');
       	    $oauth_callback = $this->params()->fromRoute('oauth_callback');
            $opauth_service = $this->getServiceLocator()->get('opauth_service');

            // set custom login and callback routes
            $opauth_service->setLoginUrlName('custom_lfjopauth_login');
            $opauth_service->setCallbackUrlName('custom_lfjopauth_callback');

            return $opauth_service->redirect($provider, $oauth_callback);
        }

        return $this->redirect()->toRoute('somewhere_over_the_rainbow');
    }

    public function callbackAction()
    {
        // if user is not logged in
        if (!$this->auth()->hasIdentity())
        {
       	    $provider = $this->params()->fromRoute('provider');
       	    $opauth_service = $this->getServiceLocator()->get('opauth_service');

            // set custom login and callback routes
            $opauth_service->setLoginUrlName('custom_lfjopauth_login');
            $opauth_service->setCallbackUrlName('custom_lfjopauth_callback');

            $opauth_service->callback($provider);
        }
	
        return $this->redirect()->toRoute('somewhere_else_over_the_rainbow');
    }
}