Laravel 5.3 - TokenMismatchException on every post request - laravel

I was working on my project normally and when i hit refresh to see a change that i just made in one of the html pages, the page showed a text on top left "Redirecting to: http://localhost:8888/xxx ". It redirected me to the login page. When i clicked login, the 'TokenMismatchException' error showed:
The login form has the hidden input _token , header has it too. As i said everything was fine. I have been working on this project for 2 months. Is this related to files permissions ?
Here is the login form.
<form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-7">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-7">
<input id="password" type="password" class="form-control" name="password" required>
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Login
</button>
<a class="btn btn-link" href="{{ url('/password/reset') }}">
Forgot Your Password?
</a>
</div>
</div>
</form>
Thanks :)

After i installed a fresh laravel copy i started to paste my old files to see where would regenerate the mismatch token problem.
It went down to the routes/web.php file. Something there was causing the error. It was a space before the opening tag of php on routes/web.php file.
Something that did not caught my eye. Like that the project was working fine but suddenly did not anymore.
As i read , the space is considered an output. Check this explanation :
https://stackoverflow.com/a/4345822/6634389

Why don't you use {{ csrf_field() }} or try with that?

Run:
php artisan cache:clear; composer dump-autoload; composer clear-cache
Refresh the browser, make sure you generated the key for your app. This should work. For me it did.

You are refreshing your page, so each time the pages load with same csrf-token. So when you are trying to post your data, laravel refuses it and provide error message due to each request should have a unique csrf-token. You can read about this herelaravekl csrf protection
Either you should again reload your page or you should disable the csrf protection by commenting it inside the
App=>Http=>Kernel.php
inside the $middlewareGroups=>'web'
\App\Http\Middleware\VerifyCsrfToken::class, //comment this line

I was having the same issue, I solved it by using following in my view
<input type="hidden" name="_token" value="{{ session()->getToken() }}">

Related

Method 'POST' error 419 on local server Laravel

When i submit a post form on my page it doesn't work, it redirects me on the action route with error 419, this is an example of my form:
<form action="{{route('client.login')}}" method="POST">
#csrf
#method('POST')
<h4 class="login-title">Login</h4>
<div class="login-form">
<div class="row">
<div class="col-md-12 col-12 mb--20">
<label>Email*</label>
<input class="mb-0" type="email" name="email" value="{{ old('email') }}">
</div>
<div class="col-12 mb--20">
<label>Password</label>
<input class="mb-0" type="password" autocomplete="current-password" name="password"
value="{{ old('password') }}">
</div>
<div class="col-md-12">
<div class="d-flex align-items-center flex-wrap">
<button type="submit" class="btn btn-black me-3">Login</button>
<div class="d-inline-flex align-items-center">
<input type="checkbox" id="remember" name="remember" class="mb-0 me-1">
<label for="remember" class="mb-0 font-weight-400">Ricordami</label>
</div>
</div>
#if (Route::has('password.request'))
<p>Password dimenticata?</p>
#endif
</div>
</div>
</div>
</form>
I've checked the csrf tokens, and they match.
I've the exact same code on my server-side files and they work perfectly, but doesn't work on my local server.
I can't find anywhere the log of this error.
EDIT:
My issue was in the .env file, I’ve written a ; rather than a :
Remove #method('POST') this line and try because you don't need to mention method="POST", you already mentioned method in form tag.
Welcome you in advance.
Try to add <meta name="csrf-token" content="{{ csrf_token() }}"> in the head of app.blade.php file

This password reset token is invalid while trying to reset password in laravel

I am having issue in my password reset and i am getting the error of this password reset token is invalid i am unable to solve this issue:
My Controller:
class ResetPasswordController extends Controller
{
use ResetsPasswords;
}
My Routes:
\Illuminate\Support\Facades\Auth::routes();
Route::get('password/reset/{token}', 'Auth\ResetPasswordController#showResetForm');
Route::post('password/reset', 'Auth\ResetPasswordController#reset')->name('password.request');
And my View:
<form class="form-horizontal" method="POST" action="{{ route('password.request') }}">
{{ csrf_field() }}
<input type="hidden" name="token" value="{{ $token }}">
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ $email or old('email') }}" required autofocus>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
<label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
#if ($errors->has('password_confirmation'))
<span class="help-block">
<strong>{{ $errors->first('password_confirmation') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Reset Password
</button>
</div>
</div>
</form>
I have also added the screen shot of my error please have a look on it also
and solution will be highly appreciated!
I has solved this problem with Laravel 7.x. I think Laravel 6.x is the same!
I create a variable $token = Str::random(64);
Next I create a record in password_resets table with value of token is: bcrypt($token)
( bcrypt() is function create password when you seed database)
Finally, link you send to email is origin $token
Because your token is incorrect, it should be a string length of 64 characters and look like this:
a8935edacb0711a304395c1f58979b545b4a636387053de6012e73048e5a60d2
And in your password_resets table in your database, it should be encrypted and look like this:
$2y$10$YOdbMZk2N7xLsfXZIuMIv.ZayZQCB21L.GXVPdtt/WMOO1hJL7enO
Change your MAIL_DRIVER= to log, truncate password_resets table (if on local), then do another password reset, then check your logs to read the email and see what the password reset token is. Copy and paste that url in your browser and see if you still get that error then we take it from there. :)
I got this issue resolved by running migrations. The password reset token column had the wrong type. It was not storing token correctly due to the wrong charset/collation of the column. Run migration and it should be fine.
or
maybe your reset password form does not contain an input for email
I had a different issue.
My passwords configuration in auth.php looks like this:
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => env('AUTH_PASSWORD_EXPIRE')
],
],
But I forgot to configure the AUTH_PASSWORD_EXPIRE .env variable on my live server.
In my case, the issue was the users table. I created the users table manually with my custom fields prior to installing laravel breeze and added the breeze required fields manually to table. I did not used breeze migration. That caused the issue.
Now I run breeze migration to create users table and then added my custom fields manually. It solved the issue.
You should also check which hash function you are using while registering the user, if the hash function for storing the user password while resetting is different from the one used to login the user you might recieve this error. In my case one was using bcrypt and one was using Hash::make

I can't login into laravel app, it's only possible if dev tools on chrome and mozilla are open

I have an issue with my laravel application. Login works only if i open dev tools on the browser. Is it maybe linked to the fact that chrome and mozilla are caching a page, or maybe the csrf token is not walid?
This is a blade
<div class="login-page">
<div class="form" >
<p style="display:none;" class="message" #if($errors->has('email') || $errors->has('password')) {{ 'has-error'}} #endif>Logovanje nije uspjelo!</p>
<form method="POST" action="{{ route('login') }}">
{!! csrf_field() !!}
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<div>
<input id="email" type="email" placeholder="email" name="email" value="{{ old('email') }}" required autofocus>
</div>
</div>
<div class="{{ $errors->has('password') ? ' has-error' : '' }}">
<div>
<input id="password" type="password" placeholder="lozinka" name="password" required>
</div>
</div>
<div>
<label>
<input style="max-width: 10px;text-align: left" type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Zapamti Me
</label>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Login
</button>
<a class="message" href="{{ route('password.request') }}">
Zaboravili ste lozinku?
</a>
</div>
</div>
<div>
<p class="message">Niste registrovani? Napravite nalog</p>
</div>
</form>
</div>
</div>
The controller is default laravel cotnroller, didn't change anything. In web routes i have Auth::login();
The experience you're describing is typical of code which contain console.log() or any of the other console functionality.
The console object is only activated when the Dev Toolbar is opened. Prior to that, calling the console object will result in it being reported as undefined. After the toolbar has been opened, the console will exist (even if the toolbar is subsequently closed), so your console calls will then work.

Retrieve old value with validation for dynamic form in laravel

I have an issue.
I have dynamic form as per user wants.
Everything is going good except i cannot retrieve old values when validation occurs. What i want is validation message with old input values. The problem is due to dynamic form .
Here is my code :
<div class="col-sm-5">
<input class="form-control" type="text" name="key_name[]"
placeholder="Size" value="{{ old('key_name[]') }}">
</div>
<div class="col-sm-5">
<input class="form-control" type="text" name="key_value[]"
placeholder="Price" value="{{ old('key_value[]') }}">
</div>
<button type="button" class="btn btn-danger remove-field"><i
class="ion-trash-a"></i> Delete
</button>
#if ($errors->has('key_name[]'))
<div class="error-add-size-message">
<span class="help-block">
<strong> * {{ $errors->first('key_name[]') }}</strong>
</span>
</div>
#endif
Thanks in advance.
You can count form inputs in your controller and flash this count in the session.
$count = count($request->input("key_name"));
session()->flash("form_count",$count);

Laravel Edit Post Content using CKeditor

I am trying to edit existing post using CKeditor. Content isn't loading.
<div class="form-group">
<label>Content</label>
<textarea id="editor1" name="content" class="form-control" rows="3" placeholder="Enter ...">
{{ Request::old('content') }}
</textarea>
</div>
Also im having trouble getting date from the database too.
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input type="date" name="published_at" class="form-control" value="{{ $post->published_at->format('M jS Y') }}">
</div>
For the textarea, check for old input, but fall back to defaulting to using the model's content attribute (the post's content)
<textarea ....>{!! Request::old('content', $post->content) !!}</textarea>
For your date issue, I don't know what problem you are having.

Resources