I'm Sesha, Full-Stack web developer, Programmer, Laravel & Symfony Artisan, and Typescript & Vuejs Enthusiast.
- ๐ซ How to reach me: ... Ping me on twitter: @seshaCS
Laravel OTP Generator, SImple package to generate and send OTPs
License: MIT License
I'm Sesha, Full-Stack web developer, Programmer, Laravel & Symfony Artisan, and Typescript & Vuejs Enthusiast.
otp-generator/src/OtpGenerator.php
Line 121 in d0eec40
event to try resend, i need to get current time expired
I'm trying to verify otp with the code of 6 digits and it verifies any random value first time and then give error that otp is is not valid. I'm using this package in laravel 9.19.
while we attempt
$verify = Otp::validate($identifier, $otp->token);
It does not increment no_times_attempt field in table.
The code is in OtpGenerator.php is in validate()
if ($otp->token == $token) {
return (object) [
'status' => true,
'message' => 'OTP is valid',
];
}
$otp->increment('no_times_attempted');
but it should be like below for proper functioning:
if ($otp->token == $token) {
$otp->increment('no_times_attempted');
return (object) [
'status' => true,
'message' => 'OTP is valid',
];
}
It works on my localhost but on production everything is fine but only the validate() method didn't work as i expected, what could cause this to happen?
My timezone format in expiredAt token in hosting can't same with my configuration timezone, how to fix it?
It does not make sense to update otp in table if maximum number of attempts reached.
the generate function is:
public function generate(string $identifier): object
{
$this->deleteOldOtps();
$otp = OtpModel::where('identifier', $identifier)->first();
if ($otp == null) {
$otp = OtpModel::create([
'identifier' => $identifier,
'token' => $this->createPin(),
'validity' => $this->validity,
'generated_at' => Carbon::now(),
]);
} else {
$otp->update([
'identifier' => $identifier,
'token' => $this->useSameToken ? $otp->token : $this->createPin(),
'validity' => $this->validity,
'generated_at' => Carbon::now(),
]);
}
if ($otp->no_times_generated == $this->maximumOtpsAllowed) {
return (object) [
'status' => false,
'message' => "Reached the maximum times to generate OTP",
];
}
$otp->increment('no_times_generated');
return (object) [
'status' => true,
'token' => $otp->token,
'message' => "OTP generated",
];
}
but it should be:
public function generate(string $identifier): object
{
$this->deleteOldOtps();
$otp = OtpModel::where('identifier', $identifier)->first();
if ($otp == null) {
$otp = OtpModel::create([
'identifier' => $identifier,
'token' => $this->createPin(),
'validity' => $this->validity,
'generated_at' => Carbon::now(),
]);
} else {
if ($otp->no_times_generated == $this->maximumOtpsAllowed) {
return (object) [
'status' => false,
'message' => "Reached the maximum times to generate OTP",
];
}
$otp->update([
'identifier' => $identifier,
'token' => $this->useSameToken ? $otp->token : $this->createPin(),
'validity' => $this->validity,
'generated_at' => Carbon::now(),
]);
}
$otp->increment('no_times_generated');
return (object) [
'status' => true,
'token' => $otp->token,
'message' => "OTP generated",
];
}
In case the otp is only for a specific action the otp should be deleted after successful validation so it can not be validated again.
Ideally, there would be an option which activates this behaviour.
Please include timezone configuration in the config file because Carbon::now() is using default time zone configured in php.ini and not the timezone defined in the .env file.
Thats the error, I am getting.
[2022-02-23 14:15:26] local.ERROR: Return value of Seshac\Otp\OtpGenerator::generate() must be an instance of Seshac\Otp\object, instance of stdClass returned {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Return value of Seshac\Otp\OtpGenerator::generate() must be an instance of Seshac\Otp\object, instance of stdClass returned at /var/www/snap/vendor/seshac/otp-generator/src/OtpGenerator.php:135)
[stacktrace]
?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.