A
A
Artem00712018-04-03 20:17:11
Laravel
Artem0071, 2018-04-03 20:17:11

What is wrong with laravel passport?

Situation:
There is a way

Route::get('test', '[email protected]'); // api/test

So, it seems to work just fine. The problem arises when you need to check whether the user is authorized or not.
That is, if I now write in TestController'e Auth::user() - it will display null, although the token is passed and it is valid
But at the same time, if I do the following:
Route::get('test', '[email protected]')->middleware(['auth:api']);

Then, Auth::user() will display the user ..
But I need to get into the TestController both as a registered user and as an unregistered one (it depends on how the data will be displayed). But it turns out either so, or in any way
How to fix it?
Version 5.6
===================================
And:
return $this->setData([
            'user' => Auth::user(), // null
            'token' => $request->bearerToken() // а тут выводит токен
]);

Those. the token explicitly gets to laravel, but it does not process it if there is no authorization check

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
iljaGolubev, 2018-04-04
@iljaGolubev

In config/auth.php like this?

'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

If the route does not define a guard to use, then the default one is used.
There are many solutions...
Try auth()->guard('api')->user() for example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question