Question

I'm trying to set a login(Authentication) using Zend2 and DoctrineODMModule but I am getting an error. enter image description here

I have followed the tutorial to setup the Authentication of Zend2 with doctorineODMModule on github

any suggestion what I am doing wrong? or what I have to do?

Was it helpful?

Solution

i have done it in the following way. in doctrine mdule.config.php

'authentication'    => array(
    'odm_default'   => array(
    'object_manager'        => 'doctrine.documentmanager.odm_default',
    'identity_class'        => 'Admin\Document\User',
    'identity_property'     => 'username',
    'credential_property'   => 'password',
    ),
),

'odm_driver' => array(
    'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
    'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Document')
),
'odm_default' => array(
    'drivers' => array(
    __NAMESPACE__ . '\Document' => 'odm_driver'
    )
)

in Admin/Document/User.php created two methods getUsername and getPassword.

public function getUsername(){
    return $this->username;     
}

public function getPassword(){
    return $this->password;
}

created controller in index controller.php

public function loginAction(){
    $this->layout('layout/login-layout.phtml');
    $login_error=false;
    $loginForm = new LoginForm();
    if ($this->request->isPost())
    {
        $loginForm->setData($this->request->getPost());
        if ($loginForm->isValid())
        {
           // try {
            //  throw new \Exception("My exception");

            $data = $loginForm->getData();
            $authService = $this->getServiceLocator()
            ->get('doctrine.authenticationservice.odm_default');

            $adapter = $authService->getAdapter();
            $adapter->setIdentityValue($data['username']);  // i am using username
            $adapter->setCredentialValue(md5($data['password']));
            $authResult = $authService->authenticate();
            if ($authResult->isValid()) {
                $this->redirect()->toRoute('admin_index'); // or last viewed page
            }
            /*} catch (Exception $e) {
                echo "Caught exception $e\n";
                echo $e->getPrevious();
                $login_error=false;
                return new ViewModel(array(
                        'loginForm' => $loginForm,
                        'login_error' => $login_error,
                ));
                //exit;
            }/
            return array(
                    'loginForm' => $loginForm,
                    'errors' => 'username or password is not valid',
            );

            $this->redirect()->toRoute('admin_index');
        }  else {
        //
        // LOG Event ( login|password not valide )
        //
        //Zend\Debug\Debug::dump("not valid data");
        //Zend\Debug\Debug::dump($loginForm->getMessages());
            $login_error=true;
        }//* */
        }
    }
    //
    return new ViewModel(array(
            'loginForm' => $loginForm,
            'login_error' => $login_error,
    ));
}

OTHER TIPS

With the message you give,

A value for the identity was not provided prior to authentication with ObjectRepository authentication adapter

I'd say that either you didn't give the field to use as identity on you're User Document or during authentication process (in your action) you didn't populate the value for identity (aka login)

Please give more informations about your app (odm module configuration, Identity class...) to provide you a better help

as configuration you should have stg like :

...
'doctrine' => array(
    'driver' => array(
        __NAMESPACE__ . '_orm_driver' => array(
            'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
            'cache' => 'array',
            'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
        ),
        'orm_default' => array(
            'drivers' => array(
                __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_orm_driver'
            )
        ),
        __NAMESPACE__ . '_odm_driver' => array(
            'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
            'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Document')
        ),
        'odm_default' => array(
            'drivers' => array(
                __NAMESPACE__ . '\Document' => __NAMESPACE__ . '_odm_driver'
            )
        )
    ),
    'authentication' => array(
        'odm_default' => array(
            'objectManager' => 'doctrine.documentmanager.odm_default',
            'identityClass' => 'Application\Document\User',
            'identityProperty' => 'username',
            'credentialProperty' => 'password',
            'credentialCallable' => 'Application\Utils::hashPassword' // Not needed if you don't hash passwords
        ),
    ),
),
...

This was working great for some of my projects

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top