Skip to content
Advertisement

Can’t access the Auth::user() from a custom route file ? Laravel 7

For a purpose i decided to create a separate routing file for the admin and separating its logic from the web.php but instead i am facing this issue :

//admin.php ( routing file )


<?php

use IlluminateSupportFacadesRoute;
use IlluminateSupportFacadesAuth

    Route::get('/admin', function ()
            {
                dd(Auth::user());  //return null
            });

ps: the admin.php is registered in the RouteServiceProvider

public function map()
    {
        $this->mapApiRoutes();
        $this->mapWebRoutes();
        $this->mapAdminRoutes();
         //
    }

protected function mapAdminRoutes()
    {
        Route::middleware('admin')
            ->namespace('AppHttpControllersAdmin')
            ->group(base_path('routes/admin.php'));
    }

Advertisement

Answer

Add web middleware

Route::middleware(['web','admin'])->...
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement