Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

codehero's avatar

TokenMissMatch

Hello, when I edit something, i have a TokenMismatchException.

I can create something, but when I use to edit something and click to so submit I have a TokenMismatch.

Tokens are set and they are in the formrequest. I have tried it with 2 different controllers and models.

      {!! Form::model($page, ['method' => 'PATCH', 'url' => ['Admin\PageController@update', $page->id], 'class' => 'form form-horizontal']) !!}
            @include('admin.pages.form')
            {!! Form::close() !!}

In form

<input name="_token" type="hidden" value="aAUZSuisrn2PTFc5rIKoIgo2xhJErQqRl1FVaf7K">
0 likes
14 replies
kubin's avatar

Hi,

ensure that

  • storage/framework/sessions is writeable
  • the session doesn't cleared until the request gets validated

If that doesn't help, compare the submitted token value with the one currently stored in session. If those two don't match, you most likely have a problem correctly setting cookies or maintaining the session throughout several requests.

codehero's avatar

@asterix The sessions folder is writeable. There are session files in it.

After the TokenMisMatch I am also logged out automatically...

kubin's avatar

Since you also get logged out it seems that the current session actually gets cleared somewhere (which removes both the _token and the current user from the session, resulting in the behavior you described).

Do you have any calls to Session::clear(), Session::forget() or the likes in your application? Can you do a dd(Session::all()) and check if the contents are as you expect?

Also, it's possible that the permissions on the sessions directory changed after session files were created successfully. Try to delete all files inside sessions (assuming that you're working in a dev environment) and check if a new one is created once you hit the page in question.

If multiple files are created for multiple requests all issued by yourself, this is another indicator that your Session object gets cleared somewhere along the way.

kubin's avatar

Sorry for the double posts, don't know why this is happening :/

codehero's avatar

@Devmaurice The Routes

Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function()
{
    Route::group(['middleware' => 'admin'], function()
    {
        Route::get('dashboard', 'AdminController@index');
     
        Route::resource('pages', 'PageController');


    });

});

Controller

 public function edit($id)
    {
       $page =  Page::findOrFail($id);
        return view('admin.pages.edit', compact('page'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Requests\PageRequest $request, $id)
    {
      //  dd($request->all());
        $this->dispatch(new UpdatePage($request->all(), $id));
        return redirect('admin/pages');
    }

@asterix Okay, I will check.

codehero's avatar

My Middleware for checking If user is Admin

public function handle($request, Closure $next)
    {

        if(!Auth::check()){
            return redirect('/');
        }

        if (!Auth::user()->hasRole('admin'))
        {
            return redirect('/');
        }


        return $next($request);
    }
Devmaurice's avatar

Remove this:

  if(!Auth::check()){
            return redirect('/');
        }

from Middleware Admin then use the out of the box auth middleware in the route

codehero's avatar

@Devmaurice it´s registered.

    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'admin' => \App\Http\Middleware\CheckIfIsAdmin::class,
    ];

And that´s the form

<form method="POST" action="http://localhost/innecco/public/admin/pages/14" accept-charset="UTF-8" class="form form-horizontal">
    <input name="_method" type="hidden" value="PATCH">
    <input name="_token" type="hidden" value="EWonRwaqPNn0BIYJ4uPfBXWfePZFWWZUTIC6hYJI">
</form>

That´s crazy. I had this error never before ...

codehero's avatar

When I use it without the Route Groups on my routes.php it´s working. Crazy.... I want to use the Groups... It´s easier to handle..

It´s because of the prefix in Route Group... without it works...

Devmaurice's avatar

@muellernm Try this

$router->group(['prefix' => 'admin','middleware' => 'auth', 'namespace' => 'Admin'], function($router) {
    $route->get('dashboard', 'AdminController@index');     
        $route->resource('pages', 'PageController');

});

Please or to participate in this conversation.