Laravel 5.2: Auth::logout() is not working

前端 未结 8 1473
孤独总比滥情好
孤独总比滥情好 2020-12-16 00:04

I\'m building a very simple app in Laravel 5.2, but when using AuthController\'s action to log out, it just simply doesn\'t work. I have a nav bar which checks

8条回答
  •  被撕碎了的回忆
    2020-12-16 00:22

    The problem is from the 'guest' middleware in the AuthController constructor. It should be changed from $this->middleware('guest', ['except' => 'logout']); to $this->middleware('guest', ['except' => 'getLogout']);

    If you check the kernel file, you can see that your guest middleware point to \App\Http\Middleware\RedirectIfAuthenticated::class

    This middleware checks if the user is authenticated and redirects the user to the root page if authenticated but lets the user carry out an action if not authenticated. By using $this->middleware('guest', ['except' => 'getLogout']); , the middleware will not be applied when the getLogout function is called, thereby making it possible for authenticated users to make use of it.

    N/B: As in the original answer, you can change getLogout to logout since the getLogout method simply returns the logout method in laravel's implementation.

提交回复
热议问题