Sunday, April 9, 2017

Laravel unauthenticated handler is not called

I found a strange behavior in my Laravel app.

I have routes like this :

Route::group(['as' => 'web', 'middleware' => ['web']], function()
{
    Route::group(['middleware'=>['auth']], function() {
        Route::get('personal', 'Web\MyController@personal');
    }
}

In my app/Exceptions/Handler.php, I defined the unauthenticated function like this :

protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }

    return redirect()->guest('login');
}

So when user is not authenticated and hit route with auth middleware, it should redirect to /login.

This works fine on local and development servers. But in production server, it never being called. On production server, if unauthenticated user hit route with auth middleware, it will be always redirected to /auth instead of /login.

Do you have any experience to this problem? Thank you



via Destiya

Advertisement