Zend Expressive Authentication with doctrine

Greetings.

i am trying to create a login page with zend auth. Therefore iam reading the tutorial on: https://samsonasik.wordpress.com/2018/01/12/create-login-functionality-in-expressive-3/

<?php
// config/autoload/dependencies.global.php
return [

'dependencies' => [
    'aliases' => [
        // ...
        Zend\Expressive\Authentication\UserRepositoryInterface::class =>
            Zend\Expressive\Authentication\UserRepository\PdoDatabase::class
    ],

    'factories' => [
        // ...
        Zend\Expressive\Authentication\AuthenticationInterface::class =>
            Zend\Expressive\Authentication\Session\PhpSessionFactory::class,
    ],
    // ...
],

];

In this example the “Userrepository” is mapped to a PDODatabase. My Problem is that iam using doctrine, so i do not have a PDODatabase config.

How do i use a doctrine User Entity/Repository and map it to the “Userrepository”. I would appreciate any help/examples.

Hi. The zendframework/zend-expressive-authentication package comes with a basic PDO implementation for db access. Since you’re using Doctrine, you’ll need to write your own. On the most basic level, your Doctrine user entity will need to implement the Zend\Expressive\Authentication\UserInterface and your Doctrine user repository will need to implement Zend\Expressive\Authentication\UserRepositoryInterface. You’ll then alias those in your config to your user entity and user repository respectively.

1 Like

Thank you. i got it working now :slight_smile:

1 Like

Can you share your UserRepository for sample where i got it done with PDODatabase but not able to do with doctrine

Hi. i dont have access to the code.
make sure your UserRepository implements the Zend\Expressive\Authentication\UserRepositoryInterface and is aliased in your config.

return [
    // ...
    'dependencies' => [
        // ...
        'aliases' => [
            // ...
            UserRepositoryInterface::class => YourUserRespository::class
        ]
    ]
];

At the most basic level you just need an authentication method that returns an Object implementing the User interface when the authentication was successfull.

to give you an example. In this case the Mezzio\Authentication\DefaultUser is used. But you could also implement the Mezzio\Authentication\UserInterface on your UserEntity and return it directly. hope it helps

public function authenticate(string $credential, ?string $password = null): ?UserInterface {
     //pseudo code
     $user = $this->findByUsername($credential);
     if (password_verify($user->getPassword(), $password)) {
         return ($this->userFactory)(
                $credential,
                [],
                []
            );
     }
}