PHPackages                             yarazan/laravel-sanctum-api-only - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. yarazan/laravel-sanctum-api-only

ActiveProject[Framework](/categories/framework)

yarazan/laravel-sanctum-api-only
================================

Template Laravel API-only project with Sanctum

v1.0.0(1y ago)01MITPHPPHP ^8.2

Since Nov 20Pushed 1y ago1 watchersCompare

[ Source](https://github.com/YaraZan/laravel-sanctum-api-only)[ Packagist](https://packagist.org/packages/yarazan/laravel-sanctum-api-only)[ RSS](/packages/yarazan-laravel-sanctum-api-only/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (12)Versions (7)Used By (0)

[![image](https://private-user-images.githubusercontent.com/115586290/388072172-db98d9d8-324c-48ef-ad83-54a059081e05.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NzU1OTY1OTEsIm5iZiI6MTc3NTU5NjI5MSwicGF0aCI6Ii8xMTU1ODYyOTAvMzg4MDcyMTcyLWRiOThkOWQ4LTMyNGMtNDhlZi1hZDgzLTU0YTA1OTA4MWUwNS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjYwNDA3JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI2MDQwN1QyMTExMzFaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0xNGIzYTFkMjQzMmMyM2UwMjg1ZmEyOTU0MjNkZDYxNDdhNjA2NTcwMzU4MzY1MDVkNTZkYWE5M2Y5OThkNjQ3JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.se63YrYTJYKE5vVlNsXpR7Gs86NUiRkXOrtTLxRHaGQ)](https://private-user-images.githubusercontent.com/115586290/388072172-db98d9d8-324c-48ef-ad83-54a059081e05.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NzU1OTY1OTEsIm5iZiI6MTc3NTU5NjI5MSwicGF0aCI6Ii8xMTU1ODYyOTAvMzg4MDcyMTcyLWRiOThkOWQ4LTMyNGMtNDhlZi1hZDgzLTU0YTA1OTA4MWUwNS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjYwNDA3JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI2MDQwN1QyMTExMzFaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0xNGIzYTFkMjQzMmMyM2UwMjg1ZmEyOTU0MjNkZDYxNDdhNjA2NTcwMzU4MzY1MDVkNTZkYWE5M2Y5OThkNjQ3JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.se63YrYTJYKE5vVlNsXpR7Gs86NUiRkXOrtTLxRHaGQ)

 Laravel Sanctum API Only
==========================

[](#-laravel-sanctum-api-only-)

[![Build Status](https://github.com/YaraZan/laravel-sanctum-api-only/actions/workflows/laravel.yml/badge.svg)](https://github.com/YaraZan/laravel-sanctum-api-only/actions)[![Build Status](https://github.com/YaraZan/laravel-sanctum-api-only/actions/workflows/php.yml/badge.svg)](https://github.com/YaraZan/laravel-sanctum-api-only/actions)[![Total Downloads](https://camo.githubusercontent.com/1aef8a9c0f0dbd543bd16484be63f6661f4c0276d2d06eab481011dfc80b1deb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f796172617a616e2f6c61726176656c2d73616e6374756d2d6170692d6f6e6c79)](https://packagist.org/packages/yarazan/laravel-sanctum-api-only)[![Latest Stable Version](https://camo.githubusercontent.com/ab778ba4edd4fa7940ba726b8aaa424b6d1a1b497c487da2ff0c493d1d10da8f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f796172617a616e2f6c61726176656c2d73616e6374756d2d6170692d6f6e6c79)](https://packagist.org/packages/yarazan/laravel-sanctum-api-only)[![License](https://camo.githubusercontent.com/85edac467347a571d7ea7260c3f809d05094cb28ea636e3ec3a219e55c1e7413/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f796172617a616e2f6c61726176656c2d73616e6374756d2d6170692d6f6e6c79)](https://packagist.org/packages/yarazan/laravel-sanctum-api-only)

### Overview

[](#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
------------

[](#key-features)

### 1. **UUID Handling**

[](#1-uuid-handling)

- Models requiring UUIDs can use the `HasUuid` trait (`App\Traits\HasUuid`) to automatically handle UUID generation and binary storage (16 bytes).
- Custom validation rule: `BinaryUuidExists` for checking object existence during validation steps.
- Database configuration supports both MySQL and PostgreSQL UUID handling.

### 2. **API Authentication**

[](#2-api-authentication)

- **Sanctum-based authentication** with `/register` and `/login` routes.
- Upon successful authentication, the server responds with a JSON object containing a `['token']` structure.
- Default **custom validation rules** are implemented for both `/register` and `/login` requests.

### 3. **Extended Token Management**

[](#3-extended-token-management)

- Includes a modified version of `HasApiTokens` as `HasApiTokensWithLocation` to track **device name** and **location** alongside tokens.

### 4. **Root User Management**

[](#4-root-user-management)

- Pre-configured **root user** with a dedicated role (`root` or `admin`).
- Artisan command `app:generate-root-password` generates a secure root user password and stores it in the `.env` file.
    - Ensure the `.env` file exists before running the command.
- Default seeders create:
    - `root` role (or custom role based on your `.env` configuration).
    - Root user with credentials stored in the `.env` file.

### 5. **Role Management**

[](#5-role-management)

- Includes an implemented policy and controller for the **Role** model.
- Role-based access control is applied using Sanctum's `auth:sanctum` middleware.

Getting Started
---------------

[](#getting-started)

### 1. **Installation**

[](#1-installation)

1. Clone the repository: ```
    git clone
    ```
2. Navigate to the project directory: ```
    cd
    ```
3. Install dependencies: ```
    composer install
    ```
4. Copy the `.env.example` file to `.env`: ```
    cp .env.example .env
    ```
5. Generate the application key: ```
    php artisan key:generate
    ```

### 2. **Database Configuration**

[](#2-database-configuration)

- Configure your database connection in the `.env` file: ```
    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**

[](#3-uuid-configuration)

- For MySQL, UUIDs are stored as `BINARY(16)`.
- For PostgreSQL, UUIDs use the native `UUID` type.

---

Usage
-----

[](#usage)

### Root User Setup

[](#root-user-setup)

1. Generate a root password:

    ```
    php artisan app:generate-root-password
    ```

    - This will generate a secure password in the `.env` file.
2. Run seeders to create the root role and root user:

    ```
    php artisan db:seed
    ```

### Routes

[](#routes)

- Define your API routes in the `routes/api.php` file.
- Protect routes with Sanctum middleware: ```
    Route::middleware('auth:sanctum')->group(function () {
        // Your protected routes here
    });
    ```

### Example Endpoints

[](#example-endpoints)

#### Registration

[](#registration)

```
POST /register
Content-Type: application/json

{
    "name": "Your Name",
    "email": "your@example.com",
    "password": "securepassword",
    "password_confirmation": "securepassword"
}
```

#### Login

[](#login)

```
POST /login
Content-Type: application/json

{
    "email": "your@example.com",
    "password": "securepassword"
}
```

### Seeders

[](#seeders)

- Default seeders are included for `Role` and `User` models.

Policies and Validation
-----------------------

[](#policies-and-validation)

### Policies

[](#policies)

- **Role Policy** is pre-configured for the `Role` model.

### Validation Rules

[](#validation-rules)

- `BinaryUuidExists` ensures UUID existence during validation.
- Custom rules for `/register` and `/login` routes ensure robust input validation.

Contributing
------------

[](#contributing)

Feel free to fork this repository and submit pull requests. Contributions are always welcome!

License
-------

[](#license)

This project is open-sourced and licensed under the [MIT License](https://opensource.org/licenses/MIT).

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity1

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

539d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4e1a9610e6fcabd9c2adb656257883f3875a47878af06d7ffa5203e47c2d8133?d=identicon)[YaraZan](/maintainers/YaraZan)

---

Top Contributors

[![YaraZan](https://avatars.githubusercontent.com/u/115586290?v=4)](https://github.com/YaraZan "YaraZan (7 commits)")

---

Tags

frameworklaravel

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/yarazan-laravel-sanctum-api-only/health.svg)

```
[![Health](https://phpackages.com/badges/yarazan-laravel-sanctum-api-only/health.svg)](https://phpackages.com/packages/yarazan-laravel-sanctum-api-only)
```

###  Alternatives

[bagisto/bagisto

Bagisto Laravel E-Commerce

26.2k161.6k7](/packages/bagisto-bagisto)[krayin/laravel-crm

Krayin CRM

22.0k32.8k1](/packages/krayin-laravel-crm)[unopim/unopim

UnoPim Laravel PIM

9.4k1.8k](/packages/unopim-unopim)[juzaweb/cms

Juzaweb CMS is a Content Management System (CMS) developed based on Laravel Framework and web platform whose sole purpose is to make your development workflow simple again. Project develop by Juzaweb

187571.2k](/packages/juzaweb-cms)[codewithdennis/larament

Larament is a time-saving starter kit to quickly launch Laravel 13.x projects. It includes FilamentPHP 5.x pre-installed and configured, along with additional tools and features to streamline your development workflow.

3691.5k](/packages/codewithdennis-larament)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
