Commit 77c28e45 authored by Moreau Elise's avatar Moreau Elise
Browse files

check if user is authentified and exist

parent b758841b
<?php
declare(strict_types=1);
namespace App\Application\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Slim\Psr7\Response;
use Doctrine\ORM\EntityManager;
class AuthMiddleware implements Middleware
{
/**
* @var UserRepository
*/
protected $userRepository;
/**
* Constructor.
*
* @param EntityManager $em The entity manager
*/
public function __construct(EntityManager $em)
{
$this->userRepository = $em->getRepository('App\Domain\User\User');
}
/**
* {@inheritdoc}
*/
public function process(Request $request, RequestHandler $handler): ResponseInterface
{
if (isset($_SESSION['userId'])) {
$user = $this->userRepository->find($_SESSION['userId']);
if ($user === false) {
session_unset();
} else {
return $handler->handle($request);
}
}
$response = new Response();
return $response->withHeader('Location', '/login')->withStatus(302);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment