slimphp / Slim-Csrf

Slim Framework CSRF protection middleware
MIT License
338 stars 58 forks source link

Add csrf-middleware conditionally #163

Closed trendschau closed 2 years ago

trendschau commented 2 years ago

Not sure if it is the place here, but I am looking for a way to add the csrf-middleware conditionally in slim 4 because my application does not start sessions in all cases.

I start the session in a middleware conditionally:

# add session
$app->add(new CreateSession($session_segments, ltrim($routepath, '/') ));

Then I add csrf protection to the container with the next middleware in case a session has been started:

# if session add csrf protection
$app->add(new CsrfProtection($container, $responseFactory));

And now instead of ...

// Register Middleware To Be Executed On All Routes
$app->add('csrf');

... I want to add it only if session has started. Maybe complete misconception on my side ?

trendschau commented 2 years ago

sorry for that question, it does not make sense that way and I will close it.