0
votes

I'm using rappasoft/laravel-5-boilerplate.

Have a weird issue where I'm using the 'auth' middleware for routes. Testing access to a page that requires login. When not logged in it displays the Unauthenticated exception error, and does not redirect to Login screen. In the Handler.php file unauthenticated exists and has code to redirect to the login screen, but this function is never called!

Why would the unauthenticated function not be called on the Illuminate\ Auth\AuthenticationException?? It does't make sense.

The route:

Route::group(['middleware' => 'auth'], function () {
    Route::get('project/add', 'Project\ProjectController@create')-
>name('project.add');
});

Exceptions/Handler.php

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

    return redirect()->guest( route( 'frontend.auth.login' ) );
}

And then to test, I'm simply visiting: http://px.app/project/add

2
Could you edit your question to include an MVCE? It's difficult for us to find any clues about what you've missed without seeing your code. - HPierce
added code snippets - zetetic

2 Answers

0
votes

To get around this, I've put in the code that checks authentication into the render method, so now it correctly calls unauthenticated when triggered by the exception.

Very weird it couldn't do this before by default.

0
votes

Ensure you've included AuthenticationException::class in the $dontReport array in your Exceptions/Handler.php.