最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Web routes $request→user() and Auth::user() getting null - Stack Overflow

programmeradmin2浏览0评论

laravel upgraded 9 to 11, I'm getting $request->user() and Auth::user() null value in web route. I'm using php-open-source-saver/jwt-auth

routes/web.php

Route::get('customer/{id}', [FrontendController::class, 'home']);

app/Http/Controllers/FrontendController.php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

public function home(Request $request)
{
    dd($request->user(), Auth::user());
}

bootstrap/app.php

->withRouting(
    web: __DIR__ . '/../routes/web.php',
    api: [
        __DIR__ . '/../routes/api/core.php',
        __DIR__ . '/../routes/api/user.php'
    ],
    commands: __DIR__ . '/../routes/console.php',
    health: '/up'
)
->withMiddleware(function (Middleware $middleware) {
    //dd($middleware->getMiddlewareGroups());
})
->withExceptions(function (Exceptions $exceptions) {
    //
})->create();

Middleware groups showing when dd($middleware->getMiddlewareGroups())

"web" => [
    0 => "Illuminate\Cookie\Middleware\EncryptCookies",
    1 => "Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse",
    2 => "Illuminate\Session\Middleware\StartSession",
    3 => "Illuminate\View\Middleware\ShareErrorsFromSession",
    4 => "Illuminate\Foundation\Http\Middleware\ValidateCsrfToken",
    5 => "Illuminate\Routing\Middleware\SubstituteBindings",
],
"api" => [
    0 => "Illuminate\Routing\Middleware\SubstituteBindings"
]

Config file

config/auth.php

'defaults' => [
    'guard' => 'api',
    'passwords' => 'users',
],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'jwt',
        'provider' => 'users',
    ],
],

config/session.php

'driver' => 'file'

Environment

Q A
Framework Laravel
Framework version 11.x
PHP-Open-Source-Saver/jwt-auth 2.7
PHP version 8.2

laravel upgraded 9 to 11, I'm getting $request->user() and Auth::user() null value in web route. I'm using php-open-source-saver/jwt-auth

routes/web.php

Route::get('customer/{id}', [FrontendController::class, 'home']);

app/Http/Controllers/FrontendController.php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

public function home(Request $request)
{
    dd($request->user(), Auth::user());
}

bootstrap/app.php

->withRouting(
    web: __DIR__ . '/../routes/web.php',
    api: [
        __DIR__ . '/../routes/api/core.php',
        __DIR__ . '/../routes/api/user.php'
    ],
    commands: __DIR__ . '/../routes/console.php',
    health: '/up'
)
->withMiddleware(function (Middleware $middleware) {
    //dd($middleware->getMiddlewareGroups());
})
->withExceptions(function (Exceptions $exceptions) {
    //
})->create();

Middleware groups showing when dd($middleware->getMiddlewareGroups())

"web" => [
    0 => "Illuminate\Cookie\Middleware\EncryptCookies",
    1 => "Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse",
    2 => "Illuminate\Session\Middleware\StartSession",
    3 => "Illuminate\View\Middleware\ShareErrorsFromSession",
    4 => "Illuminate\Foundation\Http\Middleware\ValidateCsrfToken",
    5 => "Illuminate\Routing\Middleware\SubstituteBindings",
],
"api" => [
    0 => "Illuminate\Routing\Middleware\SubstituteBindings"
]

Config file

config/auth.php

'defaults' => [
    'guard' => 'api',
    'passwords' => 'users',
],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'jwt',
        'provider' => 'users',
    ],
],

config/session.php

'driver' => 'file'

Environment

Q A
Framework Laravel
Framework version 11.x
PHP-Open-Source-Saver/jwt-auth 2.7
PHP version 8.2

Expected behaviour

$request->user() or Auth::user() should be a logged-in user in the controller.

Actual behaviour

$request->user() and Auth::user() both are getting null

Share edited Apr 8 at 8:06 DarkBee 15.5k8 gold badges72 silver badges118 bronze badges asked Mar 7 at 12:14 Jitendra PatelJitendra Patel 548 bronze badges 2
  • 1 check the middlewares that are applied to your specific route, and post them in your question, example php artisan route:list --path=customer – kris gjika Commented Mar 7 at 12:44
  • only web middleware applied for this route, I have check using this command php artisan route:list -v – Jitendra Patel Commented Mar 10 at 4:57
Add a comment  | 

2 Answers 2

Reset to default 1

You're using JWT (stateless authentication), but the route you're hitting is defined in web.php, which uses the web middleware group. This group is session-based (Laravel's default), not JWT-based.

So when you call:

$request->user() 
Auth::user()

It uses the default session guard (web), but you're authenticated using the api guard (jwt)—hence, null.

Use auth:api middleware on web routes

Route::middleware('auth:api')->get('customer/{id}', [FrontendController::class, 'home']);

only web middleware applied for this route, I have check using this command php artisan route:list -v

This is the point in order to have authenticated user, the route must be in auth middleware group (auth or auth:api)

Route::group(['middleware' => 'auth'], function(){
   /** your routes go here */
});

or for single route

Route::get('someroute', [Controller::class, 'method'])->middleware('auth');

==== Edit 8/4/25 ==== In your case this will not work: you are authorising api requests, not web requests.

Move routes to api.php (core.php in your case)

发布评论

评论列表(0)

  1. 暂无评论