You can install the package with Composer.
composer require rahmatwaisi/otp-auth
use RahmatWaisi\OtpAuth\Facades\OtpGenerator;
use RahmatWaisi\OtpAuth\Core\OtpType;
.
.
// To Create a new OTP using a key like User::$id or User::$email, etc.
$otp = OtpGenerator::create('key') // output: 234234
$user = User::query()->inRandomOrder()->first();
$otp = OtpGenerator::createFrom('smth', $user->id) // output: 234234, a 6-digit integer
$otp = OtpGenerator::createFrom('smth', $user->email, OtpType::NUMBER) // output: 234234, a 6-digit integer
$otp = OtpGenerator::createFrom('smth', $user->username, OtpType::STRING) // output: "aBcDeF", a 6-char string
$otp = OtpGenerator::createFrom('smth', $user->custom_key, OtpType::NUMBER, 12, now()->addDay()) // output: 123321234234, a 12-digit integer
$otp = OtpGenerator::createFrom('smth', $user->whatever, OtpType::STRING, 8, now()->addDay()) // output: "aBcdEfgH", an 8-char string
All codes above are using the publishable configs in the package which are located in configs/otp.php
$otp = OtpGenerator::builder()
->withPrefix('dummy_prefix')
->withKey('my_custom_key')
->withType(OtpType::NUMBER)
->withTtl(now()->addMinutes(2))
->withLength(8)
->build();
// Output: 12345678, an 8-digit integer
use RahmatWaisi\OtpAuth\Facades\OtpGenerator;
use RahmatWaisi\OtpAuth\Core\OtpType;
.
.
// To Remove, Forget or Resolve an OTP using a key like User::$id or User::$email, etc. Do like this:
// Forgets the OTP just using the key
OtpGenerator::forget($user->id);
// Checks wether an incoming OTP from request exists or not?
OtpGenerator::verify($user->id, request()->get('otp'));
OtpGenerator::verifyUsing('custom_prefix', $user->id, request()->get('otp'));
// Removes an OTP using both key and its value
OtpGenerator::remove($key, 'aBcDeFgH');
// Gets an OTP just using the key
OtpGenerator::get($key);
OtpGenerator::get($user->username);
$user = User::query()->inRandomOrder()->first();
// Get an OTP just using the key and default configs
OtpGenerator::resolver()->withDefaultSettings()->withKey($user->id)->resolve();
// Forgets an OTP just using the key and default configs
OtpGenerator::resolver()->withDefaultSettings()->withKey($user->id)->forget();
// Checks for existence of an OTP just using the key and default configs
OtpGenerator::resolver()->withDefaultSettings()->withKey($user->id)->exists(request()->get('otp'));
// Get an OTP just using the key and default prefix
OtpGenerator::resolver()->withDefaultPrefix()->withKey($user->id)->resolve();
// Checks for existence of an OTP just using the key and default perfix
OtpGenerator::resolver()->withDefaultPrefix()->withKey($user->id)->exists(request()->get('otp'));
All codes above are using the publishable configs in the package which are located in configs/otp.php
// Get an OTP just using the key and custom prefix
$otp = OtpGenerator::resolver()
->withPrefix('smth')
->withKey($user->id)
->resolve();
By using this package you can easily creat and validate any form with any length of OTPs.
Feel free to suggest improvements, I'll appreciate any contribution from anyone. Thank you.
Made with 💙 In a late late night.