Apply Auth Middleware to All Laravel Routes

user3351236 picture user3351236 · Jan 15, 2019 · Viewed 11.4k times · Source

What is the correct way to authenticate all routes except login and register when I apply auth middleware in all controllers? Is there a way to apply auth middleware in one place and exclude login, register routes?

Answer

Khan Shahrukh picture Khan Shahrukh · Jan 15, 2019

You can group all your authenticated routes like following, laravel provides a default middleware for auth and guest users

Route::group(['middleware' => ['auth']], function () { 
    Route::get('home', 'HomeController@index');
    Route::post('save-user', 'UserController@saveUser');
    Route::put('edit-user', 'UserController@editUser');
});

The above route names are just made up, please follow a proper naming convention for your routes and controllers. Also read about middlewares over here and about routing over here