PHPackages                             cuongnx/laravel-flexible-otp - 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. [Database &amp; ORM](/categories/database)
4. /
5. cuongnx/laravel-flexible-otp

ActiveLibrary[Database &amp; ORM](/categories/database)

cuongnx/laravel-flexible-otp
============================

A flexible OTP (One-Time Password) library for Laravel, supporting multi-purpose configurations, multi-storage (MongoDB/MySQL), resend cooldown, hashing for security, and optional sending via providers like Zalo ZNS, SpeedSMS, or mail.

v1.0.2(4mo ago)09MITPHPPHP ^8.2

Since Jan 7Pushed 4mo agoCompare

[ Source](https://github.com/xuancuong220691/laravel-flexible-otp)[ Packagist](https://packagist.org/packages/cuongnx/laravel-flexible-otp)[ RSS](/packages/cuongnx-laravel-flexible-otp/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (5)Used By (0)

cuongnx/laravel-flexible-otp
============================

[](#cuongnxlaravel-flexible-otp)

[![Latest Version on Packagist](https://camo.githubusercontent.com/a1146cdd57a1ced7ae43844f1cfcf75c05c1d1a63b8bc92eb0df53a332502727/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f63756f6e676e782f6c61726176656c2d666c657869626c652d6f74702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/cuongnx/laravel-flexible-otp)[![Total Downloads](https://camo.githubusercontent.com/388ae2f83705457ec4c947f5fa89566387466cc9a3e0a3da92eb186aff367e98/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f63756f6e676e782f6c61726176656c2d666c657869626c652d6f74702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/cuongnx/laravel-flexible-otp)[![License](https://camo.githubusercontent.com/3007292e90c56ab874f518a24a591e761c8c0f31c7d5927d8772221dc19192c7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f63756f6e676e782f6c61726176656c2d666c657869626c652d6f74702e7376673f7374796c653d666c61742d737175617265)](https://github.com/cuongnx/laravel-flexible-otp/blob/main/LICENSE)

A powerful and highly flexible One-Time Password (OTP) package for Laravel applications.

---

✨ Key Features
--------------

[](#-key-features)

- **Multi-purpose configuration** – Define separate OTP settings for different use cases (login, phone verification, password reset, etc.).
- **Multi-storage support** – Works seamlessly with MySQL or MongoDB.
- **Per-purpose cooldown &amp; rate limiting** – Prevent abuse with fine-grained control.
- **Optional automatic sending** – Built-in support for Zalo ZNS, SpeedSMS, Mail, or any custom provider.
- **Modern PHP 8+ named arguments** – Clean, readable, and type-safe API.

---

📦 Requirements
--------------

[](#-requirements)

- PHP ^8.2
- Laravel ^11.0 or ^12.0

---

🚀 Installation
--------------

[](#-installation)

```
composer require cuongnx/laravel-flexible-otp
```

The package auto-registers its service provider and facade.

---

⚙️ Configuration
----------------

[](#️-configuration)

Publish the configuration file (recommended):

```
php artisan vendor:publish --tag=otp-config
```

This will create:

```
config/otp.php

```

---

🧩 Create the OTP Model
----------------------

[](#-create-the-otp-model)

The package supports both **MySQL** and **MongoDB**.

Generate the appropriate model:

```
php artisan otp:make-model
```

- If `connection` is set to `mongodb` → the model extends `MongoDB\\Laravel\\Eloquent\\Model`
- Otherwise → the model extends `Illuminate\\Database\\Eloquent\\Model`

After generation, update the config:

```
'model' => App\\Models\\OtpRecord::class, // or the name you chose
```

---

🗄 Migration
-----------

[](#-migration)

A migration is included and automatically published. Run:

```
php artisan migrate
```

This creates the `one_time_passwords` table or collection.

---

🛠 Usage
-------

[](#-usage)

```
use Cuongnx\\LaravelFlexibleOtp\\Facades\\Otp;

// Generate OTP normal (returns plain token, does not send)
$response = Otp::generate('+84123456789');

// Generate OTP with purpose (returns plain token, does not send)
$response = Otp::generate('+84123456789', purpose: 'verify_phone');

// Generate and automatically send the OTP
$response = Otp::generate(
    '+84123456789',
    purpose: 'verify_phone',
    send: true,
    provider: 'zalo_zns' // or 'speedsms', 'mail'
);

// Validate OTP
$result = Otp::validate('+84123456789', '123456', purpose: 'verify_phone');

if ($result->status) {
    // OTP is valid
}

// Check validity without consuming
$isValid = Otp::isValid('+84123456789', '123456', purpose: 'verify_phone');
```

---

🧾 Example Configuration (`config/otp.php`)
------------------------------------------

[](#-example-configuration-configotpphp)

```
return [
    'validity' => 10,                    // minutes
    'resend_cooldown' => 2,              // minutes
    'length' => 6,
    'type' => 'numeric',
    'send_provider' => 'none',           // none | mail | zalo_zns | speedsms
    'connection' => 'mongodb',           // mysql | mongodb
    'model' => App\\Models\\OtpRecord::class,

    'purposes' => [
        'login' => [
            'type' => 'numeric',
            'length' => 4,
            'validity' => 5,
            'resend_cooldown' => 1,
            'send_provider' => 'none',
        ],
        'verify_phone' => [
            'type' => 'alpha_numeric',
            'length' => 6,
            'validity' => 15,
            'resend_cooldown' => 3,
        ],
        // Add more purposes as needed
    ],
];
```

---

📩 Automatic OTP Sending
-----------------------

[](#-automatic-otp-sending)

When `send: true` is passed, the package dispatches an `OtpGenerated` event.

Create a listener to handle sending:

```
php artisan make:listener SendOtpListener --event="CuongNX\LaravelFlexibleOtp\Events\OtpGenerated"
```

Then implement your sending logic inside the handle method.

#### Quick Start: Generate Ready-to-Use Listener

[](#quick-start-generate-ready-to-use-listener)

The package provides a convenient command to create a complete listener with pre-built support for popular providers:

```
php artisan otp:make-listener
```

This command generates app/Listeners/SendOtpListener.php with ready-to-use code for:

- **SpeedSMS**
- **Zalo ZNS**
- **Mail**

You can also specify a custom name:

```
php artisan otp:make-listener MyCustomOtpSender -f
```

---

🧹 Cleaning Expired OTPs
-----------------------

[](#-cleaning-expired-otps)

Manually clean expired OTPs:

```
php artisan otp:clean
```

Recommended: schedule it in `app/Console/Kernel.php`:

```
$schedule->command('otp:clean')->daily();
```

---

📄 License
---------

[](#-license)

MIT License – see the [LICENSE](LICENSE) file for details.

---

**cuongnx/laravel-flexible-otp** – A flexible, secure, and developer-friendly OTP solution for modern Laravel projects.

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance78

Regular maintenance activity

Popularity4

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity50

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

Every ~2 days

Total

3

Last Release

121d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/951e95d597ea9f09e996482a483501ca3eba1233872148e8513405e9c87fe23b?d=identicon)[xuancuong220691](/maintainers/xuancuong220691)

---

Top Contributors

[![xuancuong220691](https://avatars.githubusercontent.com/u/12405391?v=4)](https://github.com/xuancuong220691 "xuancuong220691 (5 commits)")

---

Tags

laravelotpAuthenticationmysql2famongodbone-time-passwordmulti-factorspeedsmszalo-zns

### Embed Badge

![Health badge](/badges/cuongnx-laravel-flexible-otp/health.svg)

```
[![Health](https://phpackages.com/badges/cuongnx-laravel-flexible-otp/health.svg)](https://phpackages.com/packages/cuongnx-laravel-flexible-otp)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k25.2M34](/packages/kirschbaum-development-eloquent-power-joins)[danielme85/laravel-log-to-db

Custom Laravel Log channel handler that can store log events to SQL or MongoDB databases. Uses Laravel native logging functionality.

135934.5k1](/packages/danielme85-laravel-log-to-db)[jenssegers/mongodb-sentry

An extension for Laravel-MongoDB that lets you work with Sentry

5445.0k1](/packages/jenssegers-mongodb-sentry)[alexgeno/phone-verification-laravel

A library for phone verification via Laravel notification channels. Any notification channel can be used as a sender, and Redis or MongoDB can be used as a storage.

112.4k](/packages/alexgeno-phone-verification-laravel)[moharrum/laravel-adminer

Adminer database management tool for your Laravel application.

451.0k](/packages/moharrum-laravel-adminer)

PHPackages © 2026

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