1. Go to this page and download the library: Download sirix/mezzio-authentication library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Sirix\Mezzio\Authentication\Middleware\AuthenticateMiddleware;
$app->get('/api/me', [
AuthenticateMiddleware::class,
ProfileHandler::class,
], 'profile');
use Sirix\Mezzio\Authentication\Attribute\Authenticated;
use Sirix\Mezzio\Routing\Attributes\Attribute\Get;
#[Get('/api/me', name: 'profile')]
#[Authenticated]
final class ProfileHandler implements RequestHandlerInterface
{
public function handle(ServerRequestInterface $request): ResponseInterface
{
// User is authenticated
}
}
use Sirix\Mezzio\Authentication\Contract\AuthManagerInterface;
$manager->login(['userId' => 1, 'roles' => ['admin']]);
$manager->context($request); // AuthContextInterface from request attributes
$manager->check($request); // true/false based on request auth context
$manager->guest($request); // true/false based on request auth context
$manager->actor($request); // ActorInterface from request auth context
$manager->token($request); // TokenInterface from request auth context
$response = $manager->logout($request, $response); // detaches token from transport
use Sirix\Mezzio\Authentication\Contract\ActorInterface;
$actor->getRoles(); // ['admin', 'editor']
use Sirix\Mezzio\Authentication\Contract\AuthActorProviderInterface;
use Sirix\Mezzio\Authentication\Contract\ActorInterface;
use Sirix\Mezzio\Authentication\Contract\TokenInterface;
final readonly class MyActorProvider implements AuthActorProviderInterface
{
public function getActor(TokenInterface $token): ?ActorInterface
{
// Custom logic to resolve actor from token
}
}
use Sirix\Mezzio\Authentication\Contract\TokenStorageInterface;
final readonly class RedisTokenStorage implements TokenStorageInterface
{
// implement create(), load(), delete()
}
use Sirix\Mezzio\Authentication\Contract\TokenTransportInterface;
final readonly class QueryParamTransport implements TokenTransportInterface
{
// implement fetch(), attach(), detach()
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.