passport: Passport 401 Unauthenticated.
Laravel: 5.6.27 Passport: 7.0.1 Passport work ok with personal access token, but not with OAuth clients.
I found bug in vendor/laravel/passport/src/Guards/TokenGuard.php in protected function authenticateViaBearerToken($request). $psr->getAttribute(‘oauth_user_id’) always return null, so I added this code for quick fix, and it’s working now:
Line: 118
$clientId = $psr->getAttribute('oauth_client_id');
if(!$user) {
$clientId = $psr->getAttribute('oauth_client_id');
$user = $this->provider->retrieveById(optional(\Laravel\Passport\Client::find($clientId))>user_id);
}
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 20 (1 by maintainers)
By the way - I had this problem just after configuring Passport. The solution in my case was a simple
php artisan optimize:clearOk so it took me a while to understand this myself. The api routes for managing the oauth clients cannot be used by authenticating with a Bearer token. It can only be accessed when a user is authenticated though a web app. The reason for this is that the routes are protected by the
webandauthmiddleware and not theapiandauth:apimiddleware as you’d expect. The exact reason for this is that you’d normally only access these routes from an authenticated state in a Laravel application. When this happens a cookie for the authed user is set and the routes can be accessed by using axios for example (see the docs).I do agree that the docs are not clear on this and that it’s pretty confusing. I’ll send in a PR later to make sure they’re more clear.
A better way would be to make it an actual JSON API with Bearer authentication. We could combine this with the https://laravel.com/docs/5.7/passport#consuming-your-api-with-javascript functionality in other apps. I’ll try to see if this can be updated in a future Passport version. That way the client api endpoints could be consumed as an actual api.
Anyway, hope this response helps others who have been searching for a solution.
If you are using client credentials to generate your access_token.
@agorenja Make sure of your Controller
$this->middleware('auth');it is wrongI did like this and the problem is gone!
php artisan optimize:clearphp artisan migrate:rollbackphp artisan passport:install --forcedSOLVED: I am pretty sure you followed all the configurations of Laravel Passport and you have a functioning login page. Your only issue is that when you try hitting an auth:api protected route you get this 401 error.
MY PROBLEM was that I wasn’t sending the bearer token with the request to the route. Now, this might be a BAD way to solve this problem, but it is a start and I am gonna read up on this and find a BETTER way, but well, I started adding this (axios.defaults.headers.common[‘Authorization’] = 'Bearer ’ + localStorage.getItem(‘token’)) to all the actions in the modules of my store wherever an authenticated user should hit the route and the problem was solved.
Keep in mind that you need to save your token (or access_token however you named it) in your localStorage for this particular case to work.
None of these solutions worked (using laravel 7.0 and passport 9.2). Tried apache authorization header modification, composer dump-autoload, optimize:clear, passport:keys --forced, passport:install --forced, Passport::withoutCookieSerialization(). Last thing I saw was this error logged:
The error faded away when I used this method, but the problem persisted:
Later, I realized that the rest client also appended my user name and password (I wasn’t using postman for speed issue in my pc).
This is work for me thank you.
thanks for this
I was able to resolve this error that dozens or hundreds of people report online, but none of the existing solutions worked for me, except for the following.
using Laravel 5.8, following docs to set up Passport.
In my case, although I was including the csrf token in a meta tag, it was not being picked up as the Passport documentation states that laravel will by default. from the docs (https://laravel.com/docs/5.8/passport#consuming-your-api-with-javascript):
and then the example is provided:
Well that would be great if it worked that way by default as the docs state, but in my case I had to explicitly set the axios default to include the contents of said csrf-token meta tag before making the axios request. like this:
In my case, this was the only thing that allowed me to get past the 401 unauthorized error, which seems to indicate either:
A) something in my project’s configuration has altered the default behavior of axios requests by not setting them automatically include the csrf-token based on the meta tag if present or
B) the docs are inaccurate on this one point, and in the present version of Laravel and Passport that is not the default behavior as stated.