在中间件中获取当前授权用户

Get current authorised user in middleware

如何获取中间件中的当前用户? Laravel 5.6

当我尝试包含 class

use Illuminate\Support\Facades\Auth;

然后

Auth::user()

我刚得到 null

中间件

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class DebugbarMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        echo "<pre>"; var_dump(Auth::user()); echo "</pre>"; die();

        return $next($request);
    }
}

授权

$controller = new LoginController();
        $request = new Request();

        Auth::logout();

        $request->setLaravelSession(session()->driver(session()->getDefaultDriver()));

        $user = Auth::loginUsingId($id);

        if ($user) {
            $controller->authenticated($request, $user);
            return $this->sendResponse(['messages' => 'User authorization successfully'], 'M User authorization successfully');
        }

        return $this->sendError('User not found!');

全局中间件堆栈在会话启动和身份验证详细信息可用之前运行。

在 'web' 组底部或您的路由中间件中定义它。