Open config/auth.php. Find guards array and you will see that you have web guard and api guard. The guard specified should correspond to one of the keys in the guards array.
5.3: api routes, auth middleware confusion
I'm getting very confused by the differences between routes set-up in web.php and ones in api.php. Also the differences between using $this->middleware('auth') and $this->middleware('auth:api'). Please be patient with me...
Setup:
- I have fired up the standard auth setup with
php artisan make:auth. registering and logging in with a user works and I end up at /home. - I have a controller called MembersController thats forming a API endpoint, returning JSON. With no auth, this is working as I want it to.
- I have a route in routes/api.php like this:
Route::group(['prefix' => 'v2'], function() {
Route::resource('members', 'MembersController');
});
To try and add authentication to MembersController I added:
public function __construct()
{
$this->middleware('auth');
}
When I navigate to http://domain.dev/api/v2/members I get redirected to http://domain.dev/login. If I log in here I get redirected to /home "You are logged in!". GOOD. Now I'm logged in, if I try and go back to http://domain.dev/api/v2/members I get immediately redirected to /home. NOT GOOD.
Interestingly, if I add the members resource route to routes/web.php and not routes/api.php, after I log in I do get sent to /home, BUT if I try to go to http://domain.dev/api/v2/members it works, and I see my pretty pretty JSON :)
So... the reason I'm using the api routes is that I want to eventually get auth:api middleware working, and use token auth sent in a header, or a POST var. This is where I've found the docs get rather thin, but it's probably because I have some fundamental understanding missing.
By the way, if someone fancies some StackExchange points, this person is having exactly the same issue it seems: http://stackoverflow.com/questions/39561695/laravel-5-3-api (not my question)
I worked out what was wrong, thanks to lots of articles around the web and delving into the 5.3 code more than before. I think the links offered above probably do contain the answers but were a little too 'conceptual' for my tiny tired brain. Here's the 'answer' I posted to a similar question on SE:
If you are specifying routes in api.php, you will need to use the auth:api middleware. For example:
Route::group(['middleware' => ['auth:api']], function () {
Route::get('/test', function (Request $request) {
return response()->json(['name' => 'test']);
});
});
Notes about Token auth and Laravel 5.3:
- If you've setup laravel's default auth system, you will also need to add a column for
api_tokento the user table. If you are using DB seeders, you might want to add something like:$table->char('api_token', 60)->nullable();to your users table seeder. Alternatively just add the column manually and fill that column with a random 60-char key. - When making the request, you can add the
api_tokenas a URL/Querystring parameter like so:domain.com/api/test?api_token=[your 60 char key]. You can also send the key as a header (if using Postman or similar), i.e: Header:Authorization, Value:Bearer [your 60 char key]. - I order to get a useful error if the token is incorrect, also send the following header with all requests:
Header:
Accept, Value:application/json. This allows theexpectsJson()check in theunauthenticated()function insideApp/Exceptions/Handler.phpto work correctly.
I found it hard to find clear docs from Laravel about using token auth with 5.3, I think it's because there's a drive to make use of Passport, and it supports tokens in a different way. Here's the article that probably helped most getting it working: https://gistlog.co/JacobBennett/090369fbab0b31130b51
Please or to participate in this conversation.