Uriah Ahrak
Uriah Ahrak

Reputation: 31

Laravel middleware - empty request

I'm using laravel 5.5, and tymon/jwt-auth as my jwt service provider. When I'm using the JWTAuth facade from the controllers - all goes fine - I get the token and can manipulate any thing.

When I'm trying to use the JWTAuth facade inside a custom middleware I created - the headers are gone and the request is empty. Quite sure it isn't a problem in laravel's middleware since JWT's middleware can get the token by the headers.

I'm running an nginx reverse proxy on port 80 which redirects to apache server on port 8080 and apache runs the laravel project.

Suggestion anyone?

Code:

api.php

Route::get('organizations', 'OrganizationController@index')->middleware('org');

Kernel.php

'org' => \App\Http\Middleware\OrganizationMiddleware::class,

OrganizationMiddleware.php

<?php

namespace App\Http\Middleware;

use Closure;
use JWTAuth;

class OrganizationMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
     public function handle($request, Closure $next)
     {
         $token = JWTAuth::getToken();
         return response()->json(['token' => $token], 200);

//       return $next($request);
    }
}

Non of my tries goes good. The middleware answers, but the request is totally empty. So I can't get the headers or the token, or any other parameter. Really weird. Tested locally also, only on apache, and with the appropriate .htaccess file. Also enabled CORS just in case. Token is totally empty.

UPDATE:

composer.json

"require": {
    "php": ">=5.6.4",
    "aws/aws-sdk-php-laravel": "~3.0",
    "illuminate/contracts": "^5.5",
    "laravel/framework": "5.5.*",
    "laravel/tinker": "~1.0",
    "spatie/laravel-fractal": "^5.2",
    "tymon/jwt-auth": "0.5.11"
},

Upvotes: 1

Views: 2960

Answers (2)

anshuraj
anshuraj

Reputation: 342

Set your jwt token into header

$request->headers->set('Authorization', 'Bearer' . $token);

Maybe the headers get lost during redirection of request from nginx to apache. Check your server config.

Upvotes: 0

lagbox
lagbox

Reputation: 50561

$request->bearerToken() would pull the Authorization Bearer token from the request headers.

$request->header() if you want to get all the headers.

$request->header('name', 'default') to get a header by name.

$request->headers to get the HeaderBag.

Not sure why things would be empty at this point. Version information might help.

Upvotes: 0

Related Questions