• Stars
    star
    139
  • Rank 262,136 (Top 6 %)
  • Language
    PHP
  • Created about 5 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

OTP generator and validator

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 ichtrojan/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' => [
        ...
        Ichtrojan\Otp\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' => Ichtrojan\Otp\Otp::class,
    ];
...

Run Migrations

php artisan migrate

Usage 🧨

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

Generate OTP

<?php

Otp::generate(string $identifier, int $digits = 4, int $validity = 10)
  • $identifier: The identity that will be tied to the OTP.
  • $digit (optional | default = 4): The amount of digits to be generated, can be any of 4, 5 and 6.
  • $validity (optional | default = 10): The validity period of the OTP in minutes.

Sample

<?php

$otp = Otp::generate('[email protected]', 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

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

Sample

<?php

$otp = 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.

More Repositories

1

laravel-location

A simple Laravel Package to sort Countries, States and Cities
PHP
199
star
2

nigerian-banks

An API that provides Nigerian bank logos, slug and code.
Go
158
star
3

horus

HTTP request logger for Golang
Go
87
star
4

essential-kit

A Boilerplate for all the boring stuffs πŸ”₯😎
JavaScript
50
star
5

fragrance

Fragrance showcase built with Go
Go
49
star
6

laravel-frustration

Frustrate unusual users
PHP
43
star
7

go-location

A simple Go Package to sort Countries, States and Cities
Go
42
star
8

go-todo

A simple todo list application in Golang
Go
37
star
9

thoth

An Error Logger for Go
Go
30
star
10

error-pages

A collection of error pages ⚠️
HTML
27
star
11

payments

Card Payments package
Go
18
star
12

rabbit

Trigger Laravel jobs from Go
Go
17
star
13

deploy_tut

Demo for Logrocket
PHP
15
star
14

MacOS-PHP-Essentials

Set up your PHP Environment for your Mac
Shell
12
star
15

Object-Detection

Real-time Object Detection using Tensorflow and Javascript
JavaScript
12
star
16

node-redis

A CRUD RESTFUL API USING REDIS AS DATABASE
JavaScript
11
star
17

node-rpc-client

A RPC client demo built in NodeJS
JavaScript
10
star
18

go-practice

Go
9
star
19

grpc-server

Go
7
star
20

Barcode-Generator

A Simple Barcode Generator😎πŸ”₯
JavaScript
6
star
21

go-docker

Dockerisation of a Golang application
Go
4
star
22

go-package-example

Go Package Example
Go
3
star
23

chow

A CLI tool that converts slack's users JSON export to CSV
Go
3
star
24

Markdown-Editor

A Markdown Editor in VueJS
HTML
3
star
25

go-deck

Go
2
star
26

PHP-Docker

A Docker Boilerplate for PHP Stack
2
star
27

web-server-in-different-languages

Ruby
2
star
28

rails-docker

Ruby on Rails deployment with Docker
Ruby
2
star
29

pop-game

A Bubble Pop game in javascript
JavaScript
2
star
30

twilio-api-project-article-code

PHP
2
star
31

travis-article-sample-project

PHP
2
star
32

estonia

PHP
1
star
33

MongoDB-CRUD

A CRUD RESTFUL API USING MONGODB AS DATABASE
JavaScript
1
star
34

chat-rpc

1
star
35

tonic

Go
1
star
36

laravelng-todo

PHP
1
star
37

Text-Search-Using-Algolia

A text search integration using Algolia and Laravel
PHP
1
star
38

go-mysql

1
star
39

learning-go

1
star
40

probable-parakeet

1
star
41

adefaj

HTML
1
star
42

flask-docker

The Demo Application for DigitalOcean Article
Dockerfile
1
star
43

rails-demo

Ruby
1
star
44

rant-api

Go
1
star
45

carthook

Carthook Interview
PHP
1
star
46

ichtrojan.github.io

VR demo
JavaScript
1
star
47

file-encryption

PHP
1
star
48

allsaints

PHP
1
star
49

go-restful-api

Go
1
star
50

fuckthepolice

Go
1
star