How to use the middleware auth in Laravel 8 correctly?

Asked

Viewed 94 times

0

People created this group of routes for certain pages of my project to be protected by middleware auth:

Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'as' => 'admin.'], function () {
    Route::get('/', [AuthController::class, 'loginPage'])->name('login');

    Route::group(['middleware' => 'auth'], function () {
        Route::get('home', [AuthController::class, 'home'])->name('home');
    });
});

But when I check the list of routes the middleware was not applied to the route home: inserir a descrição da imagem aqui

Does anyone know what might be going wrong?

And this would be the best way to make this process?

  • 3

    In the route print there is the middleware auth. I don’t understand. Open an anonymous browser session and try to access the home route, if ok, you should not load the home route/view page See another way to use middleware groups here https://laravel.com/docs/8x/middleware#middleware-groups

  • The way you protected the route is correct. Also, from what I saw in the screeshot, the middleware is being applied. Have you checked if you can access without logging in? Try logging out or accessing.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.