Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Middleware Before Controller's Constructor On Laravel 5.1?

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()) {
        return $this->respond('tymon.jwt.absent', 'token_not_provided', 400);
    }

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

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

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

    return $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 running before the middleware. How can I change that, or do you have another solution?

Update: I'm using dingo/api for routing, maybe it's an error on their side?

like image 240
HTMHell Avatar asked Apr 13 '16 19:04

HTMHell


People also ask

What is middleware in Laravel?

Laravel middleware provide us a convenient mechanism for filtering any HTTP requests entering of our applications. So now here i will show you a example by using constructor for adding a middleware for each method as like index, create, store, edit, delete, show and also update of controller.

How to add a controller in Laravel?

A: In Laravel, you can add a Controller by using the Artisan command: This will create the controller in the app/Http/Controllers directory. Share your opinion in the comment section. COMMENT NOW Shahzeb is a Digital Marketer with a Software Engineering background, works as a Community Manager — PHP Community at Cloudways.

Why does the controller constructor have to be before the middleware?

This means that in the request lifecycle, the controller constructor has to be before the middlewares. This luckily has been solved and if you use a callback for middleware dependent logic in the constructor, you will avoid this problem.

How do I define the middleware of the controller?

The easiest way to define the middleware is in the constructor of the controller. Remember to define the namespace of the middleware in the controller as well. See the following code for defining the middleware in the constructor:


1 Answers

You should use the middleware(s) in the routes

Route::middleware('jwt.auth')->group(function() {
// your routes 
});
like image 161
Tiago Mateus Avatar answered Nov 07 '22 21:11

Tiago Mateus