Laravel 5.2 token mismatch and middleware error

312 views Asked by At

I'm using Laravel 5.2 and doing all authentication manually. So, although everything works, but I get a token mismatch error and the reason is that I'm not passing my routes through the web middleware in my routes file:

Route::group(['middleware'=>['web']],function (){
    Route::get('/', function () {
        return view('welcome');
    })->name('home');
});
Route::social();

where Route::social(); is

public function social() {

    $this->post('/signup',['uses'=>'UserController@postSignUp','as'=>'signup']);
    $this->post('/signin',['uses'=>'UserController@postSignIn','as'=>'signin']);
    $this->get('/dashboard',function() {
        return view('dashboard');
    })->middleware('auth');
}

But if I move Route::social(); to the web middleware group, it doesn't count errors and so return empty errors even if there are. How do I get around with it? I want both things!

I've the token field in my form using {!! Form::token() !!}

1

There are 1 answers

0
Jerodev On

You are probably manually adding an $error array to your view, the web middleware will do the same thing so this will be overwritten. The web middleware group includes \Illuminate\View\Middleware\ShareErrorsFromSession which creates an error variable in the views with validation errors.

There are two ways to fix this. One is to only include the \App\Http\Middleware\VerifyCsrfToken middleware for this route. The other, wich I would prefer, is to add the route to the web middleware group, but use another name for your array with errors.