cetria/laravel-auth

Laravel package providing customizable authentication API endpoints using Sanctum and Bearer tokens.

1.0.0 2025-05-12 07:44 UTC

This package is auto-updated.

Last update: 2025-05-12 07:46:04 UTC


README

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

  1. 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,
    
  2. Add the custom guard to config/auth.php:
    'guards' => [
     'sanctumWithSpecialTokenNames' => [
         'driver' => 'sanctumWithSpecialTokenNames',
         'provider' => 'users',
     ],
    ],
    

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']);
    Route::get('/logout', [Auth::class, 'logout']);
    Route::get('/allDeviceLogout', [Auth::class, '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.0
  • cetria/reflection >= 0.2

Auth User Model

Your user model must:

  • implements Cetria\Laravel\Auth\Interfaces\AuthUser
  • Use the Illuminate\Database\Eloquent\Factories\HasFactory trait

Auth User Factory

Your factory class must:

  • implements the Cetria\Laravel\Auth\Interfaces\AuthModelFactory
  • Use the Cetria\Laravel\Auth\Traits\AuthModelFactory trait

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>