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

血红的双手。 提交于 2019-11-29 01:36:21

I also had similar problem in Laravel 5.2. You should change your route to

Route::get('auth/logout', 'Auth\AuthController@logout');

or in AuthController constructor add

public function __construct()
{
    $this->middleware('guest', ['except' => ['logout', 'getLogout']]);
}

That worked for me.

Nimisha Molia

use below code

Auth::logout();

or

auth()->logout();

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.

Hekmat

In Http->Middleware->Authenticate.php change login in else statement to /

return redirect()->guest('/');

and define following route in routes.php

Route::get('/', function () {
    return view('login');
});

for logout call following function:

public function getlogout(){
    \Auth::logout();
    return redirect('/home');
}

Important: redirect to /home instead of / that first calls $this->middleware('auth'); and then in middleware redirect to /

Napoleon256611

This should be the content of your constructor in AuthController

$this->middleware('web');
$this->middleware('guest', ['except' => 'logout']);

Simply add below route and do not add this inside any route group(middleware):

Route::get('your-route', 'Auth\AuthController@logout');

Now logout should work as it should in L 5.2 without modifying anything in AuthController.

/**
 * Log the user out of the application.
 *
 * @param \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function logout(Request $request)
{
    $this->guard()->logout();

    $request->session()->flush();

    $request->session()->regenerate();

    return redirect('/');
}

/**
 * Get the guard to be used during authentication.
 *
 * @return \Illuminate\Contracts\Auth\StatefulGuard
 */
protected function guard()
{
    return Auth::guard();
}
Nassima Noufail

add this line in routes.php file Route::get('auth/logout', 'Auth\AuthController@getLogout'); and add this in your view <a href="{{ url('/auth/logout') }}" > Logout </a> it works fine for me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!