Add the session middleware for starting the session on every request.

This commit is contained in:
Dave Smith-Hayes 2024-11-24 21:22:49 -05:00
parent 55d8f19ec9
commit 7b54952b55
6 changed files with 41 additions and 0 deletions

View File

@ -6,6 +6,7 @@ use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig; use Slim\Views\Twig;
use Slim\Routing\RouteContext; use Slim\Routing\RouteContext;
use Odan\Session\SessionInterface;
abstract class Controller abstract class Controller
{ {

View File

@ -15,6 +15,9 @@ class DashboardPage extends Controller
public function handle(): Response public function handle(): Response
{ {
if (!$this->session->has("logged_in")) {
}
// get the user details // get the user details
// get the channels // get the channels
return $this->render('dashboard.twig', [ 'session' => $this->session ]); return $this->render('dashboard.twig', [ 'session' => $this->session ]);

View File

@ -34,6 +34,7 @@ class LoginUserAction extends Controller
} }
// start the session // start the session
$this->session->getFlash()->add('success', "Successfully loggedin.");
$this->session->set('user', [ 'id' => $user->getId(), 'authenticated' => true ]); $this->session->set('user', [ 'id' => $user->getId(), 'authenticated' => true ]);
return $this->redirect('/dashboard', 302); return $this->redirect('/dashboard', 302);
} }

View File

@ -19,6 +19,7 @@ class AuthenticatedMiddleware implements MiddlewareInterface
public function process(Request $request, RequestHandler $handler): Response public function process(Request $request, RequestHandler $handler): Response
{ {
if ($this->session->has('user')) { if ($this->session->has('user')) {
dd($this->session->get('user'));
if ($this->session->get('user')['authenticated']) { if ($this->session->get('user')['authenticated']) {
return $handler->handle($request); return $handler->handle($request);
} }

View File

@ -0,0 +1,29 @@
<?php
namespace Slovocast\Middleare;
use Odan\Session\SessionManagerInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
/**
* Start a session on every single request, regardless. There may be a reason
* to never start a session but I highly doubt it.
*/
class SessionMiddleware implements MiddlewareInterface
{
public function __construct(
private SessionManagerInterface $sessionManager
) { }
public function process(Request $request, RequestHandler $handler): Response
{
if (!$this->sessionManager->isStarted()) {
$this->sessionManager->start();
}
return $handler->handle($request);
}
}

View File

@ -10,6 +10,7 @@ use Psr\Container\NotFoundExceptionInterface;
use Slim\App; use Slim\App;
use Slim\Views\Twig; use Slim\Views\Twig;
use Slim\Views\TwigMiddleware; use Slim\Views\TwigMiddleware;
use Slovocast\Middleare\SessionMiddleware;
use Twig\Error\LoaderError; use Twig\Error\LoaderError;
class Middlewares class Middlewares
@ -29,6 +30,11 @@ class Middlewares
*/ */
$config = $container->get('config'); $config = $container->get('config');
/**
* Global session start
*/
$app->add(SessionMiddleware::class);
// Twig // Twig
$templateCache = false; $templateCache = false;
$twig = Twig::create(APP_TEMPLATES_DIR, [ 'cache' => $templateCache ]); $twig = Twig::create(APP_TEMPLATES_DIR, [ 'cache' => $templateCache ]);