|
4 | 4 |
|
5 | 5 | namespace Hypervel\Auth; |
6 | 6 |
|
7 | | -use Hypervel\Auth\Middleware\Authenticate; |
8 | | -use Hypervel\Auth\Middleware\RedirectIfAuthenticated; |
9 | | -use Hypervel\Session\Middleware\AuthenticateSession; |
| 7 | +use Closure; |
| 8 | +use Hypervel\Http\Request; |
| 9 | +use Hypervel\Routing\Exceptions\UrlGenerationException; |
10 | 10 | use Hypervel\Support\Attributes\BootOnly; |
| 11 | +use Hypervel\Support\Facades\Route; |
11 | 12 |
|
12 | | -/** |
13 | | - * Single source of truth for boot-time auth redirect wiring. |
14 | | - * |
15 | | - * Configures the worker-lifetime redirect callbacks on the auth/guest |
16 | | - * middleware and the authentication exception; it does not resolve or issue |
17 | | - * redirects at runtime. |
18 | | - */ |
19 | 13 | class AuthenticationRedirects |
20 | 14 | { |
| 15 | + /** @var null|Closure(Request): ?string */ |
| 16 | + protected static ?Closure $guestDestinationCallback = null; |
| 17 | + |
| 18 | + /** @var null|Closure(Request): string */ |
| 19 | + protected static ?Closure $userDestinationCallback = null; |
| 20 | + |
21 | 21 | /** |
22 | | - * Configure where guests are redirected by the "auth" middleware. |
| 22 | + * Set the shared login destination for the worker lifetime. |
23 | 23 | * |
24 | | - * The callback persists in authentication middleware and exception |
25 | | - * static properties for the worker lifetime and affects every |
26 | | - * subsequent unauthenticated or session-mismatch request. |
| 24 | + * Pass null to disable guest redirects. |
27 | 25 | */ |
28 | 26 | #[BootOnly] |
29 | 27 | public static function redirectGuestsTo(callable|string|null $redirect): void |
30 | 28 | { |
31 | | - $redirect = $redirect === null |
32 | | - ? static fn () => null |
33 | | - : $redirect; |
34 | | - |
35 | | - self::redirectTo(guests: $redirect); |
| 29 | + static::$guestDestinationCallback = is_string($redirect) || $redirect === null |
| 30 | + ? static fn () => $redirect |
| 31 | + : Closure::fromCallable($redirect); |
36 | 32 | } |
37 | 33 |
|
38 | 34 | /** |
39 | | - * Configure where users are redirected by the "guest" middleware. |
40 | | - * |
41 | | - * The callback persists in the guest middleware static property for the |
42 | | - * worker lifetime and affects every subsequent already-authenticated |
43 | | - * guest-route request. |
| 35 | + * Set the shared signed-in destination for the worker lifetime. |
44 | 36 | */ |
45 | 37 | #[BootOnly] |
46 | 38 | public static function redirectUsersTo(callable|string $redirect): void |
47 | 39 | { |
48 | | - self::redirectTo(users: $redirect); |
| 40 | + static::$userDestinationCallback = is_string($redirect) ? static fn () => $redirect : Closure::fromCallable($redirect); |
49 | 41 | } |
50 | 42 |
|
51 | 43 | /** |
52 | | - * Configure where users are redirected by the authentication and guest middleware. |
53 | | - * |
54 | | - * The callbacks persist in authentication middleware and exception |
55 | | - * static properties for the worker lifetime and affect every |
56 | | - * subsequent matching request. |
| 44 | + * Configure the shared login and signed-in destinations for the worker lifetime. |
57 | 45 | */ |
58 | 46 | #[BootOnly] |
59 | 47 | public static function redirectTo(callable|string|null $guests = null, callable|string|null $users = null): void |
60 | 48 | { |
61 | | - $guests = is_string($guests) ? fn () => $guests : $guests; |
62 | | - $users = is_string($users) ? fn () => $users : $users; |
| 49 | + if ($guests !== null) { |
| 50 | + static::redirectGuestsTo($guests); |
| 51 | + } |
| 52 | + |
| 53 | + if ($users !== null) { |
| 54 | + static::redirectUsersTo($users); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Resolve the login destination, if the application provides one. |
| 60 | + */ |
| 61 | + public static function guestDestination(Request $request): ?string |
| 62 | + { |
| 63 | + if (static::$guestDestinationCallback !== null) { |
| 64 | + return (static::$guestDestinationCallback)($request); |
| 65 | + } |
| 66 | + |
| 67 | + return static::namedDestination('login'); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Resolve the signed-in destination for the current request. |
| 72 | + */ |
| 73 | + public static function userDestination(Request $request): string |
| 74 | + { |
| 75 | + if (static::$userDestinationCallback !== null) { |
| 76 | + return (static::$userDestinationCallback)($request); |
| 77 | + } |
| 78 | + |
| 79 | + foreach (['dashboard', 'home'] as $uri) { |
| 80 | + if (($destination = static::namedDestination($uri)) !== null) { |
| 81 | + return $destination; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + $routes = Route::getRoutes()->get('GET'); |
| 86 | + |
| 87 | + foreach (['dashboard', 'home'] as $uri) { |
| 88 | + if (isset($routes[$uri])) { |
| 89 | + return '/' . $uri; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return '/'; |
| 94 | + } |
63 | 95 |
|
64 | | - if ($guests) { |
65 | | - Authenticate::redirectUsing($guests); |
66 | | - AuthenticateSession::redirectUsing($guests); |
67 | | - AuthenticationException::redirectUsing($guests); |
| 96 | + /** |
| 97 | + * Resolve a conventional route when its required parameters are available. |
| 98 | + */ |
| 99 | + protected static function namedDestination(string $name): ?string |
| 100 | + { |
| 101 | + if (! Route::has($name)) { |
| 102 | + return null; |
68 | 103 | } |
69 | 104 |
|
70 | | - if ($users) { |
71 | | - RedirectIfAuthenticated::redirectUsing($users); |
| 105 | + try { |
| 106 | + return route($name); |
| 107 | + } catch (UrlGenerationException) { |
| 108 | + return null; |
72 | 109 | } |
73 | 110 | } |
| 111 | + |
| 112 | + /** |
| 113 | + * Flush all static state. |
| 114 | + */ |
| 115 | + public static function flushState(): void |
| 116 | + { |
| 117 | + static::$guestDestinationCallback = null; |
| 118 | + static::$userDestinationCallback = null; |
| 119 | + } |
74 | 120 | } |
0 commit comments