2

I have set-up Laravel using passport as per the documentation here:

https://laravel.com/docs/5.3/passport.

I have written one route in API route and send request http://localhost/laravel_project/public/api/user using postman but its showing me below error:

NotFoundHttpException in RouteCollection.php line 161:

I have the following route (in routes/api.php):

Route::get('/user', function (Request $request) {
    return array(
      1 => "John",
      2 => "Mary",
      3 => "Steven"
    );
})->middleware('auth:api');

but when I removed ->middleware('auth:api') line in the route it's working fine for me.

How can I fix this?

Also please tell me if I don't want to add passport authentication in my some routes how can i do this?

devpro
  • 16,184
  • 3
  • 27
  • 38
okconfused
  • 3,567
  • 4
  • 25
  • 38

2 Answers2

2

I was having the same problem, it seems you have to specify the Accept header to application/json as shown by Matt Stauffer here

Some further notes:

  1. Your default Accept header is set to text/html, therefore Laravel will try redirect you to the url /login but probably you haven't done PHP artisan make:auth so it wont find the login route.
  2. When you remove the middleware it will work because you are no longer authenticating your request
  3. To authenticate some routes, just group them using Route::group and auth:api as the middleware
Chillie
  • 1,030
  • 10
  • 28
James Dube
  • 738
  • 7
  • 11
1

In your routes/api.php you can do this:

Route::group(['middleware' => 'auth:api'], function(){

    Route::get('/user', function (Request $request) {
        return array(
            1 => "John",
            2 => "Mary",
            3 => "Steven"
        );
    });

});

All the routes you define inside this group will have the auth:api middleware, so it will need passport authentication in order to access to it.

Outside of this group you can put your api routes that doesn't need authentication.

EDIT: In order to make sure that the route actually exists with the required middleware, run php artisan route:list.

Gerard Reches
  • 3,048
  • 3
  • 28
  • 39
  • Still I am getting error: NotFoundHttpException in RouteCollection.php line 161: – okconfused Oct 25 '16 at 12:23
  • @okconfused What happens when you run `php artisan route:list`? The route appears in the list with api and auth:api as middleware? I'm using Laravel 5.3 and passport in my project with this route and it's working for me. – Gerard Reches Oct 25 '16 at 12:51
  • When I ran this command php artisan route:list, it shows me Method: GET|HEAD, URI: api/user, Atcion: Closure and Middleware: api,auth:api I am trying to hit API in the postman by this url http://localhost/project/public/api/user with Header. Content-Type: application/json Also I have only one route in the routes/api.php which is mentioned in the above question may be this is the issue. Can you please tell me how you sending the API request with token? – okconfused Oct 25 '16 at 13:08
  • @okconfused Maybe the problem is with postman and not with your route? Your route exists as artisan says, so it doesn't seems to be a problem with your route definition, maybe how you are trying to access to it. – Gerard Reches Oct 25 '16 at 13:19
  • in the postman, it's work for me when I removed this ->middleware('auth:api') – okconfused Oct 25 '16 at 13:24
  • @okconfused Are you sure that you get the `NotFoundHttpException` error on /api/user route? If you are not authenticated, when you try to access to the route it will redirect you to another route, generally the login route. Do you already defined the auth routes? If not, probably the error comes from the missing login route. – Gerard Reches Oct 25 '16 at 13:28
  • Yes I don't have login route in the routes/api.php file. How I define login route? – okconfused Oct 25 '16 at 13:31
  • @okconfused It's well explained on its documentation section: [Authentication](https://laravel.com/docs/5.3/authentication) – Gerard Reches Oct 25 '16 at 13:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126693/discussion-between-okconfused-and-gerard-reches). – okconfused Oct 26 '16 at 07:01
  • Yes, My team-lead has been resolved this problem. Please check the this link: `http://stackoverflow.com/questions/39525968/laravels-5-3-passport-and-api-routes/40393694#40393694` – okconfused Nov 13 '16 at 05:28