PHPackages                             nealyip/laravel-otp-validation - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. nealyip/laravel-otp-validation

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

nealyip/laravel-otp-validation
==============================

Validate before making action with otp

v1.0.7(7y ago)92472MITPHPPHP &gt;=7

Since Sep 21Pushed 7y ago2 watchersCompare

[ Source](https://github.com/nealyip/laravel-otp-validation)[ Packagist](https://packagist.org/packages/nealyip/laravel-otp-validation)[ Docs](https://github.com/nealyip/laravel-otp-validation)[ RSS](/packages/nealyip-laravel-otp-validation/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (2)Versions (9)Used By (0)

Description
-----------

[](#description)

The packages handle otp in various way for you. It is designed for modal dialog and called by ajax.

Install
-------

[](#install)

```
composer require nealyip/laravel-otp-validation

```

Add this provider to config/app.php

```
Nealyip\LaravelOTPValidation\Providers\OTPServiceProvider::class,
```

Publish config

```
php .\artisan vendor:publish --provider=Nealyip\LaravelOTPValidation\Providers\OTPServiceProvider
```

Configuration
-------------

[](#configuration)

You may translate error message under resources/lang/vendor/otp\_messages

By default it use log transport service, you may change it to the given mail service or write your own sms provider.

For the given mail service, simply change config/otp\_validation.php

```
// for example
'transport'    => \Nealyip\LaravelOTPValidation\Transport\MailTransport::class,
```

for the default mail transport class, define your mail driver, from address and name (MAIL\_FROM\_ADDRESS and MAIL\_FROM\_NAME for smtp)

OTP\_SEED you can change the seed for otp by changing OTP\_SEED env
check otp\_validation.php for more details

Development
-----------

[](#development)

To implement your own transport interface, simply implement the **Nealyip\\LaravelOTPValidation\\Transport\\TransportInterface** interface. for example,

```
namespace App\SMS\Transport;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Nealyip\LaravelOTPValidation\Transport\TransportInterface;

class SMSTransport implements TransportInterface {
    /**
     * @var Client
     */
    private $_client;

    public function __construct()
    {
        $this->_client = new Client();
    }

    /**
     * @inheritDoc
     */
    public function type()
    {
        return static::TYPE_SMS;
    }

    /**
     * @inheritdoc
     */
    public function send($phone_number, $message)
    {

        $sms_url = 'https://someurl';
        $parsed_url = parse_url($sms_url);
        $host       = $parsed_url['host'];

        try {

            $this->_client->request('POST', $sms_url, [
                'http_errors' => true,
                'headers'     => [
                    'Host'         => $host,
                    'Authorization'=> 'Bearer ' . config('sms.auth_code') ,
                    'Content-Type' => 'application/json',
                    'Accept'       => 'application/json'
                ],
                'json'        => compact('phone_number', 'message')
            ]);
        } catch (GuzzleException $e) {
            throw new \Exception(trans('error.fail_to_send'), 0, $e);
        }
    }
}
```

and change your config file (config/otp\_validation.php)

```
'transport'    => App\SMS\Transport\SMSTransport::class,
```

How to use
----------

[](#how-to-use)

First you are required to implement **Nealyip\\LaravelOTPValidation\\OTP\\OTPTarget** to your user model. Implement the target user mobile number and email address functions for otp transportation.

```
namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements Nealyip\LaravelOTPValidation\OTP\OTPTarget {

    /**
     * Provide the email for the user used by the Email Provider,
     * the return value may varies by the scene.
     * May return empty string if you use only SMS otp.
     *
     * @param string $scene
     * @return string
     */
    public function otpEmail($scene = null)
    {

        return $this->email;
    }

    /**
     * Provide the mobile phone number for the user used by the SMS Provider,
     * the return value may varies by the scene.
     *
     * @param null $scene
     * @return string
     */
    public function otpMobile($scene = null)
    {

        return $this->country_code . $this->area_code . $this->mobile_number;
    }

    /**
     * A unique user id, for example
     *
     * @return string
     */
    public function otpIdentifier()
    {
        return $this->id;
    }
}
```

Add routes for password attempt and resend request

```
Route::group(['prefix' => 'backend'], function(){
    otp_validation_routes();
});
```

To send the first otp, just call the send method from the OTPValidationService

```
use Nealyip\LaravelOTPValidation\Services\OTPValidationService;

class FormController{

protected $_service;

public function __construct(OTPValidationService $service) {
    $this->_service = $service;
}

public function create(){
    ...
    $user = request()->user();
    $payload = $this->_service->scene('changepassword')->send($user, ['id' => $user->id], [], 'You are about to change your password, please complete with this one time password :otp');

    return response()->json(['sent' => true, 'key' => $payload->key]);
}
```

And you may build a modal dialog from the client side and ask for the password.

To attempt a password, just call the routes, for example

```
PUT /backend/otp_validation/{key}
with json message body
{otp: 'password'}

```

to request resend

```
POST /backend/otp_validation/{key}

```

if the otp is correct it will return a success response with HTTP status code 200

you can then consume the otp

```
use Nealyip\LaravelOTPValidation\Services\OTPValidationService;

class FormController{

protected $_service;

public function __construct(OTPValidationService $service) {
    $this->_service = $service;
}

public function create(Request $request){
    ...

        $user = request()->user();
    if ($request->input('otp')) {
        // WrongTargetException will be thrown if the otp is incorrect
        $this->_service->scene('changepassword')->consume($user, ['id' => $user->id], $request->input('otp'));

        // success validate the otp
        // do your staff here

        return .....
    } else {
        $payload = $this->_service->scene('changepassword')->send($user, ['id' => $user->id], [], 'You are about to change your password, please complete with this one time password :otp');

        return response()->json(['sent' => true, 'key' => $payload->key]);
    }
}
```

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity68

Established project with proven stability

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 ~47 days

Recently: every ~82 days

Total

8

Last Release

2824d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/abab514b2a660eea5db6f70b1b24571011d02a549fb458860b9a4fe2a3b99679?d=identicon)[nealyip](/maintainers/nealyip)

---

Tags

laravelotp

### Embed Badge

![Health badge](/badges/nealyip-laravel-otp-validation/health.svg)

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

###  Alternatives

[spatie/laravel-honeypot

Preventing spam submitted through forms

1.6k6.0M60](/packages/spatie-laravel-honeypot)[proengsoft/laravel-jsvalidation

Validate forms transparently with Javascript reusing your Laravel Validation Rules, Messages, and FormRequest

1.1k2.3M49](/packages/proengsoft-laravel-jsvalidation)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[axlon/laravel-postal-code-validation

Worldwide postal code validation for Laravel and Lumen

3853.3M1](/packages/axlon-laravel-postal-code-validation)[laravel-validation-rules/credit-card

Validate credit card number, expiration date, cvc

2412.2M5](/packages/laravel-validation-rules-credit-card)[illuminatech/validation-composite

Allows uniting several validation rules into a single one for easy re-usage

184485.5k](/packages/illuminatech-validation-composite)

PHPackages © 2026

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