LaravelPackages.net
Acme Inc.
Toggle sidebar
raja-muhammad-asher/laravel-otp-mongodb

A simple package to generate and validate OTPs for mongodb/laravel-mongodb

89
3
v1.0.2
About raja-muhammad-asher/laravel-otp-mongodb

raja-muhammad-asher/laravel-otp-mongodb is a Laravel package for a simple package to generate and validate otps for mongodb/laravel-mongodb. It currently has 3 GitHub stars and 89 downloads on Packagist (latest version v1.0.2). Install it with composer require raja-muhammad-asher/laravel-otp-mongodb. Discover more Laravel packages by raja-muhammad-asher or browse all Laravel packages to compare alternatives.

Last updated

Laravel OTP for mongodb โ–ฒ

Introduction ๐Ÿ––

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

Thanks https://github.com/mongodb/laravel-mongodb.

Original https://github.com/ichtrojan/laravel-otp Thanks bud <3

Installation ๐Ÿ’ฝ

Install via composer

composer require raja-muhammad-asher/laravel-otp-mongodb

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 Asher\Otp\Otp;

(new Otp)->generate(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 Asher\Otp\Otp;

(new Otp)->generate('[email protected]', '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 Asher\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 Asher\Otp\Otp;

(new Otp)->validate('[email protected]', '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" }

Check The validity of an OTP

To verify the validity of an OTP without marking it as used, you can use the isValid method:

<?php
use Asher\Otp\Otp;

(new Otp)->isValid(string $identifier, string $token);

This will return a boolean value of the validity of the OTP.

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

If you find an issue with this package or you have any suggestion please help out. I am not perfect.

Star History Chart