Skip to content

Commit 94dea9a

Browse files
committed
fixed csrf error page
1 parent a624417 commit 94dea9a

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

app/Middlewares/CsrfViewMiddleware.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,33 @@ public function __invoke($request, $response, $next){
2121

2222
//this can be access from the view with {{ csrf.field | raw }}
2323

24+
if (false === $request->getAttribute('csrf_status')) {
25+
26+
// successfully passed CSRF check
27+
28+
// display suitable error here
29+
$route = $request->getAttribute('route');
30+
31+
// return NotFound for non existent route
32+
if (empty($route->getName())) {
33+
//throw new NotFoundException($request, $response);
34+
return $response->write("CSRF error: return to the previous page, refresh it, then retry");
35+
36+
}
37+
38+
$name = $route->getName();
39+
$groups = $route->getGroups();
40+
$methods = $route->getMethods();
41+
$arguments = $route->getArguments();
42+
43+
44+
return $response->withRedirect($this->container->router->pathFor($name));
45+
46+
47+
48+
} else {
49+
50+
}
2451

2552
//the below must be done in all middlewares
2653
$response = $next($request, $response);

config/ContainerConfig.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@
2424
};
2525

2626
$container['csrf'] = function ($container){
27-
return new \Slim\Csrf\Guard;
27+
//return new \Slim\Csrf\Guard;
28+
29+
$guard = new \Slim\Csrf\Guard();
30+
$guard->setFailureCallable(function ($request, $response, $next) {
31+
$request = $request->withAttribute("csrf_status", true); //set to false if you dont want persistent tokens
32+
return $next($request, $response);
33+
});
34+
return $guard;
2835
};
2936

3037

0 commit comments

Comments
 (0)