0

Im new to zf2. zfcuser is setup as in the installation guide. Im able to register, login and logout.

I had created modules for frontend and backend. Im trying to check if user is login for the backend - admin and all the child modules.

I tried to include

$sm  = $app->getServiceManager();
        $auth = $sm->get('zfcuser_auth_service');
        if (!$auth->hasIdentity()) {
            //redirect to login page
        } 

in my admin/module.php function onBootStrap

it did check for login, BUT not only for the admin, is for the entire modules including the frontend.

I just need to check login for the admin modules, and all the child modules of admin.

Couldn't figure out how. Please help

Ninjoe Quah
  • 399
  • 6
  • 24

1 Answers1

0

In you module.config.php

<?php

namespace AdminModule;
use Zend\Authentication\AuthenticationService;

return array(
    __NAMESPACE__ => array(
        'params' => array(),
        'service_manager' => array(
            'invokables' => array(
               'Zend\Authentication\AuthenticationService' => 'Zend\Authentication\AuthenticationService',
            ),
        ),
        'services' => array(
            // Keys are the service names
            // Values are objects
            'Auth' => new AuthenticationService(),
        ),
    ),
);

Example in your controller/Manager

    $this->auth = new AuthenticationService();

    if($this->auth->hasIdentity()){
        $this->userid = $this->auth->getIdentity();
    }

You can use a factory too.

Example in your controller

using tools from ZfcUser module

  $this->zfcUserAuthentication()->hasIdentity()
Remi Thomas
  • 1,528
  • 1
  • 15
  • 25