Skip to content

Commit a3dbc50

Browse files
committed
wip
1 parent 38ebf87 commit a3dbc50

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

src/Http/Controllers/OtpVerificationController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Chrysanthos\LaravelOtp\Support\OtpService;
66
use Illuminate\Foundation\Auth\User;
77
use Illuminate\Routing\Controller;
8-
use Illuminate\Support\Facades\Cache;
98
use Illuminate\Support\Facades\Session;
109

1110
class OtpVerificationController extends Controller
@@ -25,7 +24,7 @@ public function send()
2524
$key = $service->generateKey($user);
2625

2726
if ($service->check($user, request()->integer('otp-code'))) {
28-
Cache::forever($service->generateVerifiedKey($user), true);
27+
Session::put($service->generateVerifiedKey($user), true);
2928
Session::forget($key);
3029

3130
return redirect()->intended('/dashboard');

src/Listeners/OtpListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Illuminate\Auth\Events\Login;
77
use Illuminate\Auth\Events\Logout;
88
use Illuminate\Foundation\Auth\User;
9-
use Illuminate\Support\Facades\Cache;
9+
use Illuminate\Support\Facades\Session;
1010

1111
class OtpListener
1212
{
@@ -24,7 +24,7 @@ public function generate(Login $event)
2424

2525
public function clear(Logout $event)
2626
{
27-
Cache::forget(
27+
Session::forget(
2828
app(OtpService::class)->generateVerifiedKey($event->user)
2929
);
3030
}

src/Middleware/RedirectToOtpPage.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Closure;
77
use Illuminate\Http\RedirectResponse;
88
use Illuminate\Http\Request;
9-
use Illuminate\Support\Facades\Cache;
9+
use Illuminate\Support\Facades\Session;
1010
use Symfony\Component\HttpFoundation\Response;
1111

1212
class RedirectToOtpPage
@@ -32,7 +32,13 @@ public function handle(Request $request, Closure $next)
3232
return $next($request);
3333
}
3434

35-
if (Cache::missing(app(OtpService::class)->generateVerifiedKey($user))) {
35+
$otpService = app(OtpService::class);
36+
37+
if (!Session::has($otpService->generateVerifiedKey($user))) {
38+
if (!Session::has($otpService->generateOtpSentKey($user))) {
39+
$otpService->generateOtpAndSend($user);
40+
}
41+
3642
if ($request->header('X-Inertia')) {
3743
return inertia()->location(route('2fa.index'));
3844
}

src/Support/OtpService.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,9 @@ public function generateVerifiedKey(Authenticatable $user): string
4747
{
4848
return get_class($user).'-'.$user->id.'2fa-verified';
4949
}
50+
51+
public function generateOtpSentKey(Authenticatable $user): string
52+
{
53+
return get_class($user).'-'.$user->id.'otp-sent';
54+
}
5055
}

0 commit comments

Comments
 (0)