Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authentication using Doctrine 2 MongoDbODM Module

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?

like image 995
Shahbaz Avatar asked Apr 30 '26 00:04

Shahbaz


1 Answers

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,
    ));
}
like image 50
Shahbaz Avatar answered May 02 '26 15:05

Shahbaz