dealhub/app/Services/OTPService.php

30 lines
606 B
PHP

<?php
namespace App\Services;
use App\Models\User;
use Illuminate\Support\Facades\Cache;
class OTPService
{
public function generate(User $user, int $length = 6): string
{
$code = \Str::random($length);
Cache::put("otp_$user->id", $code, now()->addMinutes((int) config('auth.otp_lifespan', '10')));
return $code;
}
public function verify(User $user, string $otp): bool
{
$code = Cache::get("otp_$user->id");
if ($code === $otp) {
Cache::forget("otp_$user->id");
return true;
}
return false;
}
}