How to check if the user is logged in

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to check if the user is logged in

Khurram
This post has NOT been accepted by the mailing list yet.
After reading lots of blogs and documentation i found out that getControllerConfig() is the function which can perform pre -instantiation logic before the controller gets constructed. All i wanted to do is bascially when ever some one enter on the /Dashboard /Dashboard/edit/11 /Dashboard/ /Dashboard/edit /Dashboard/add etc etc. I want to check $auth->hasIdentity() then display the contents otherwise redirect the user to login page. All the solutions i have come across even the ZfcUser they are checking the $auth->hasIdentity() inside the controller actions which i do not want to do further more ZfcUser is too much for my small redirect problem. I want to handle this from the top level and redirect the user. Unfortunately inside the getControllerConfig() i can not do redirect because the Event object has not yet been passed to it so no use. I also overloaded the onDispatch inside my DashboardController.php but it does not work as well niether i can do it on preDispatch inside the Module.php Can some one please throw a small code snippet or deeper explanation of how to achieve it in zf2. Below is my Module.php https://github.com/khurramijazm/zf2/blob/master/module/Admin/Module.php
Reply | Threaded
Open this post in threaded view
|

Re: How to check if the user is logged in

samsonasik
This post has NOT been accepted by the mailing list yet.
change priority with minus :

   public function onBootstrap(MvcEvent $e)
  {
      $eventManager->attach('dispatch', array($this, 'checkLogin'), -3);
   }

    public function checkLogin(MvcEvent $e)
    {
       $sm = $e->getApplication()->getServiceManager();
        if (!$sm->get('Admin\Authentication\Service')->hasIdentity()) {
               //redirect to login page
       }
   }