1. Go to this page and download the library: Download thecyrilcril/laravel-otp library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
thecyrilcril / laravel-otp example snippets
use Thecyrilcril\Otp\Contracts\OtpPurpose;
enum Purpose: string implements OtpPurpose
{
case EmailVerification = 'email_verification';
case PhoneVerification = 'phone_verification';
public function value(): string
{
return $this->value;
}
}
use Thecyrilcril\Otp\Concerns\HasOtps;
final class User extends Authenticatable
{
use HasOtps;
}
$issued = $user->issueOtp(Purpose::EmailVerification);
$issued->code; // the plaintext, available exactly once
$issued->expiresAt; // CarbonImmutable
use Thecyrilcril\Otp\Exceptions\OtpThrottledException;
try {
$user->issueOtp(Purpose::EmailVerification);
} catch (OtpThrottledException $e) {
// $e->retryAfterSeconds — seconds until the limit resets
}
declare(strict_types=1);
namespace App\Notifications;
use Carbon\CarbonImmutable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use SensitiveParameter;
final class SendOtpCode extends Notification implements ShouldBeEncrypted, ShouldQueue
{
use Queueable;
public function __construct(
#[SensitiveParameter]
private readonly string $code,
private readonly CarbonImmutable $expiresAt,
) {}
/** @return list<string> */
public function via(mixed $notifiable): array
{
return ['mail'];
}
public function toMail(mixed $notifiable): MailMessage
{
$minutes = (int) max(1, round(now()->diffInMinutes($this->expiresAt)));
return new MailMessage()
->subject('Your verification code')
->line('Use the code below to verify your account.')
->line("**{$this->code}**")
->line("This code expires in {$minutes} minute(s).")
->line("If you didn't request this, you can safely ignore this email.");
}
}