PHPackages                             morcen/laravel-otp-generator - 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. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. morcen/laravel-otp-generator

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

morcen/laravel-otp-generator
============================

Generate OTP for your Laravel application

v1.1.0(4y ago)193[1 issues](https://github.com/morcen/laravel-otp-generator/issues)[3 PRs](https://github.com/morcen/laravel-otp-generator/pulls)MITPHPPHP ^8.0

Since Mar 14Pushed 2y ago1 watchersCompare

[ Source](https://github.com/morcen/laravel-otp-generator)[ Packagist](https://packagist.org/packages/morcen/laravel-otp-generator)[ Docs](https://github.com/morcen/laravel-otp-generator)[ GitHub Sponsors](https://github.com/morcen)[ RSS](/packages/morcen-laravel-otp-generator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (13)Versions (8)Used By (0)

[![](https://camo.githubusercontent.com/2bedf63f24cda7efab02da955dc11fb7ef8a060e2f26b73c33a7aac84529b8a3/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f737570706f72742d756b7261696e652e7376673f743d31)](https://supportukrainenow.org)

Generate OTP for your Laravel application
=========================================

[](#generate-otp-for-your-laravel-application)

[![Latest Version on Packagist](https://camo.githubusercontent.com/a2d68ad6865fc1dc3a0259c1b518e2150b1ee5a9f4b8d1cbbfa863dfe6021d6a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6f7263656e2f6c61726176656c2d6f74702d67656e657261746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/morcen/laravel-otp-generator)[![GitHub Tests Action Status](https://camo.githubusercontent.com/3814963504fce4ba6e7e968715b5c8ef91a2da4eca47277f0ebb2d5994677f73/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6d6f7263656e2f6c61726176656c2d6f74702d67656e657261746f722f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/morcen/laravel-otp-generator/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/3be26df6d6e0fa24c8a828f33d7c0b450aaff0da0dc3f3ddf13f66ef35dd9760/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6d6f7263656e2f6c61726176656c2d6f74702d67656e657261746f722f436865636b253230262532306669782532307374796c696e673f6c6162656c3d636f64652532307374796c65)](https://github.com/morcen/laravel-otp-generator/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/715ff237fdd38adc6d6289cfc4b90ff0971101f7150300accae68354d6bb7ebf/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6f7263656e2f6c61726176656c2d6f74702d67656e657261746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/morcen/laravel-otp-generator)

This package requires Laravel &gt;= 8.x.

Installation
------------

[](#installation)

1. Install the package via composer:

    ```
    composer require morcen/laravel-otp-generator
    ```
2. Publish the config file with:

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

    Open `config/otp.php` and update the following:

    - `identifier` - this is what this package will use to query for the OTP record in the database
    - `set` - this defines the characters that will be used for the code. Possible values are:
        - `numbers` - from zero to nine (0 to 9)
        - `lowercase` - English alphabet from `a` to `z`
        - `uppercase` - English alphabet from `A` to `Z`
        - `others`- defaults to empty string. You can put any other characters you wish to see as part of the OTP code.
        - `all` - uses all letters and numbers, including the characters in `others` option. This is the defualt option.
    - `lifetime` - defines how long the OTP will be valid for. Default is `15` minutes.
    - `length` - defines the default length of the OTP. Though this can be simply overriden whenever the `generate` or `generateFor` methods are called. Default length is `4`.
    - `encrypt` - if set to `true`, it will have additional property `hash` in the returned object. If used with `generateFor()` method, the value that will be saved in the database will be the encrypted value. Available since `v1.1.0`.
    - `alg` - This will be the hashing algorithm used for the OTP code. This will only take effect if `encrypt` is set to `true`. Valid options are `sha1` and `md5`. Available since `v1.1.0`.
3. Publish the migrations with:

    ```
    php artisan vendor:publish --tag="otp-generator-migrations"
    ```

    and update it to include the `identifier` field mentioned in the previous step. For example, if you have

    ```
    'identifier' => 'email'
    ```

    in your `config/otp.php`, you have to add this line in the migration file created:

    ```
    Schema::create('otps', function (Blueprint $table) {
        $table->id();
        $table->string('email');  // string('code');
        $table->unsignedInteger('expiration');
        $table->timestamp('created_at');
    });
    ```
4. Run the migrations with:

    ```
     php artisan migrate
    ```

Usage
-----

[](#usage)

To generate an OTP that can be validated afterwards, use the method `generatedFor()`. It accepts two parameters:

- `$identifierValue` (required) - the value that will be matched against the `identifier`
- `$length` (optional) - the length of the code to use. If this is not provided, it will use the default `length` option set in `config/otp.php`.

For example, we want to create an OTP for the email `abcd@example.com`:

```
use Morcen\LaravelOtpGenerator\Facades\Otp;

$otp = Otp::generateFor('abcd@example.com');
```

`$otp` receives back the OTP object\*, e.g.

```
{
    "email": "abcd@example.com",
    "code": "028988",
    "hash": "6120a26f84406f452c1b27509505093ba4aa263d",
    "expiration": 1647363156
}
```

And then to validate, use the method `validateFor`, accepting the `$identifierValue` and the OTP as parameters and returns `bool`:

```
Otp::validateFor('abcd@example.com', '028988'); // returns `true`
```

\**NOTE: `hash` property only appears in the OTP object if `encrypt` is set to `true`.*

---

To generate and receive an OTP code only:

```
use Morcen\LaravelOtpGenerator\Facades\Otp;

$otp = Otp::generate();
```

`$otp` receives back just the OTP object like this:

```
{
    "code": "234198",
    "hash": "858edae3093a5b6f5b7812cff2e2e369da0a2290"
}
```

\**NOTE: `hash` property only appears in the OTP object if `encrypt` is set to `true`.*

---

To override the default `length` of the OTP, pass the length as a parameter. For example, to generate an OTP that is 10 characters long:

```
$otp = Otp::generateFor('abcd@example.com', 10);
```

or

```
$otp = Otp::generate(10);
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

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

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Morcen](https://github.com/morcen)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 53.7% 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 ~0 days

Total

4

Last Release

1520d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0d29181937219755d8ae3ceda87a04c1fe93478a4bdcfa347daef6499ec472e9?d=identicon)[morcen](/maintainers/morcen)

---

Top Contributors

[![morcen](https://avatars.githubusercontent.com/u/28680830?v=4)](https://github.com/morcen "morcen (22 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (10 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (9 commits)")

---

Tags

laravelmorcenlaravel-otp-generator

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/morcen-laravel-otp-generator/health.svg)

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

###  Alternatives

[lab404/laravel-impersonate

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

2.3k16.4M48](/packages/lab404-laravel-impersonate)[bezhansalleh/filament-shield

Filament support for `spatie/laravel-permission`.

2.8k2.9M88](/packages/bezhansalleh-filament-shield)[spatie/laravel-login-link

Quickly login to your local environment

4381.2M1](/packages/spatie-laravel-login-link)[ryangjchandler/laravel-cloudflare-turnstile

A simple package to help integrate Cloudflare Turnstile.

438896.6k2](/packages/ryangjchandler-laravel-cloudflare-turnstile)[headerx/laravel-jetstream-passport

Passport and OAuth2 support for laravel's jetstream starter kit (Livewire)

3016.4k1](/packages/headerx-laravel-jetstream-passport)

PHPackages © 2026

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