newQuery
newQuery

Reputation: 78

Laravel 5 TokenMismatchException in VerifyCsrfToken.php line 46

EDIT: i get this error even for public/auth/login after pushing the Login button

Hi i'm simply trying to output a Session::flash() but I get this error message:

TokenMismatchException in VerifyCsrfToken.php line 46

Here is the code:

public function update($id, Requests\EditPostRequest $request)
{

    $post = Post::findOrFail($id);
    $post->update($request->all());
    return redirect(route('news.edit', $id))->with('success', 'Operation successful !');
}

And this is in my view:

  @if(Session::has('success'))
   <div class="alert alert-success">

    {{ Session::get('success') }}
   </div>
  @endif

I really need to fix that :/

Upvotes: 0

Views: 4427

Answers (3)

Steven Grauwmans
Steven Grauwmans

Reputation: 196

Do you have a CSRF token in your form?

<form method="post" action="someaction">
{{ csrf_field() }} // Generates an input field with a token
// Other input fields
</form>

Upvotes: 0

Dennis
Dennis

Reputation: 143

It appears to be related to using the same key in both your local/dev environment and production one.

I'm not sure how you'd get collisions, though, given that the sessions should be independent of environment, but I was doing some local development earlier. Everything was working fine in dev, pushed my project to my other PC and than I hit the TokenMismatchException.

I had a poke around my .env file and sure enough, the keys were the same between my two environments. Changing the key in my prod environment and the TokenMismatchException went away straight away.

Just look at your token which get set if they are the same change the production key with php artisan key:generate , hope for you this will work, just let me know if it don't.

Upvotes: 1

Michel
Michel

Reputation: 1165

Just add {{ Form::token() }} before the closing tag of your form.

Upvotes: 1

Related Questions