Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ghaleon's avatar

Redirecting the user if not authenticated

I'm using laravel 5.0 and I'm trying to check if the user is authenticated. If he is, then he can proceed to the page. But if he is NOT authenticated, I wan to redirect him to my login page. Can't figure out what's wrong:

If I try to access http://localhost/chdps/public

Route::group(['middleware' => 'auth' ], function(){
    Route::get('/', [
        'as' => 'home', 'uses' => 'MainController@index'
    ]);
});

Then the URL become: http://localhost/chdps/public/auth/login and I get an error:

NotFoundHttpException in compiled.php line 7959:

Maybe because I do not have that route registered, I don't know... Is there a place where I should customize my login url ?

Don't know how to fix it. Already read what is in Authentication.

0 likes
2 replies
ahuggins's avatar

If you do not have the /auth/login route registered, that's probably why you are getting this error.

You should be able to run php artisan make:auth and it will scaffold a basic authentication system for you that should include that route.

Then if you want to specify things, you can edit some things in the AuthController.php file.

1 like
Ghaleon's avatar

@ahuggins Thanks for the reply !

No idea why, but php artisan make:auth gives me nothing but error. I'm implementing my own "auth system". Guess I found the problem: App/Http/Middleware/Authenticate.php

public function handle($request, Closure $next)
    {
        if ($this->auth->guest())
        {
            if ($request->ajax())
            {
                return response('Unauthorized.', 401);
            }
            else
            {
                return redirect()->guest('/login');  <- I JUST EDITED THIS
            }
        }

        return $next($request);
    }

Please or to participate in this conversation.