Code Monkey home page Code Monkey logo

laravel-otp's Introduction

Laravel OTP โ–ฒ

Latest Stable Version Total Downloads License

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 ichtrojan/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 Ichtrojan\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 Ichtrojan\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 Ichtrojan\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 Ichtrojan\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"
}

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.

laravel-otp's People

Contributors

ichtrojan avatar bkd27 avatar codelikesuraj avatar lhamycodes avatar fisayoafolayan avatar developersunesis avatar paulayuk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.