Add the session middleware for starting the session on every request.
This commit is contained in:
parent
55d8f19ec9
commit
7b54952b55
@ -6,6 +6,7 @@ use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Slim\Views\Twig;
|
||||
use Slim\Routing\RouteContext;
|
||||
use Odan\Session\SessionInterface;
|
||||
|
||||
abstract class Controller
|
||||
{
|
||||
|
@ -15,6 +15,9 @@ class DashboardPage extends Controller
|
||||
|
||||
public function handle(): Response
|
||||
{
|
||||
if (!$this->session->has("logged_in")) {
|
||||
|
||||
}
|
||||
// get the user details
|
||||
// get the channels
|
||||
return $this->render('dashboard.twig', [ 'session' => $this->session ]);
|
||||
|
@ -34,6 +34,7 @@ class LoginUserAction extends Controller
|
||||
}
|
||||
|
||||
// start the session
|
||||
$this->session->getFlash()->add('success', "Successfully loggedin.");
|
||||
$this->session->set('user', [ 'id' => $user->getId(), 'authenticated' => true ]);
|
||||
return $this->redirect('/dashboard', 302);
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ class AuthenticatedMiddleware implements MiddlewareInterface
|
||||
public function process(Request $request, RequestHandler $handler): Response
|
||||
{
|
||||
if ($this->session->has('user')) {
|
||||
dd($this->session->get('user'));
|
||||
if ($this->session->get('user')['authenticated']) {
|
||||
return $handler->handle($request);
|
||||
}
|
||||
|
29
app/src/Middleware/SessionMiddleware.php
Normal file
29
app/src/Middleware/SessionMiddleware.php
Normal 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);
|
||||
}
|
||||
}
|
@ -10,6 +10,7 @@ use Psr\Container\NotFoundExceptionInterface;
|
||||
use Slim\App;
|
||||
use Slim\Views\Twig;
|
||||
use Slim\Views\TwigMiddleware;
|
||||
use Slovocast\Middleare\SessionMiddleware;
|
||||
use Twig\Error\LoaderError;
|
||||
|
||||
class Middlewares
|
||||
@ -29,6 +30,11 @@ class Middlewares
|
||||
*/
|
||||
$config = $container->get('config');
|
||||
|
||||
/**
|
||||
* Global session start
|
||||
*/
|
||||
$app->add(SessionMiddleware::class);
|
||||
|
||||
// Twig
|
||||
$templateCache = false;
|
||||
$twig = Twig::create(APP_TEMPLATES_DIR, [ 'cache' => $templateCache ]);
|
||||
|
Loading…
Reference in New Issue
Block a user