ema-salas/otp-generator

Generate OTP using a Trait into your Laravel models

1.0.0 2023-10-21 00:22 UTC

This package is auto-updated.

Last update: 2024-09-14 14:50:33 UTC


README

Introduction

This is a simple package for Laravel to generate and validate OTPs (One Time Passwords).

Installation 💽

Install via composer

composer require ema-salas/otp-generator

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' => [
        ...
        EmaSalas\OtpGenerator\OtpServiceProvider::class,
    ];
...

Publish

php artisan vendor:publish

Run Migrations

php artisan migrate

Usage 🧨

NOTE
To use Otp Generator you must use this Trait into your models

<?php

  use Illuminate\Database\Eloquent\Model;
  use EmaSalas\OtpGenerator\Otp;

  class MyModel extends Model
  {
    use Otp;
  }

Generate OTP

<?php

  $myModel = MyModel::where(['column' => $value])->first();
  $token = $myModel->generateToken(string $identifier, int $digits = 4, int $validity = 10)

Validate OTP

<?php

  $myModel = MyModel::where(['column' => $value])->first();
  $myModel::validateOtp(string $token)
  • $token: The token tied to the identity.

Responses

On Success

{
  "status": true,
  "message": "OTP is valid"
}

Does not exist

{
  "status": false,
  "message": "OTP does not exist"
}

Not Valid*

{
  "status": false,
  "message": "OTP is not valid"
}

Expired

{
  "status": false,
  "message": "OTP Expired"
}

Inspiration

This package was created inspired in next packages [1]: https://github.com/ichtrojan/laravel-otp/tree/master "ichtrojan/laravel-otp" [2]: https://github.com/erdemkeren/laravel-otp/tree/master "erdemkeren/laravel-otp"

Contribution

If you find an issue with this package or you have any suggestion please help out. I am open to PRs.