Last active
January 10, 2022 17:32
-
-
Save geerteltink/7d42a7e69f75b328b068b39678b3fbb3 to your computer and use it in GitHub Desktop.
zend-expressive-authentication with doctrine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
use App\Infrastructure\Repository\DoctrineUserRepositoryFactory; | |
use Zend\Expressive\Authentication\AuthenticationInterface; | |
use Zend\Expressive\Authentication\Session\PhpSessionFactory; | |
use Zend\Expressive\Authentication\UserRepositoryInterface; | |
return [ | |
'dependencies' => [ | |
'factories' => [ | |
AuthenticationInterface::class => PhpSessionFactory::class, | |
UserRepositoryInterface::class => DoctrineUserRepositoryFactory::class, | |
], | |
], | |
'authentication' => [ | |
'username' => 'email', | |
'password' => 'password', | |
'redirect' => '/signin', | |
], | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Infrastructure\Repository; | |
use App\Domain\User\User; | |
use App\Domain\User\UserRepository; | |
use Doctrine\Common\Collections\Criteria; | |
use Doctrine\Common\Persistence\ObjectRepository; | |
use Doctrine\ORM\Tools\Pagination\Paginator; | |
use Zend\Expressive\Authentication\UserInterface; | |
class DoctrineUserRepository implements UserRepository | |
{ | |
/** | |
* @var ObjectRepository | |
*/ | |
private $repository; | |
public function __construct( | |
ObjectRepository $repository | |
) { | |
$this->repository = $repository; | |
} | |
public function authenticate(string $identity, string $password = null) : ?UserInterface | |
{ | |
/** @var User $user */ | |
$user = $this->repository->findOneBy([ | |
'email' => $identity, | |
]); | |
if ($user && $user->verifyPassword($password)) { | |
return $user; | |
} | |
return null; | |
} | |
public function getRolesFromUser(string $identity) : array | |
{ | |
/** @var User $user */ | |
$user = $this->repository->findOneBy([ | |
'email' => $identity, | |
]); | |
if (! $user) { | |
return []; | |
} | |
return $user->getUserRoles(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Domain\User; | |
use Zend\Expressive\Authentication\UserRepositoryInterface; | |
interface UserRepository extends UserRepositoryInterface | |
{ | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Will you please suggest or refer for mezzio if you have any ?.