# Problem in using the Identity Helper

**URL:** <https://discourse.laminas.dev/t/problem-in-using-the-identity-helper/875>\
**Category:** Components & MVC\
**Tags:** zend-view\
**Created:** [December 29, 2018, 9:34am UTC](https://discourse.laminas.dev/t/problem-in-using-the-identity-helper/875 "2018-12-29T09:34:17Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [December 30, 2018, 8:00am UTC](https://discourse.laminas.dev/t/problem-in-using-the-identity-helper/875/5 "2018-12-30T08:00:12Z")

</div>

Here are the key steps:

1. Create the factory for the authentication service.

```php
namespace MyModule;

use Interop\Container\ContainerInterface;
use Zend\Authentication\Adapter\DbTable\CredentialTreatmentAdapter;
use Zend\Authentication\AuthenticationService;
use Zend\Db\Adapter\AdapterInterface;
use Zend\ServiceManager\Factory\FactoryInterface;

class AuthenticationServiceFactory implements FactoryInterface
{
    public function __invoke(
        ContainerInterface $container,
        $requestedName,
        array $options = null
    ) {
        // Database adapter
        /** @var \Zend\Db\Adapter\Adapter $dbAdapter */
        $dbAdapter = $container->get(AdapterInterface::class);

        // Auth adapter
        $authAdapter = new CredentialTreatmentAdapter(
            $dbAdapter,
            'users', // table name
            'username', // identity column
            'password', // credential column
            'SHA1(?)' // credential treatment
        );

        return new AuthenticationService(null, $authAdapter);
    }
}

```

1. Register the factory:

```php
'service_manager' => [
    'factories' => [
        Zend\Authentication\AuthenticationServiceInterface::class => MyModule\AuthenticationServiceFactory::class,
    ],
],

```

Then use it in your controller for login:

```php
public function loginAction()
{
    // …
    
    if ($this->getRequest()->isPost()) {

        // Get form data…
        
        // Get authentication service
        /** @var \Zend\Authentication\AuthenticationService $authenticationService */
        $authenticationService = $this->plugin('identity')->getAuthenticationService();

        // Set identity and credential for auth service
        /* @var $adapter \Zend\Authentication\Adapter\DbTable\AbstractAdapter */
        $adapter = $authenticationService->getAdapter();
        $adapter->setIdentity($data['location'])
                ->setCredential($data['password']);
        
        // Validation
        if ($authenticationService->authenticate()->isValid()) {
            // Get result
            /** @var \Zend\Authentication\Storage\Session $storage */
            $storage = $authenticationService->getStorage();
            $result = $adapter->getResultRowObject(
                [
                    'id',
                    'roleid',
                    'username',
                    'nickname',
                ]
            );

            // Create user
            $user = new User($result->id);

            // Write to session
            $storage->write($user);
            
            // Redirect…
        }
    }
    
    // …
}

```

and logout:

```php
public function logoutAction()
{
    // Has identity?
    if (! $this->identity()) {
        return $this->redirect()->toRoute('…');
    }

    // Get authentication service
    /** @var \Zend\Authentication\AuthenticationService $authenticationService */
    $authenticationService = $this->plugin('identity')->getAuthenticationService();

    // Clear identity
    $authenticationService->clearIdentity();

    // Set success message
    $this->flashMessenger()->addSuccessMessage('Logout successful…');

    return $this->redirect()->toRoute('…');
}

```

And in your view scripts:

```php
var_dump($this->identity()); // User::class (after login)

```

## Attention

The above code has not been tested, it only shows the main process and needs to be customized!

---

_[View the full topic](https://discourse.laminas.dev/t/problem-in-using-the-identity-helper/875)._
