Search code examples
zend-framework2zend-db

Zend 2 After login how to set user


I have the following code and which works, but now the next step.

How and where do i have to set a session so the script "sees" that the user is already logged in?

if ($form->isValid()) {
    $securePass = $this->getUsersTable()->getUserByUsername( $this->params()->fromPost('username') );       
    if( $securePass ){   
        $bcrypt = new Bcrypt();
        if ($bcrypt->verify( $this->params()->fromPost('password') , $securePass->password)) {

            $sm          = $this->getServiceLocator();
            $dbAdapter   = $sm->get('Zend\Db\Adapter\Adapter');
            $authAdapter = new AuthAdapter(
                    $dbAdapter,
                    'users',
                    'username',
                    'password'
                    );
            $authAdapter
                ->setIdentity($securePass->username)
                ->setCredential($securePass->password);                           

            $result = $authAdapter->authenticate($authAdapter);
            echo $result->getIdentity() . "\n\n";
        } 
        else {

        }

Solution

  • LoginController.php

    if ($form->isValid()) {
        $securePass = $this->getUsersTable()->getUserByUsername( $this->params()->fromPost('username') );       
        if( $securePass ){   
            $bcrypt = new Bcrypt();
            if ($bcrypt->verify( $this->params()->fromPost( 'password' ) , $securePass->password ) ) {
    
                $sm          = $this->getServiceLocator();
                $dbAdapter   = $sm->get('Zend\Db\Adapter\Adapter');
                $authAdapter = new AuthAdapter(
                    $dbAdapter,
                    'users',
                    'username',
                    'password'
                );
                $authAdapter->setIdentity($securePass->username)
                ->setCredential($securePass->password);                           
                $result = $authAdapter->authenticate($authAdapter);
    
                $sesssionData = $authAdapter->getResultRowObject();
    
                $auth = new AuthenticationService();
                $storage = $auth->getStorage();
                $storage->write($sesssionData);
    
                return $this->redirect()->toRoute('user_list');
            }
        }
    }
    
    
    
    public function onBootstrap(MvcEvent $e)
    {
    
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
        $app            = $e->getParam('application');
        $app->getEventManager()->attach('render', array($this, 'setLayoutTitle'));
    $eventManager->attach(MvcEvent::EVENT_DISPATCH, array($this, 'checkLogin'));
    }
    
    public function checkLogin(MvcEvent $e)
    {
        $iden = new AuthenticationService();
        if( $iden->getIdentity() === NULL ){
            $matches    = $e->getRouteMatch();
            $controller = $matches->getParam('controller');
            $getController = explode( '\\', $controller );  
    
            if( isset( $getController[2] ) && $getController[2] != 'Login' ){
                $controller = $e->getTarget();
                return $controller->plugin('redirect')->toRoute('login');
            }
        }   
    }