grocy/middleware/SessionAuthMiddleware.php
2018-04-14 11:10:38 +02:00

27 lines
626 B
PHP

<?php
namespace Grocy\Middleware;
use \Grocy\Services\SessionService;
class SessionAuthMiddleware extends BaseMiddleware
{
public function __invoke(\Slim\Http\Request $request, \Slim\Http\Response $response, callable $next)
{
$route = $request->getAttribute('route');
$routeName = $route->getName();
$sessionService = new SessionService();
if ((!isset($_COOKIE['grocy_session']) || !$sessionService->IsValidSession($_COOKIE['grocy_session'])) && $routeName !== 'login')
{
$response = $response->withRedirect('/login');
}
else
{
$response = $next($request, $response);
}
return $response;
}
}