laravel-auth maintained by cetria
Description
Laravel package providing customizable authentication API endpoints using Sanctum and Bearer tokens.
Last update
2026/08/15 21:39
(dev-master)
License
Downloads
302
Tags
api - authentication - auth - laravel - rest-api - laravel-package - token-authentication - bearer-token - sanctum - laravel-sanctum
Laravel Sanctum Auth Endpoints
Custom Laravel package providing ready-to-use authentication API endpoints using Sanctum and Bearer tokens.
Features
- Plug-and-play authentication routes
- Login with token creation (
/auth) - Get current user (
/me) - Logout current device (
/logout) - Logout all devices (
/allDeviceLogout) - Custom guard with named tokens (e.g.
devel,mobile, etc.)
Installation
composer require cetria/cetria-laravel-auth
Configuration
- Register the service provider (if not auto-discovered).
If your Laravel version does not support package auto-discovery or you have it disabled, manually register the service provider in
bootstrap/providers.php:
Cetria\Laravel\Auth\Providers\SanctumWithSpecialTokenNames::class,
- Add the custom guard to
config/auth.php:
'guards' => [
'sanctumWithSpecialTokenNames' => [
'driver' => 'sanctumWithSpecialTokenNames',
'provider' => 'users',
],
],
- Add the
Laravel\Sanctum\HasApiTokenstrait to your authentication model:
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens;
// ...
}
Usage
Define routes in routes\api.php:
use Illuminate\Support\Facades\Route;
use Cetria\Laravel\Auth\Providers\SanctumWithSpecialTokenNames;
Route::post('/auth', [Auth::class, 'auth'])->name('auth');
// Protected routes
Route::middleware('auth:' . SanctumWithSpecialTokenNames::$guardName)->group(function () {
Route::get('/me', [Auth::class, 'me'])->name('me');
Route::get('/logout', [Auth::class, 'logout'])->name('logout');
Route::get('/allDeviceLogout', [Auth::class, 'logoutAllDevices'])->name('logoutAllDevices');
// Add more authenticated routes here...
});
Testing
This package includes pre-built feature tests for the authentication endpoints and the custom guard.
Requirements
To run the tests, make sure the following dependencies are installed:
phpunit >= 11.0cetria/reflection >= 0.2
Auth User Model
Your user model must:
- implements
Cetria\Laravel\Auth\Interfaces\AuthUser - Use the
Illuminate\Database\Eloquent\Factories\HasFactorytrait
Auth User Factory
Your factory class must:
- implements the
Cetria\Laravel\Auth\Interfaces\AuthModelFactory - Use the
Cetria\Laravel\Auth\Traits\AuthModelFactorytrait
Setup
Once the requirements are met, add the test directory to your phpunit.xml configuration:
<testsuites>
<testsuite name="FeatureAuth">
<directory>vendor/cetria/laravel-auth/src/Tests/Feature</directory>
</testsuite>
</testsuites>