In the AuthServiceProvider;
Auth::viaRequest('api', function ($request) {
if ($request->input('api_token')) {
return User::where('api_token', $request->input('api_token'))->first();
}
});
I can't seem to get this to work. GET requests do not have a body so no input is present.
Also I've tried using $request->header('api_token') but still getting unauthorised
If I do an independent search on the DB like below it works;
Auth::viaRequest('api', function ($request) {
return User::where('api_token', 'my_api_key')->first();
});
Can you confirm that the $request header can be accessed here?
Thanks, but we don't process questions on github. I'd politely ask that you seek advice on the forums.
A simple yes/no would have sufficed. If it's a no then that's a bug.
I have exactly the same issue, with the out-of-the-box code.
I can see that the boot() function runs, as a dd() in that block returns what I would expect, but within the viaRequest block just doesn't get called.
@GrahamCampbell can you check again, please?
The issue still exists in 5.2.4.
@codepotato That's correct... I took my issue to SO and the issue is now resolved from the answer I got.
Link to question below;
http://stackoverflow.com/questions/34706482/authentication-in-lumen-5-2
Right, found the problem.
There's yet another undocumented feature (thanks for that guys!).
In the auth.php config file, under "guards", the api driver needed to be set to "token".
In the AuthServiceProvider.php, viaRequest needed to use "token" and the headers sent cannot include hyphens.
Hope that helps someone!
@codepotato I want to use your solution but I am using lumen-passport and not sure whether I should be changing the driver to token (which did fix the issue) as the driver is currently set to passport. Are you using any implementation of laravel-passport by any chance?
Most helpful comment
Right, found the problem.
There's yet another undocumented feature (thanks for that guys!).
In the auth.php config file, under "guards", the api driver needed to be set to "token".
In the AuthServiceProvider.php, viaRequest needed to use "token" and the headers sent cannot include hyphens.
Hope that helps someone!