bonabrian/laravel-otp

Laravel OTP generator and validator without using database

1.0.0 2020-10-20 06:34 UTC

This package is auto-updated.

Last update: 2024-04-20 14:33:05 UTC


README

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