5

I have set up the Laravel Passport package for Laravel 5.3 just as described in the official documentation (https://laravel.com/docs/5.3/passport#introduction).

I want the API to be consumed by a mobile application, so I am trying to implement Password Grant Tokens. I have created a password grant client, and the token request process...

$response = $http->post('http://my-app.com/oauth/token', [
    'form_params' => [
        'grant_type' => 'password',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'username' => 'my@email.com',
        'password' => 'my-password',
        'scope' => '',
    ],
]);

...Just works as expected, returning an access-token and a refresh-token for one of my users.

On the one hand,

php artisan route:list

Lists correct middleware for api/user URI: api,auth:api

And driver for api guard is correctly set to passport in config/auth.php. Summing up, every step of the installation process has been done (https://laravel.com/docs/5.3/passport#installation).

Defaults contents of api.php:

Route::get('/user', function (Request $request) {
    return $request->user();
})->middleware('auth:api');

The problem comes when I access to http://my-app.com/api/user, because it seems it is authenticating the request using the 'web' middleware, not the 'api'... When I access, I am redirected to /login (login form) if the user was not logged in, and to /home if it was...

Any help would be really appreciated. Thanks in advance.

2
  • did you follow the directions here as far as utilizing your own API endpoints using a X-CSRF-TOKEN ?
    – dargue3
    Nov 10, 2016 at 15:52
  • @dargue3. Yes, I also included \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class, in Kernel.php, nothing changed. But really I do not want to consume my own API using JavaScript. It is about consuming it from an external mobile application, and for that I need the password grant type method... I still have the problem. Any other idea?
    – andcl
    Nov 11, 2016 at 10:05

2 Answers 2

10

Solved! Just for the record, the solution:

I was sending the request to http://my-app.com/api/user with HTTP Header wrong. I was sending:

Type: Authorization - Content: Bearer: $accessToken 

...and the correct way was:

Type: Authorization - Content: Bearer $accessToken (without colon)

I never thought it could be a typo... Anyway, the error was not easy to detect because the redirection to the login form misleaded me from the beginning. I believe it was such an strange behaviour indeed...

4
  • 3
    I knew is an old post, but worth to mention here by adding Authorization is still not working, you need to add one more Accept: "application/json" in your header too. This is for latest version Laravel 5.5
    – Edwin Wong
    Feb 20, 2018 at 2:11
  • It may fix the problem, but not is the solution
    – Ali
    Sep 28, 2018 at 12:25
  • okay, I really need to know how you solve this problem. I am having the same un authenticated error. here is the post... could you please check this out? stackoverflow.com/questions/56590953/…
    – user10828115
    Jul 7, 2019 at 3:13
  • Commented on your post :)
    – andcl
    Jul 8, 2019 at 9:11
0

The correct solution is removing redirectTo() from this file Authenticate middleware in app/http/middleware/Authenticate.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.