简体   繁体   中英

How to solve Laravel 419 csrf token error

I know that mostly the 419 error has to do with csrf token in Laravel and I have correctly added meta tag and attach it with every ajax request. The page I am having problem with is not ajax submission.

<meta name="csrf-token" content="{{ csrf_token() }}">

For any other forms I am using spatie/laravel-html repo so it adds _token on all the forms.

Following is the html of one of the form

<form method="POST" action="https://example.com/login" class="needs-validation">
    <input type="hidden" name="_token" value="NmvrH2BZtzCg4ity9cRDG9JXgJER3EUtO0BnXxGH">
    <div class="form-group">
        <label for="email" class="form-control-label required">Email</label>
        <input type="email" name="email" id="email" placeholder="Email" required="required" class="form-control">
    </div>
    <div class="form-group">
        <label for="password" class="form-control-label required">Password</label>
        <input type="password" name="password" id="password" placeholder="Password" required="required" class="form-control">
    </div>
    <div>
        <span data-href="https://example.com/register" class="clickable-row btn float-left">Sign Up</span>
        <button type="submit" class="btn float-right">Login</button>
    </div>
</form>

In my project only one route /account is causing 419 error . Strange thing is that it throws error when clicked on update first time and then if I go back to the same page it works.

I have tried clearing cache , changing route name or change the folder permission of storage on production but nothing seems to solve the issue.

What else can I do to resolve the error?

Thank you

Try to set SESSION_DRIVER=file in your .env file

Then run

php artisan config:clear
php artisan cache:clear

And retry

According to https://laravel.com/docs/5.8/csrf , creating a meta tag with the csrf token can be used for ajax requests. You still need to add a csrf token field to forms you are submitting. Try adding @csrf in your form. It will create a hidden field with the token.

<form method="POST" action="/target">
    @csrf
    ...
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM