Ad

Authentication In Lumen 5.2

- 1 answer

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 anyone confirm that the $request header can be accessed here?

Ad

Answer

Before you read my answer, please read this article (just in case you're using Apache Web Server).

Headers containing invalid characters (including underscores) are now silently dropped.

Ok, now to get your api_token value, you need to change your request header-field name to Api-Token (this is standard, you may read this).

Now you can access your Api-Token value via:

$request->header('Api-Token', 'any-default-value');

If you really want to get your non standard header definition, you may use getallheaders function. For more information, you may read Symfony\Component\HttpFoundation\[email protected] method.

Ad
source: stackoverflow.com
Ad