dingo / dingo/api

Users authenticated with JWTAuth can not be retrieved from Request

Open
#1,381 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
PHP
Stars
9.4k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

Hi

I am using Laravel Versioned API which uses Dingo. https://github.com/mitchdav/laravel-versioned-api

I am trying to make a middleware to handle ACL.

All examples show that I should get the user from the request inside the middleware, and then I can call ->can on the user to test it's permissions as configured with the gate helper.

The key part of the middleware looks like this:

```
/**
* Handle an incoming request.
*
* @param Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$user = $request->user();
```

However, $user is always null from this call.

I tried several other methods too:

```
/**
* Handle an incoming request.
*
* @param Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$userFromRequest = $request->user();
$userFromAuth = Auth::user();
$id = Auth::id();
$userFromDingoAPI = \Dingo\Api\Facade\API::user();
```

None of it returns thing properly.

My route set up looks like this:

```
$api->version(
'V3',
[
'middleware' => ['api.auth', 'acl'],
'provider' => 'V3'
],
function () use ($api)
{
$api->get('auth/me', 'App\API\V3\Controllers\UserController@me');
}
);
```

I have a work around, but it does not let me use the gate helper functionality.

I can get the user entity with the following code in the middleware:

```
/**
* Handle an incoming request.
*
* @param Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$controller = $request->route()->getController();
$user = $controller->getUser();
```

This calls a method on the controller which calls:

`$user = JWTAuth::parseToken()->authenticate()`

In order to get the user.

This, however, returns the user entity and does not support the gate helper functionality.

Thanks very much in advance.

Will

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.