Search code examples
phplaravellaravel-5.3

Laravel 5.3 redirect if not logged in


I'm using Laravel 5.3 with Valet.

web.php

Auth::routes();
Route::group(['middleware' => 'auth'], function () {
  Route::get('/dashboard', 'DashboardController@index');
});

Kernel.php

protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];

If I hit the /dashboard route not logged in, it throws an exception rather than redirecting me to the /login page. exception

Is this the correct behaviour or am I missing something? I thought it was supposed to redirect you to the login page, no?


Solution

  • Yes, it should redirect. A fresh install will redirect when not logged in and when it's not an api request.

    It seems it is no longer capturing the error and instead allowing it to surface all the way to the top.

    Possibly you have made changes to your App\Exceptions\Handler.php code and is causing it to skip past handling these errors correctly.