Search code examples
phplaravellaravel-routinglaravel-7laravel-authentication

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 Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth

    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('App\Http\Controllers\Admin')
            ->group(base_path('routes/admin.php'));
    }

Solution

  • Add web middleware

    Route::middleware(['web','admin'])->...