Redirecting many times laravel 8

  laravel, laravel-8, php, redirect

I have encountered a lot of this error. but I can’t understand why that doesn’t work??
Here is the scenario: I want to make a middleware to prevent authenticated users from viewing the login and register form.
So, I already wrote the MustNotBeLoggedIn.php:

<?php

namespace AppHttpMiddlewareAuthenticateUsers;

use Closure;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;

class MustNotBeLoggedIn
{
    /**
     * Handle an incoming request.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if (session()->has('user')) {
            return redirect()->route('not_allowed');
        }
        return $next($request);
    }
}

and here is web.php:

Route::group(['prefix' => 'authenticate', 'middleware' => 'check.MustNotBeLoggedIn'], function () {
        // Login / Register Form
        Route::view('/login-register', 'authentication.login_register')->name('authenticate.login-register');

        // Login, Register, Logout
        Route::post('/login', [LoginAndRegisterController::class, 'login'])->name('authenticate.validate');
        Route::post('/register', [LoginAndRegisterController::class, 'register'])->name('authenticate.new');
        Route::post('/logout', [LoginAndRegisterController::class, 'logout'])->name('authenticate.logout');

        // Forget Password
        Route::view('/forget-password', 'authentication.forget_password')->name('authenticate.forget_password');
        Route::get('/password-reset/{token}', function ($token) {
            return view('authentication.reset_password', ['token' => $token]);
        })->name('password.reset');
        Route::post('/send-reset-link', [ResetPasswordController::class, 'requestLink'])->name('authenticate.send_reset_link');
        Route::post('/reset', [ResetPasswordController::class, 'resetPassword'])->name('authenticate.reset_password');
    });

and I registered the middleware in the kernel.php
'check.MustNotBeLoggedIn' => AppHttpMiddlewareAuthenticateUsersMustBeLoggedIn::class,

one last notice: The view not allowed hasn’t any middlewares

Route::view('/not_found', 'errors.404')->name('not_allowed');

Source: Laravel

Leave a Reply