1

In laravel 5.3, I can submit a form from my project when i logged in admin but when I logout from admin and submit the same form without refresh the page I get a TokenMismatchException.

My route:

Route::group(['prefix' => 'admin', 'middleware' => ['backend']], function()
{
    Route::get('logout', 'Auth\LoginController@logout')->name('user.logout');
});

My Form:

{!! Form::open(['route' => ['contactus.store'], 'method'=> 'POST', 'id' => 'contact_us_form']) !!}

    <?php echo Form::text('name', NULL, ['placeholder' => 'name', 'class' => 'form-control', 'id' => 'name']); ?>

{!! Form::close() !!}
Dees Oomens
  • 4,554
  • 3
  • 29
  • 61
Rana
  • 11
  • 1
  • Possible duplicate of [What is the right way to resolve token mismatch error in laravel?](https://stackoverflow.com/questions/45223087/what-is-the-right-way-to-resolve-token-mismatch-error-in-laravel) – online Thomas Jul 24 '17 at 14:46
  • 1
    What middleware "backend" have ? – erashdan Jul 24 '17 at 14:48

4 Answers4

1

If i understood correctly the problem:

  • You are logged in as user 'admin';
  • You go the form page;
  • You logout from admin user;
  • Without refreshing the form page, you submit the form and get the TokenMismatchException

In that case, this is the expected behaviour. From the docs:

This token is used to verify that the authenticated user is the one actually making the requests to the application.

When you logout, your CSFR become invalid: that's why you need to refresh the page.

You can disable them for specific URIs by modifying the $except property in VerifyCsrfToken.php:

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        'your/route',
    ];
}
gbalduzzi
  • 9,356
  • 28
  • 58
  • is it possible to add route in $except array? like this route('route-to-something'); ? – wahdan Jul 24 '17 at 18:46
  • no, you can't put something to evaluate when assigning value to a property outside a function, only constants – gbalduzzi Jul 25 '17 at 15:26
0

Tokens are there SPECIFICALLY to stop you posting the same form TWICE. If you don't want that behaviour, remove the CSRF token from your form.

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
0

First, you don't need to care token when the request is GET.
Second, when you use POST request, you have to save token in every request and use it. The coding way depends on your coding. Thank you.

Tiefan Ju
  • 510
  • 3
  • 14
-1

you should add

<input type="hidden" value="{{csrf_token()}}" name="_token"/>

to your logout form