Skip to content

Instantly share code, notes, and snippets.

@niraj-shah
Last active October 31, 2016 04:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niraj-shah/d53048b5fbc75ffb89c6 to your computer and use it in GitHub Desktop.
Save niraj-shah/d53048b5fbc75ffb89c6 to your computer and use it in GitHub Desktop.
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
Copy link

Work only login page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment