Looking to hire Laravel developers? Try LaraJobs

laravel-otp maintained by iroid

Description
A simple package to generate and validate OTPs
Author
Last update
2024/02/22 12:46 (dev-main)
License
Links
Downloads
15

Comments
comments powered by Disqus

Laravel OTP ▲

Introduction 🖖

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

Installation 💽

Install via composer

composer require iroid/laravel-otp

Run Migrations

php artisan migrate

Usage 🧨

NOTE Response are returned as objects. You can access its attributes with the arrow operator (->)

Generate OTP

<?php

use Iroid\Otp\Otp;

(new Otp)->(string $identifier, string $type, int $length = 4, int $validity = 10);
  • $identifier: The identity that will be tied to the OTP.
  • $type: The type of token to be generated, supported types are numeric and alpha_numeric
  • $length (optional | default = 4): The length of token to be generated.
  • $validity (optional | default = 10): The validity period of the OTP in minutes.

Sample

<?php

use Iroid\Otp\Otp;

(new Otp)->generate('michael@okoh.co.uk', 'numeric', 6, 15);

This will generate a six digit OTP that will be valid for 15 minutes and the success response will be:

{
  "status": true,
  "token": "282581",
  "message": "OTP generated"
}

Validate OTP

<?php

use Iroid\Otp\Otp;

(new Otp)->validate(string $identifier, string $token)
  • $identifier: The identity that is tied to the OTP.
  • $token: The token tied to the identity.

Sample

<?php

use Iroid\Otp\Otp;

(new Otp)->validate('michael@okoh.co.uk', '282581');

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"
}

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();
}

Contribution

Interested to contribute or modification contact me email.