Skip to content

Commit

Permalink
Merge pull request #591 from leepeuker/improve-session-handling
Browse files Browse the repository at this point in the history
Improve session handling
  • Loading branch information
leepeuker authored Feb 27, 2024
2 parents 8485c83 + 26baaca commit 6c8f3dc
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 6 deletions.
2 changes: 0 additions & 2 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php declare(strict_types=1);

session_start();

/** @var DI\Container $container */

use Movary\HttpController\Web\ErrorController;
Expand Down
2 changes: 1 addition & 1 deletion settings/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function addWebRoutes(RouterService $routerService, FastRoute\RouteCollector $ro
$routes->add('POST', '/add-movie-to-watchlist', [Web\WatchlistController::class, 'addMovieToWatchlist'], [Web\Middleware\UserIsAuthenticated::class]);
$routes->add('GET', '/fetchMovieRatingByTmdbdId', [Web\Movie\MovieRatingController::class, 'fetchMovieRatingByTmdbdId'], [Web\Middleware\UserIsAuthenticated::class]);

$routerService->addRoutesToRouteCollector($routeCollector, $routes);
$routerService->addRoutesToRouteCollector($routeCollector, $routes, true);
}

function addApiRoutes(RouterService $routerService, FastRoute\RouteCollector $routeCollector) : void
Expand Down
3 changes: 2 additions & 1 deletion src/Domain/User/Service/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ public function logout() : void

public function setAuthenticationCookieAndNewSession(int $userId, string $token, DateTime $expirationDate) : void
{
session_regenerate_id();
$this->sessionWrapper->destroy();
$this->sessionWrapper->start();
setcookie(self::AUTHENTICATION_COOKIE_NAME, $token, [
'expires' => (int)$expirationDate->format('U'),
'path' => '/',
Expand Down
21 changes: 21 additions & 0 deletions src/HttpController/Web/Middleware/StartSession.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types=1);

namespace Movary\HttpController\Web\Middleware;

use Movary\Util\SessionWrapper;
use Movary\ValueObject\Http\Response;

class StartSession implements MiddlewareInterface
{
public function __construct(
private readonly SessionWrapper $sessionWrapper,
) {
}

public function __invoke() : ?Response
{
$this->sessionWrapper->start();

return null;
}
}
10 changes: 8 additions & 2 deletions src/Service/Router/RouterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
namespace Movary\Service\Router;

use FastRoute\RouteCollector;
use Movary\HttpController\Web;
use Movary\Service\Router\Dto\RouteList;

class RouterService
{
public function addRoutesToRouteCollector(RouteCollector $routeCollector, RouteList $routeList) : void
public function addRoutesToRouteCollector(RouteCollector $routeCollector, RouteList $routeList, bool $isWebRoute = false) : void
{
foreach ($routeList as $route) {
$middleware = $route->getMiddleware();
if ($isWebRoute === true) {
$middleware[] = Web\Middleware\StartSession::class;
}

$routeCollector->addRoute(
$route->getMethod(),
$route->getRoute(),
[
'handler' => $route->getHandler(),
'middleware' => $route->getMiddleware()
'middleware' => $middleware
],
);
}
Expand Down
22 changes: 22 additions & 0 deletions src/Util/SessionWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,29 @@ class SessionWrapper
{
public function destroy() : void
{
$_SESSION = array();

if (ini_get('session.use_cookies')) {
$sessionName = session_name();
if ($sessionName === false) {
throw new \RuntimeException('Could not get session name');
}

$params = session_get_cookie_params();

setcookie(
$sessionName,
'',
time() - 42000,
$params['path'],
$params['domain'],
$params['secure'],
$params['httponly'],
);
}

session_destroy();
session_regenerate_id();
}

public function find(string $key) : mixed
Expand Down

0 comments on commit 6c8f3dc

Please sign in to comment.