Search code examples
phplaravellaravel-5header

Is there a way to change the Header key to X-API-Authorization instead of Authorization when passing token for authenticating the Laravel API's?


I am using Laravel inbuilt Bearer token for API authentication. https://laravel.com/docs/5.8/api-authentication#passing-tokens-in-requests

$response = $client->request('POST', '/api/user', [
    'headers' => [
        'Authorization' => 'Bearer '.$token,
        'Accept' => 'application/json',
    ],
]);

I would like to change the Authorization header to X-API-Authorization since we already have a legacy authentication in place which use this header Authorization, which I have no control over.

Does Laravel provide over-riding this inbuilt functionality?


Solution

  • The header key Authorization seems to be hardcoded in laravel. Your best bet is, creating a new Authentication guard by extending the Laravel TokenGuard, and overriding the getTokenForRequest method to use your custom header key.

    See - https://laravel.com/docs/5.8/authentication#adding-custom-guards