TokenMismatchException in VerifyCsrfToken.php line 53 in Laravel 5.1
Edited:
Since you are using Form builder remove this from your form. Laravel form builder automatically adds a hidden token field to your form when you do Form::open()
So remove this line:
<input type="hidden" name="_token" value="{{ csrf_token() }}">
Well I think all missed the CSRF Token creation while logout!
As I have solved out the problem.
Just add below code to the header.
<meta name="csrf-token" content="{{ csrf_token() }}">
<script type=text/javascript>
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
</script>
And if you use {!!Form::open()!!}
it will automatically create the token. Otherwise you can use
<input type="hidden" name="_token" id="_token" value="{!! $csrf_token !!}}" />
or
{!! csrf_field() !!}
just immediate form open.
Most importantly use return Redirect::to('');
on controller function or a page reload or ajax reload that the token can be created!
Like:
public function logout() {
Session::flush();
Auth::logout();
return Redirect::to('/');
}
For ensure the token properly created or not check "view page source" on browser and it will shows like:
<meta name="csrf-token" content="TbgWTQZhTv0J4eFBQNU4rlM3jOlmBeYlTgf0waZB">
<script type=text/javascript>
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
</script>
<form method="POST" action="/login-process" accept-charset="UTF-8" class="form-inline"><input name="_token" type="hidden" value="TbgWTQZhTv0J4eFBQNU4rlM3jOlmBeYlTgf0waZB">
I think it might solve the problem as it worked for me!