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

纵饮孤独 提交于 2019-11-27 16:05:38

问题


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 for Auth::check() and it doesn't change after calling the log out action.

I have this route inside the routes.php file:

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

and it's outside the

Route::group(['middleware' => ['web']], function () statement.

I did also try to add the follow action at the end of the AuthController.php file.

public function getLogout() 
{
    $this->auth->logout();
    Session::flush();
    return redirect('/');
}

Do you have any ideas?

EDIT 1

If I clear Google's Chrome cache, it works.


回答1:


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.




回答2:


use below code

Auth::logout();

or

auth()->logout();



回答3:


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.




回答4:


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 /




回答5:


This should be the content of your constructor in AuthController

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



回答6:


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.




回答7:


/**
 * 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();
}



回答8:


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



来源:https://stackoverflow.com/questions/34479994/laravel-5-2-authlogout-is-not-working

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