Reputation: 898
How can I get current user in middleware? Laravel 5.6
When I try to include class
use Illuminate\Support\Facades\Auth;
and then
Auth::user()
I just get null
Middleware
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);
}
}
Authorization
$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!');
Upvotes: 1
Views: 2231
Reputation: 35337
The global middleware stack runs prior to the session being started and authentication details being available.
Define this at the bottom of the 'web' group or in your route middleware.
Upvotes: 4