mahbubur508 / api-auth
A robust and fast REST API Authentication package for Laravel using Sanctum.
Requires
- php: ^8.2
- laravel/framework: ^10.0|^11.0|^12.0
- laravel/sanctum: ^3.0|^4.0
README
A lightweight and secure REST API Authentication package for Laravel powered by Laravel Sanctum. Quickly add user registration, login, logout, and authenticated profile endpoints to your Laravel applications with minimal setup.
✨ Features
- 🔐 Laravel Sanctum powered authentication
- 🚀 Ready-to-use API endpoints
- 👤 User Registration
- 🔑 User Login
- 🚪 User Logout
- 🙍 Authenticated User Profile
- ⚙️ Configurable route prefixes
- 🎟️ Customizable token names
- 📦 Plug-and-play installation
- 📄 Consistent JSON responses
📋 Requirements
- PHP 8.1+
- Laravel 10.x / 11.x / 12.x / 13.x
- Laravel Sanctum
📦 Installation
Install the package via Composer:
composer require mahbubur508/api-auth
⚙️ User Model Configuration
Ensure your User model uses Sanctum's HasApiTokens trait:
<?php namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use Laravel\Sanctum\HasApiTokens; class User extends Authenticatable { use HasApiTokens; }
🗄️ Run Migrations
php artisan migrate
🔧 Publish Configuration
php artisan vendor:publish --tag="api-auth-config"
This will create:
config/api-auth.php
⚙️ Configuration
return [ /* |-------------------------------------------------------------------------- | Authentication Route Prefix |-------------------------------------------------------------------------- */ 'prefix' => 'api/v1/auth', /* |-------------------------------------------------------------------------- | Sanctum Token Name |-------------------------------------------------------------------------- */ 'token_name' => 'api_auth_token', ];
🚀 API Endpoints
Add the following header to all requests:
Accept: application/json
📝 Register User
Endpoint
POST /api/v1/auth/register
Request Body
{
"name": "John Doe",
"email": "john@example.com",
"password": "password123",
"password_confirmation": "password123"
}
🔑 Login User
Endpoint
POST /api/v1/auth/login
Request Body
{
"email": "john@example.com",
"password": "password123"
}
👤 Get Authenticated User
Endpoint
GET /api/v1/auth/me
Headers
Authorization: Bearer {access_token}
🚪 Logout User
Endpoint
POST /api/v1/auth/logout
Headers
Authorization: Bearer {access_token}
📄 Example Response
Successful Login
{
"success": true,
"message": "Login successful",
"data": {
"user": {
"id": 1,
"name": "John Doe",
"email": "john@example.com"
},
"token": "1|xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}
🔒 Security Recommendations
For production environments:
- Enable HTTPS
- Configure CORS properly
- Use secure password validation
- Apply API rate limiting
- Rotate tokens when necessary
🛠 Customization
Change route prefix and token name from:
config/api-auth.php
Example:
'prefix' => 'api/auth', 'token_name' => 'my_custom_token',
📄 License
The MIT License (MIT). See LICENSE for details.
👨💻 Author
Md. Mahbubur Rahman
Full-Stack Developer
- Laravel
- React.js
- Next.js
- REST APIs
- Docker & DevOps
❤️ Support
If you find this package useful, please consider:
⭐ Starring the repository
🐛 Reporting issues
🚀 Contributing improvements
Made with ❤️ by Md. Mahbubur Rahman