1. Go to this page and download the library: Download trianity/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/ */
trianity / laravel-otp example snippets
use Illuminate\Support\Str;
use Trianity\Otp\Facades\Otp;
$identifier = Str::random(12);
$otp = Otp::generate($identifier);
if ($otp->status && $otp->token !== null) {
// Deliver this token through your application, then validate the submitted code.
$verify = Otp::validate($identifier, $otp->token);
}
use Trianity\Otp\Rules\OtpRule;
$request->validate([
'otp' => ['
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Trianity\Otp\Rules\OtpRule;
class OtpLoginController extends Controller
{
public function verify(Request $request): RedirectResponse
{
$data = $request->validate([
'email' => ['
use Illuminate\Support\Str;
use Trianity\Otp\Facades\Otp;
$identifier = Str::random(12);
$otp = Otp::setValidity(30)
->setLength(4)
->setMaximumOtpsAllowed(10)
->setOnlyDigits(false)
->setUseSameToken(true)
->generate($identifier);
$verify = Otp::setAllowedAttempts(10)
->validate($identifier, $otp->token);
public function now(): \DateTimeImmutable;
namespace App\Support;
use DateTimeImmutable;
use DateTimeZone;
use Trianity\Otp\Contracts\OtpClock;
final class SystemOtpClock implements OtpClock
{
private readonly DateTimeZone $timezone;
public function __construct()
{
$this->timezone = new DateTimeZone('UTC');
}
public function now(): DateTimeImmutable
{
return new DateTimeImmutable('now', $this->timezone);
}
}
use App\Support\SystemOtpClock;
use Trianity\Otp\Contracts\OtpClock;
public function register(): void
{
$this->app->singleton(OtpClock::class, SystemOtpClock::class);
}
$fakeClock = new class(new \DateTimeImmutable('2026-10-05 21:21:35 UTC')) implements OtpClock
{
public function __construct(private \DateTimeImmutable $currentTime) {}
public function now(): \DateTimeImmutable
{
return $this->currentTime;
}
public function advance(\DateInterval $interval): void
{
$this->currentTime = $this->currentTime->add($interval);
}
};
$this->app->instance(OtpClock::class, $fakeClock);
$fakeClock->advance(new \DateInterval('PT26S'));