Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel 5.x CSRF Middleware with custom redirect
<?php
namespace App\Http\Middleware;
use Closure;
use Redirect;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
class VerifyCsrfToken extends BaseVerifier
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
//
];
public function handle( $request, Closure $next )
{
if (
$this->isReading($request) ||
$this->runningUnitTests() ||
$this->shouldPassThrough($request) ||
$this->tokensMatch($request)
) {
return $this->addCookieToResponse($request, $next($request));
}
// redirect the user back to the last page and show error
return Redirect::back()->withErrors( ['Sorry, we could not verify your request. Please try again.'] );
}
}
@RatJantaraksa

This comment has been minimized.

Copy link

RatJantaraksa commented Oct 31, 2016

Work only login page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.