yarazan / laravel-sanctum-api-only
Template Laravel API-only project with Sanctum
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:project
pkg:composer/yarazan/laravel-sanctum-api-only
Requires
- php: ^8.2
- laravel/framework: ^11.9
- laravel/sanctum: ^4.0
- laravel/tinker: ^2.9
Requires (Dev)
- brianium/paratest: ^7.6
- fakerphp/faker: ^1.23
- laravel/pail: ^1.1
- laravel/pint: ^1.13
- laravel/sail: ^1.26
- mockery/mockery: ^1.6
- nunomaduro/collision: ^8.1
- pestphp/pest: ^3.5
- pestphp/pest-plugin-laravel: ^3.0
This package is auto-updated.
Last update: 2025-11-28 15:49:18 UTC
README
Laravel Sanctum API Only
Overview
This Laravel project is an API-only application designed for robust authentication and authorization using Laravel Sanctum. It includes seamless support for handling UUIDs in binary format across both MySQL and PostgreSQL, along with essential features like root user management, seeders, and role-based policies.
Key Features
1. UUID Handling
- Models requiring UUIDs can use the
HasUuidtrait (App\Traits\HasUuid) to automatically handle UUID generation and binary storage (16 bytes). - Custom validation rule:
BinaryUuidExistsfor checking object existence during validation steps. - Database configuration supports both MySQL and PostgreSQL UUID handling.
2. API Authentication
- Sanctum-based authentication with
/registerand/loginroutes. - Upon successful authentication, the server responds with a JSON object containing a
['token']structure. - Default custom validation rules are implemented for both
/registerand/loginrequests.
3. Extended Token Management
- Includes a modified version of
HasApiTokensasHasApiTokensWithLocationto track device name and location alongside tokens.
4. Root User Management
- Pre-configured root user with a dedicated role (
rootoradmin). - Artisan command
app:generate-root-passwordgenerates a secure root user password and stores it in the.envfile.- Ensure the
.envfile exists before running the command.
- Ensure the
- Default seeders create:
rootrole (or custom role based on your.envconfiguration).- Root user with credentials stored in the
.envfile.
5. Role Management
- Includes an implemented policy and controller for the Role model.
- Role-based access control is applied using Sanctum's
auth:sanctummiddleware.
Getting Started
1. Installation
- Clone the repository:
git clone <repository_url>
- Navigate to the project directory:
cd <project_name>
- Install dependencies:
composer install
- Copy the
.env.examplefile to.env:cp .env.example .env
- Generate the application key:
php artisan key:generate
2. Database Configuration
- Configure your database connection in the
.envfile:DB_CONNECTION=mysql # or pgsql DB_HOST=127.0.0.1 DB_PORT=3306 # or 5432 for PostgreSQL DB_DATABASE=your_db DB_USERNAME=your_user DB_PASSWORD=your_password
3. UUID Configuration
- For MySQL, UUIDs are stored as
BINARY(16). - For PostgreSQL, UUIDs use the native
UUIDtype.
Usage
Root User Setup
-
Generate a root password:
php artisan app:generate-root-password
- This will generate a secure password in the
.envfile.
- This will generate a secure password in the
-
Run seeders to create the root role and root user:
php artisan db:seed
Routes
- Define your API routes in the
routes/api.phpfile. - Protect routes with Sanctum middleware:
Route::middleware('auth:sanctum')->group(function () { // Your protected routes here });
Example Endpoints
Registration
POST /register Content-Type: application/json { "name": "Your Name", "email": "your@example.com", "password": "securepassword", "password_confirmation": "securepassword" }
Login
POST /login Content-Type: application/json { "email": "your@example.com", "password": "securepassword" }
Seeders
- Default seeders are included for
RoleandUsermodels.
Policies and Validation
Policies
- Role Policy is pre-configured for the
Rolemodel.
Validation Rules
BinaryUuidExistsensures UUID existence during validation.- Custom rules for
/registerand/loginroutes ensure robust input validation.
Contributing
Feel free to fork this repository and submit pull requests. Contributions are always welcome!
License
This project is open-sourced and licensed under the MIT License.