How to change default redirect URL of Laravel 5 Auth filter?

后端 未结 10 1906
终归单人心
终归单人心 2020-12-04 20:17

By default if I am not logged and I try visit this in browser:

http://localhost:8000/home

It redirect me to http://localhost:8000/aut

10条回答
  •  粉色の甜心
    2020-12-04 20:45

    In Laravel 5.6, go to app/Exceptions folder and open the Handler.php, add a new method that overrides the unauthenticated method like so:

    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if($request->ajax())
        {
            return response([
                "message" => "Unauthenticated.",
                "data" => [],
            ],401);
        }
    
        return redirect()->to('/');
    }
    

    This method is triggered when you access a protected route using the built-in "auth" middleware. Now you will have full control where to redirect or the response sent.

提交回复
热议问题