Skip to content

OTP generator and validator without using database

License

Notifications You must be signed in to change notification settings

bonabrian/laravel-otp

Repository files navigation

OTP Generator & Validator

Github License

Installation

Via Composer

composer require bonabrian/laravel-otp

To publish the config file for laravel

php artisan otp:publish

Usage in Laravel

Import the facade class

use Bonabrian\Otp\Facades\Otp;

Generate an OTP:

$code = Otp::generate($secret);

The generated OTP above will only be validated using the same secret within the default expiry time.

TIP: OTP is generally used for verification. So the easiest way of determining the secret is the user's email/username or phone number or maybe user ID.

Validate an OTP:

$valid = Otp::validate($code, $secret);

Other Generate & Validate options:

// Generate
Otp::setDigits(4)->generate($secret); // 4 Digits
Otp::setExpiry(30)->generate($secret); // 30 min expiry
Otp::setDigits(4)->setExpiry(30)->generate($secret); // 4 Digits, 30 min expiry

Make sure to set the same config during validating. Example:

// Example 1
Otp::validate($code, $secret); // false
Otp::setDigits(4)->validate($code, $secret); // true

// Example 2
Otp::validate($code, $secret); // false
Otp::setExpiry(30)->validate($codde, $secret); // true

// Example 3
Otp::validate($code, $secret); // false
Otp::setDigits(4)->setExpiry(30)->validate($code, $secret); // true

About

OTP generator and validator without using database

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages