Auth::check 中间件失败

Auth::check fails in middleware

有人可以解释为什么 Laravel 会出现这种奇怪的行为吗?基本上,我正在尝试为我的应用程序创建一个中间件

 public function handle(Request $request, Closure $next)
    {
        if (auth()->check()) {

            $expires = Carbon::now()->addMinute(2);
            \Illuminate\Support\Facades\Cache::put('user-is-online-' . Auth::user()->id, true, $expires);
        }
        return $next($request);
    }
}

但是 auth()->check 它一直失败并且不返回 true ,(用户已通过身份验证),auth()->check 在其他地方工作,例如网络路由和控制器方法,但为什么不在这里?

如果您使用 auth 中间件来保护您的路由,请确保此 auth 中间件在您的中间件之前设置为 运行,否则 auth()->check() 将 return false。尝试 php artisan route:list 并检查指定路由的中间件顺序。