Since you need to change the way you get the user from the database, you really need to create and use a custom UserProvider , not a custom Guard . You will need only special protection if you want to rename the input key or storage key from api_token .
So, you will need a new user class UserProvider , which knows how to get a user with the specified credentials (token), and you need to tell Auth to use the new user class UserProvider .
First, if you are still using Eloquent, start by creating a new UserProvider class that extends the base class EloquentUserProvider . In this example, it is created in app/Services/Auth/MyEloquentUserProvider.php . In this class, you need to override the retrieveByCredentials function with information on how to get the user with the token provided.
namespace App\Services\Auth; use Illuminate\Auth\EloquentUserProvider; class MyEloquentUserProvider extends EloquentUserProvider { public function retrieveByCredentials(array $credentials) { if (empty($credentials)) { return; }
Once you have created your class, you need to tell Auth about it. You can do this in the boot() method of your AuthServiceProvider service AuthServiceProvider . In this example, the name "myeloquent" will be used, but you can use whatever you want (except for "eloquent" and "database").
public function boot() { $this->registerPolicies(); Auth::provider('myeloquent', function($app, array $config) { return new \App\Services\Auth\MyEloquentUserProvider($app['hash'], $config['model']); }); }
Finally, you need to tell Auth to use the new user myeloquent . This is done in the config/auth.php configuration file.
'providers' => [ 'users' => [ 'driver' => 'myeloquent', // this is the provider name defined above 'model' => App\User::class, ], ],
Read more about adding custom providers in here .