Laravel OTP (One-Time Watchword) gives an extra layer of security by creating and approving transitory passcodes for client confirmation. It’s a straightforward however viable way to secure delicate information and guarantee that as it were authorized clients pick up get to to your application. With simple integration, Laravel OTP makes a difference secure login forms and touchy operations.
Introduction
LaravelOtp may be a Laravel bundle, planned to create OTP and Approve OTP utilizing basic steps. This bundles will appear all OTP history.
Installation & Configuration
You’ll be able introduce this bundle by means of composer utilizing
composer require signaturetech/laravel-otp
Following run the command underneath to setup api-response.config record, you’ll be able set your arrangement.
php artisan vendor:publish --tag=otp-config
Presently include the utilize SignatureTech/LaravelOtp/Traits/Otpable characteristic to your demonstrate.
class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable, Otpable; }
test
Generate OTP
Use below code to generate otp
1 => User Details
$user = User::first();
2 => Create Otp
$otp = Otp::for($user->email)->generate();
3 => Otp with user
$userOtp = $user->createOtp($otp); $otp = $user->otp;
Verify OTP
Verify Your otp by using below code:
1=> user details
$user = User::first();
2 => Get Otp
$otp = Otp::for($user->email)->getOtp();
3 => Confirm OTP
try { $user->verifyOtp($otp, $request->get('otp')); } catch (OtpInvalidException $e) { return $e->getMessage; } catch (OtpExpiredException $e) { return $e->getMessage; }
Create OTP without model
Create otp without model using the following Instructions:
1 => Create OTP
$otp = Otp::for($user->email)->create();
Note: You’ll be able utilize email/mobile/phone number to produce otp Fair pass the detail using for strategy.
Note: You’ll utilize more strategy to setting otp all strategies portrayed in strategies segment.
2 => Verify OTP
try { $otp->verifyOtp($request->get('otp')); } catch (OtpInvalidException $e) { return $e->getMessage; } catch (OtpExpiredException $e) { return $e->getMessage; }
Methods:
1 => Length of Otp
$otp = Otp::for($user->email)->setLength(4)->generate();
Note: Default length is 6 digit and you’ll be able alter the default digit to include the OTP_LENGTH=4 in .env or config/otp.php file
2 => Available Format: alpha | alphanumeric | numeric
$otp = Otp::for($user->email)->setFormat('numeric')->generate();
=>Set Expiry(in Minutes)
$otp = Otp::for($user->email)->setExpiry(20)->generate();
Note: Default expiry is 10 minutes and you can change this to add the OTP_EXPIRY=20 in .env or config/otp.php file
Conclusion
The Laravel OTP bundle by SignatureTech offers a capable and adaptable arrangement for creating and confirming one-time passwords (OTPs) in Laravel applications. With simple establishment and arrangement, designers can rapidly coordinated OTP usefulness to improve the security of their frameworks. The bundle bolsters customization alternatives, counting setting OTP length, arrange, and expiry, making it versatile to distinctive needs. Whether you’re utilizing e-mail, versatile, or phone numbers, the bundle streamlines OTP administration whereas guaranteeing vigorous security against unauthorized get to. For engineers looking to reinforce their app’s security, this bundle could be a extraordinary instrument to execute.