It seems like you've done most of the configuration correctly by extending the Authenticatable class in your Employee model and using the HasApiTokens trait from Sanctum. However, there are a few things you might want to check to ensure that Sanctum is properly authenticating your Employee model.
-
Sanctum's Guard Configuration: Make sure that you have configured Sanctum's guard to use your
Employeeprovider. In yourconfig/auth.php, you should have something like this:
'guards' => [
'sanctum' => [
'driver' => 'sanctum',
'provider' => 'employees', // Make sure this is pointing to the correct provider
],
// other guards...
],
-
Provider Key: In the providers array, you have used
'users'as the key. You might want to change that to'employees'to avoid confusion and clearly indicate that you are using a different provider:
'providers' => [
'employees' => [ // Changed from 'users' to 'employees'
'driver' => 'eloquent',
'model' => env('AUTH_MODEL', App\Models\Employee::class),
'table' => env('AUTH_TABLE', 'employee.Employee'),
],
// other providers...
],
-
Middleware Configuration: Ensure that your route middleware is using the correct guard. If you've changed the provider key as suggested above, you should also update the middleware to use the
employeesguard:
Route::group(['middleware' => ['auth:sanctum']], function() {
Route::get('/test', [TestController::class, 'test']);
});
If you've renamed the guard to employees, then you should specify it in the middleware like this:
Route::group(['middleware' => ['auth:employees']], function() {
Route::get('/test', [TestController::class, 'test']);
});
-
Personal Access Token: Ensure that the personal access token you are using for authentication belongs to an
Employeeinstance and not aUserinstance. When you create tokens, you should be doing so from anEmployeeinstance:
$employee = Employee::find(1); // Example employee instance
$token = $employee->createToken('token-name')->plainTextToken;
-
Request Headers: When making the request to the
/testroute, ensure that you are passing the correct authentication token in the request headers:
Authorization: Bearer your-personal-access-token
-
Database Connection: Since you mentioned that you are using multiple schemas, ensure that your
Employeemodel is using the correct database connection if it's different from the default. You can specify the connection in yourEmployeemodel like this:
protected $connection = 'your_custom_connection_name';
After checking and ensuring all these configurations are correct, try to authenticate again. If it still doesn't work, you might want to clear your configuration cache with php artisan config:clear and check the logs for any additional errors that could give you more insight into the issue.