Skip to content

Commit e00cb80

Browse files
committed
wip
1 parent a852cb0 commit e00cb80

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/Middleware/RedirectToOtpPage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ public function handle(Request $request, Closure $next)
3939
}
4040

4141
if (! Session::has($otpService->generateVerifiedKey($user))) {
42-
if (! Session::has($otpService->generateOtpSentKey($user))) {
42+
if (! $otpService->optSendRecently($user)) {
4343
$otpService->generateOtpAndSend($user);
4444
}
4545

46-
if ($request->header('X-Inertia')) {
46+
if ($request->header('X-Inertia') && function_exists('inertia')) {
4747
return inertia()->location(route('2fa.index'));
4848
}
4949

src/Support/OtpService.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
class OtpService
1111
{
12+
protected $resendAfterMinutes = 2;
13+
1214
/**
1315
* @param User $user
1416
* @return void
@@ -23,7 +25,7 @@ public function generateOtpAndSend(User $user)
2325

2426
$user->notify(new $class($otp));
2527

26-
Session::put($this->generateOtpSentKey($user), true);
28+
Session::put($this->generateOtpSentKey($user), time());
2729
}
2830

2931
public function generateRandomOtp(): int
@@ -56,6 +58,17 @@ public function generateOtpSentKey(Authenticatable $user): string
5658
return get_class($user).'-'.$user->id.'otp-sent';
5759
}
5860

61+
public function optSendRecently(Authenticatable $user): bool
62+
{
63+
if (!Session::has($key = $this->generateOtpSentKey($user))) {
64+
return false;
65+
}
66+
67+
$otpSentAt = Session::get($key);
68+
69+
return $otpSentAt > time() - (60 * $this->resendAfterMinutes);
70+
}
71+
5972
public function shouldCoverRoutePath(string $path): bool
6073
{
6174
$paths = Arr::wrap(config('otp.paths'));

0 commit comments

Comments
 (0)