Search code examples
laravellaravel-5laravel-routinglaravel-middleware

Apply Auth Middleware to All Laravel Routes


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?


Solution

  • you can apply middlewares in the routes.php file, what you need to do is to put all your routes on a group, and add the middleware 'auth' ( except the Auth::routes() which are already configured), for example :

    Route::middleware(['first', 'second'])->group(function () {
        Route::get('/', function () {
            // Uses first & second Middleware
        });
    
        Route::get('user/profile', function () {
            // Uses first & second Middleware
        });
    });
    

    more information can be found in the docs: https://laravel.com/docs/5.7/routing#route-group-middleware