Laravel Sanctum custom guard
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Laravel Sanctum Custom Guard for Seamless Authentication
In today's world of web development, implementing a robust authentication system is essential to ensure secure data access. In Laravel, the use of multiple guards can be very helpful in managing different types of users and permissions. However, sometimes you may encounter issues when working with custom guards. This blog post will guide you through how to resolve these issues by establishing your custom guard for the Sanctum API authentication in Laravel.
The issue occurs due to the absence of a specific guard name in the Laravel framework. To address this, first ensure that all necessary guards are defined in config/auth.php and their respective providers are set up properly. Here is an example configuration:
'defaults' => [
'guard' => 'user',
'passwords' => 'users',
],
'guards' => [
'user' => [
'driver' => 'token',
'provider' => 'users',
'hash' => true,
],
'admin' => [
'driver' => 'token',
'provider' => 'admins',
'hash' => true,
]
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => Admin::class,
]
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'users_password_resets',
'expire' => 60,
'throttle' => 60,
],
'admins' => [
'provider' => 'admins',
'table' => 'admins_password_resets',
'expire' => 60,
'throttle' => 60,
]
],
'password_timeout' => 10800,Next, update your model files (Admin and User) to use the Laravel\Sanctum\HasApiTokens trait. This will provide API tokens for each instance of the respective model. Here are examples of these models:
// App/Models/User.php
<?php
namespace App\Models;
use Laravel\Sanctum\HasApiTokens;
class User extends Model {
use HasApiTokens;
}// App/Models/Admin.php
<?php
namespace App\Models;
use Laravel\Sanctum\HasApiTokens;
class Admin extends Model {
use HasApiTokens;
}You can then set up your routes for authentication, using the specified guard name in each route group. For example:
// app/Http/routes/api.php
<?php
Route::group(['middleware' => 'auth:sanctum'], function () {
Route::get('users', 'UserController@index');
});
Route::group([
'prefix' => 'admin',
'middleware' => 'auth:admin',
], function () {
Route::get('admins', 'AdminController@index');
});Finally, to ensure that your guards are properly registered and recognized in your application, execute the following Artisan commands:
php artisan cache:clear
php artisan config:cacheWith these steps followed, you should be able to successfully create custom Laravel Sanctum guards for seamless authentication within your Laravel application. If any issues arise, consult the Laravel documentation and community resources for further guidance.