Jwt-auth: Laravel 5.3 Policies not working with jwt

Created on 25 Dec 2016  路  8Comments  路  Source: tymondesigns/jwt-auth

I'm trying to use policies for my API but it seems that policies do not run with JWT.
https://laravel.com/docs/5.3/authorization#creating-policies

any workaround or fix?

stale

All 8 comments

Register Middleware to the route

screen shot 2016-12-28 at 11 27 02 am

Route::group(['middleware' => 'jwt.auth'], function () {
// your api url
});

Then the login user may be get inside laravel policy.

screen shot 2016-12-28 at 11 34 13 am
https://laravel.com/docs/5.3/authorization

make sure the user_id is integer number or cast it

"1" === 1 False

I had same issue but I had a general check to make (which would've been nice to use in before() ) but instead opted to use a middleware

can i use laravel policy and jwtAuth inside project?

and how?

Wondering also for laravel 5.6 and jwt-auth 1.0.0-rc2
Can't get policies to work together with this.

I have the very same problem with laravel 5.7 and [email protected].
@tymondesigns do you know whats the issue with policies?

Ok I figured it out.

Adding a middleware like this to the App\Http\Kernel

    protected $routeMiddleware = [
        //...
        'jwt.auth'      => \Tymon\JWTAuth\Http\Middleware\Authenticate::class,
    ];

and using in routes like this

    Route::group(['middleware' => ['jwt.auth']], function () {
       Route::post('/auth/logout', 'Api\AuthController@logout')->name('api.auth.logout');
       Route::post('/auth/refresh', 'Api\AuthController@refresh')->name('api.auth.refresh');
    });

solves the authentication problem :-)

For nice json error messages i customized render moethod of the App\Exceptions\Handler like this;

    public function render($request, Exception $exception)
    {
        if ($exception instanceof UnauthorizedHttpException) {
            return response()->json(['error' => $exception->getMessage()], $exception->getStatusCode());
        }

        return parent::render($request, $exception);
    }

It's just normal laravel middleware.

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Rasoul-Karimi picture Rasoul-Karimi  路  3Comments

gamelife1314 picture gamelife1314  路  3Comments

CBR09 picture CBR09  路  3Comments

aofdev picture aofdev  路  3Comments

therealmjk picture therealmjk  路  3Comments