-
Notifications
You must be signed in to change notification settings - Fork 784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passport 401 Unauthenticated. #839
Comments
AuthServiceProvider file add |
Tried this, but it's not working. |
Same error! Work fine in all my routes, but not with GET oauth/clients or any other Client oauth routes |
@agorenja Make sure of your Controller public function __construct()
{
$this->middleware('auth:api');
}
|
@xutl I get the same error (oauth_user_id -> null) with /oauth/clients |
Ok 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 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. |
@driesvints , thanks for the explanation, now it's all clearer. |
By the way - I had this problem just after configuring Passport. The solution in my case was a simple
|
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. 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: |
SOLVED: 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. |
If you are using client credentials to generate your access_token.
|
thanks for this |
thank you so much
…On Sun, Jan 12, 2020 at 7:08 PM edsel77 ***@***.***> wrote:
By the way - I had this problem just after configuring Passport. The
solution in my case was a simple
php artisan optimize:clear
thanks for this
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#839?email_source=notifications&email_token=AD2ZHTPXWCNSVZX3X4TGOLLQ5MI33A5CNFSM4F2MAZB2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIWZSRQ#issuecomment-573413702>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AD2ZHTNYAQZY7OJZGS2H3Y3Q5MI33ANCNFSM4F2MAZBQ>
.
|
I did like this and the problem is gone!
|
This works with me. |
This is work for me thank you. |
I added the route on my route/api.php Route::post('oauth/token', '\Laravel\Passport\Http\Controllers\AccessTokenController@issueToken')
->name('api.passport.token'); |
This worked for me too! When you generate access_tokens via client credentials, eg: email and password Set middleware to client_credentials and remove auth:api // Add this to Http/Kernel.php $routeMiddleware array and use it as Authorization Bearer value |
@aadriantech Awesome. Life-saving solution. Worked for me |
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). |
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
The text was updated successfully, but these errors were encountered: