Laravel 10: How to implement `Too Many Redirects` scenarios when using middleware for authentication?
I'm stuck on something that should probably be simple. After trying multiple solutions online, I still can't figure this out. I'm sure I'm missing something obvious here, but I'm working with a `Too Many Redirects` behavior when trying to implement custom middleware for user authentication in Laravel 10. My middleware checks if the user is authenticated; if not, it redirects them to the login page. However, it seems to create a redirect loop. Hereβs the middleware code: ```php namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\Auth; class RedirectIfNotAuthenticated { public function handle($request, Closure $next) { if (!Auth::check()) { return redirect('/login'); // redirect to login if not authenticated } return $next($request); } } ``` I've registered the middleware in `Kernel.php` like this: ```php protected $routeMiddleware = [ 'auth.check' => \App\Http\Middleware\RedirectIfNotAuthenticated::class, ]; ``` Then I applied it to a route group: ```php Route::group(['middleware' => ['auth.check']], function () { Route::get('/dashboard', [DashboardController::class, 'index']); }); ``` However, when I navigate to `/dashboard`, it keeps redirecting me back to `/login`, and when I try to access `/login`, it redirects me to `/dashboard` since I'm not logged in. I've tried using `dd(Auth::check())` inside the middleware, and it always returns `false` when accessing `/dashboard`. I also checked the authentication configuration in `config/auth.php`, but everything seems fine. Has anyone faced a similar scenario or can suggest what I might be missing? How would you solve this? This is part of a larger API I'm building. Any help would be greatly appreciated! I've been using Php for about a year now. Thanks for taking the time to read this! Any help would be greatly appreciated!