خرید بک لینک

Vote count: 0

I have a middleware that authenticates a JWT user using tymon/jwt-auth package:

public function handle($request, Closure $next)
{
    if (! $token = $this->auth->setRequest($request)->getToken()) {
        retu $this->respond('tymon.jwt.absent', 'token_not_provided', 400);
    }

    try {
        $user = $this->auth->authenticate($token);
    } catch (TokenExpiredException $e) {
        retu $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]);
    } catch (JWTException $e) {
        retu $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]);
    }

    if (! $user) {
        retu $this->respond('tymon.jwt.user_not_found', 'user_not_found', 404);
    }

    $this->events->fire('tymon.jwt.valid', $user);

    retu $next($request);
}

Then I have a controller and I want to pass the user from the middleware to the controller.

So I did on the controller:

public function __construct()
{
    $this->user = Auth::user();
}

The problem is that $this->user is null, but when I do this on a method of the controller, it's not null.

So:

public function __construct()
{
    $this->user = Auth::user();
}

public function index()
{
    var_dump($this->user); // null
    var_dump(Auth::user()); // OK, not null
}

So the issue is that __construct is ruig before the middleware. How can I change that, or do you have another solution?

asked 34 secs ago

برچسب: نویسنده: استخدام کار تاريخ: پنجشنبه 26 فروردين 1395 ساعت: 2:23

صفحه بندی