The built in TokenGuard does not have a way to modify the storageKey field. Therefore, you will need to create your own Guard class that sets the field, and tell Auth to use your Guard class.
First, start by creating a new Guard class that extends the base TokenGuard class. In this example, it is created at app/Services/Auth/MyTokenGuard.php:
namespace App\Services\Auth;
use Illuminate\Http\Request;
use Illuminate\Auth\TokenGuard;
use Illuminate\Contracts\Auth\UserProvider;
class MyTokenGuard extends TokenGuard
{
public function __construct(UserProvider $provider, Request $request)
{
parent::__construct($provider, $request);
$this->inputKey = 'api_key'; // if you want to rename this, as well
$this->storageKey = 'api_key';
}
}
Once you've created your class, you need to let Auth know about it. You can do this in the boot() method on your AuthServiceProvider service provider:
public function boot(GateContract $gate)
{
$this->registerPolicies($gate);
Auth::extend('mytoken', function($app, $name, array $config) {
return new \App\Services\Auth\MyTokenGuard(Auth::createUserProvider($config['provider']), $app['request']);
});
}
And finally, you need to tell Auth to use your new mytoken guard. This is done in the config/auth.php config file.
'guards' => [
'api' => [
'driver' => 'mytoken',
'provider' => 'users',
],
],