Last active
October 31, 2016 04:30
-
-
Save niraj-shah/d53048b5fbc75ffb89c6 to your computer and use it in GitHub Desktop.
Laravel 5.x CSRF Middleware with custom redirect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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.'] ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Work only login page.