feat: add app and database modules

This commit is contained in:
2026-05-21 16:05:11 +07:00
parent 37b7e783f5
commit fad70d096b
212 changed files with 23901 additions and 0 deletions
+61
View File
@@ -0,0 +1,61 @@
<?php
namespace App\Services\Auth;
use App\Models\OtpCode;
use Carbon\Carbon;
class OtpService
{
/**
* Generate and save an OTP code for an identifier.
*/
public function generate(string $identifier, int $expiryMinutes = 10): string
{
// Delete old unexpired codes for this identifier to avoid clutter
OtpCode::where('identifier', $identifier)
->whereNull('verified_at')
->delete();
$code = (string) random_int(100000, 999999);
OtpCode::create([
'identifier' => $identifier,
'code' => $code,
'expires_at' => Carbon::now()->addMinutes($expiryMinutes),
]);
return $code;
}
/**
* Verify the OTP code.
*/
public function verify(string $identifier, string $code): bool
{
$otp = OtpCode::where('identifier', $identifier)
->where('code', $code)
->whereNull('verified_at')
->where('expires_at', '>', Carbon::now())
->latest()
->first();
if ($otp) {
$otp->update(['verified_at' => Carbon::now()]);
return true;
}
return false;
}
/**
* Clear expired codes.
*/
public function cleanup(): void
{
OtpCode::where('expires_at', '<', Carbon::now())
->whereNull('verified_at')
->delete();
}
}