mohammed-abd-razaq/laravel-phone-otp-auth

A professional Laravel package for phone-based authentication with OTP verification, following clean architecture principles and Laravel best practices.

Maintainers

Package info

github.com/MOHAMMED-ABD-RAZAQ/laravel-phone-otp-auth

Issues

Documentation

pkg:composer/mohammed-abd-razaq/laravel-phone-otp-auth

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

1.0.0 2025-08-03 19:38 UTC

This package is auto-updated.

Last update: 2026-04-11 00:00:29 UTC


README

A professional Laravel package for phone-based authentication with OTP verification, following clean architecture principles and Laravel best practices.

๐Ÿš€ Features

  • Phone-based Authentication - Login and register using phone numbers
  • OTP Verification - Secure one-time password verification
  • Password Reset - Phone-based password reset with OTP
  • Clean Architecture - Repository pattern with service layer
  • Laravel Standards - Follows Laravel package conventions
  • Flexible Configuration - Customizable phone column and settings
  • Multi-language Support - Built-in internationalization
  • Professional Code - SOLID principles and clean code practices

๐Ÿ“‹ Requirements

  • PHP 8.0+
  • Laravel 9.0+
  • Laravel Sanctum (for API tokens)

๐Ÿ”ง Installation

1. Install the Package

composer require mohammed-abd-razaq/laravel-phone-auth

2. Publish Configuration

php artisan vendor:publish --tag=auth-package-config

3. Run Migrations

php artisan migrate

4. Publish Language Files (Optional)

php artisan vendor:publish --tag=auth-package-lang

โš™๏ธ Configuration

Environment Variables

Add these to your .env file:

AUTH_PHONE_COLUMN=phone
AUTH_USER_MODEL=App\Models\User

Configuration Options

Edit config/auth-package.php:

return [
    // Phone column name in users table
    'phone_column' => env('AUTH_PHONE_COLUMN', 'phone'),
    
    // User model class
    'user_model' => env('AUTH_USER_MODEL', 'App\Models\User'),
    
    // OTP settings
    'otp_length' => 6,
    'otp_expiration_minutes' => 30,
    'max_verify_attempts' => 3,
    'max_resend_count' => 3,
    'resend_delay_minutes' => 1,
    'verify_delay_minutes' => 1,
    'suspend_time_minutes' => 120,
];

๐Ÿ›ฃ๏ธ API Endpoints

Public Routes

Method Endpoint Description
POST /api/auth/login Login with phone and password
POST /api/auth/register Register new user
POST /api/auth/request-password-reset Request password reset
POST /api/auth/verify-password-reset-otp Verify password reset OTP
POST /api/auth/resend-password-reset-otp Resend password reset OTP

Protected Routes

Method Endpoint Description
POST /api/auth/verify-otp Verify signup OTP
POST /api/auth/resend-otp Resend signup OTP
POST /api/auth/change-password Change password
POST /api/auth/logout Logout user
GET /api/auth/profile Get user profile

๐Ÿ“ Usage Examples

Registration Flow

// 1. Register user (Public)
$response = $this->post('/api/auth/register', [
    'name' => 'John Doe',
    'phone' => '+1234567890',
    'email' => 'john@example.com',
    'password' => 'password123',
]);

// 2. Verify OTP (Protected - requires token)
$response = $this->withToken($token)->post('/api/auth/verify-otp', [
    'otp' => '123456',
]);

Login Flow

// Login (Public)
$response = $this->post('/api/auth/login', [
    'phone' => '+1234567890',
    'password' => 'password123',
]);

Password Reset Flow

// 1. Request password reset (Public)
$response = $this->post('/api/auth/request-password-reset', [
    'phone' => '+1234567890',
]);

// 2. Verify password reset OTP (Public)
$response = $this->post('/api/auth/verify-password-reset-otp', [
    'phone' => '+1234567890',
    'otp' => '123456',
]);

// 3. Change password (Protected - requires token)
$response = $this->withToken($token)->post('/api/auth/change-password', [
    'password' => 'newpassword123',
]);

๐Ÿงช Testing

The package includes comprehensive testing support:

  • Unit Tests: Test individual components
  • Feature Tests: Test complete workflows
  • Integration Tests: Test API endpoints

Run tests with:

composer test

๐Ÿ”ง Customization

Custom Phone Column

If your users table uses a different column name:

AUTH_PHONE_COLUMN=mobile_number

Custom User Model

AUTH_USER_MODEL=App\Models\CustomUser

๐Ÿ›ก๏ธ Security Features

  • OTP Expiration - OTPs expire after configurable time
  • Rate Limiting - Prevents abuse of OTP requests
  • Failed Attempt Tracking - Tracks and limits failed attempts
  • Secure Token Management - Uses Laravel Sanctum
  • Input Validation - Comprehensive request validation
  • Phone Number Cleaning - Standardizes phone number format

๐Ÿ”„ Error Handling

The package provides consistent error responses:

{
    "success": false,
    "message": "Invalid phone number",
    "errors": {
        "phone": ["The phone number format is invalid"]
    }
}