abdukhaligov/laravel-otp

The OTP package for Laravel.

dev-master 2024-08-10 09:36 UTC

This package is auto-updated.

Last update: 2025-01-10 10:49:37 UTC


README

Reference

Laravel OTP

Introduction

This is a simple package to generate and validate OTPs (One Time Passwords). This can be implemented mostly in Authentication.

Installation

Install via composer

composer require abdukhaligov/laravel-otp

Add service provider to the config/app.php file

<?php
   /*
    |--------------------------------------------------------------------------
    | Autoloaded Service Providers
    |--------------------------------------------------------------------------
    |
    | The service providers listed here will be automatically loaded on the
    | request to your application. Feel free to add your own services to
    | this array to grant expanded functionality to your applications.
    |
    */

    'providers' => [
        ...
        Abdukhaligov\LaravelOtp\OtpServiceProvider::class,
    ];
...

Add alias to the config/app.php file

<?php

   /*
    |--------------------------------------------------------------------------
    | Class Aliases
    |--------------------------------------------------------------------------
    |
    | This array of class aliases will be registered when this application
    | is started. However, feel free to register as many as you wish as
    | the aliases are "lazy" loaded so they don't hinder performance.
    |
    */

    'aliases' => [
        ...
        'otp' => Abdukhaligov\LaravelOtp\OtpFacade::class,
    ];
...

Run Migrations

php artisan migrate

Usage

Generate OTP

<?php

Otp::generate(string $identifier, int $digits = 6, int $validity = 10)

Otp::validate(string $identifier, string $token)

Delete expired tokens

You can delete expired tokens by running the following artisan command:

php artisan otp:clean

You can also add this artisan command to app/Console/Kernel.php to automatically clean on scheduled

<?php

protected function schedule(Schedule $schedule)
{
    $schedule->command('otp:clean')->daily();
}