Skip to content

Commit 8ec5e84

Browse files
committed
wip
1 parent 8697b0b commit 8ec5e84

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

config/otp.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,27 @@
22

33
return [
44

5+
/**
6+
* Whether the package will register the routes and listeners.
7+
*/
58
'enabled' => false,
69

10+
/**
11+
* The notification to be sent to the logged-in user.
12+
* Override this with your own implementation so that
13+
* you can customize the channels, message format etc.
14+
*/
715
'notification' => \Chrysanthos\LaravelOtp\Notifications\SendOtpToUserNotification::class,
816

17+
/**
18+
* The paths that should be protected by otp. This must be
19+
* relative paths with no slashes at the start of the string.
20+
* Use this option in case you have admin login area with tools
21+
* like Nova/Backpack/Filament and only nedd otp in the main site
22+
*
23+
* Sadly the paths must point to the uri path of the route that the login form is submitted to.
24+
*/
25+
'paths' => [
26+
'*',
27+
]
928
];

src/Middleware/RedirectToOtpPage.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public function handle(Request $request, Closure $next)
3434

3535
$otpService = app(OtpService::class);
3636

37+
if (!$otpService->shouldCoverRoutePath($request->path())) {
38+
return $next($request);
39+
}
40+
3741
if (! Session::has($otpService->generateVerifiedKey($user))) {
3842
if (! Session::has($otpService->generateOtpSentKey($user))) {
3943
$otpService->generateOtpAndSend($user);

src/Support/OtpService.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Contracts\Auth\Authenticatable;
66
use Illuminate\Foundation\Auth\User;
7+
use Illuminate\Support\Arr;
78
use Illuminate\Support\Facades\Session;
89

910
class OtpService
@@ -52,4 +53,15 @@ public function generateOtpSentKey(Authenticatable $user): string
5253
{
5354
return get_class($user).'-'.$user->id.'otp-sent';
5455
}
56+
57+
public function shouldCoverRoutePath(string $path): bool
58+
{
59+
$paths = Arr::wrap(config('otp.paths'));
60+
61+
if (!is_array($paths) || ($paths[0] ?? null) === '*') {
62+
return true;
63+
}
64+
65+
return in_array($path, $paths, true);
66+
}
5567
}

0 commit comments

Comments
 (0)