13

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.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Felipe Peña
  • 2,712
  • 4
  • 22
  • 41
  • What does it mean "doesn't work"? Could you explain in details what action do you run and what exactly doesn't work? – Marcin Nabiałek Dec 27 '15 at 11:35
  • Hello @MarcinNabiałek. I edited the question. I hope it's better explained. – Felipe Peña Dec 27 '15 at 11:39
  • Your logout route should not be outside the `web` middleware group. It should be inside it. – Thomas Kim Dec 27 '15 at 18:11
  • @ThomasKim still doesn't work – Felipe Peña Dec 27 '15 at 18:13
  • what does `Auth::logout()` do? is it bring the same result? although it should bring the same result. anyway, could you post entire `AuthController` code? – Bagus Tesa Dec 28 '15 at 06:38
  • If anyone comes here in the future still not finding joy in the answers: should you be using Auth::logout() or auth()->logout() -- try running session()->flush AND session()->save after. Much Googling brought me to find that, and it works a charm! – Max May 05 '20 at 19:28

8 Answers8

38

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.

Aztecnologic
  • 381
  • 2
  • 2
  • Thanks. I will check this later and if it works, I will accept it as the answer. – Felipe Peña Jan 08 '16 at 15:53
  • @Aztecnologic This fixed *my* issue. Don't know why we have to write a custom logout method in Laravel 5.2 though—I didn't have to in 5.1. Oh well. – tenub Jan 13 '16 at 15:35
  • Thanks. Solved my problem, too. Any explanation why? Seems to be linked to updating from 5.1 to 5.2 while using the old laravel/laravel boilerplate repository. – Karl Lorey Mar 05 '16 at 13:30
  • In addition to above answer, You can customize redirection after logging out by adding below code in AuthController protected $redirectAfterLogout = 'auth/login'; – Tayyab Hussain Mar 26 '16 at 10:41
  • 3
    Adding `getLogout` to the `except` array did the trick for me. – Mouagip Apr 25 '16 at 14:15
6

use below code

Auth::logout();

or

auth()->logout();
kamal pal
  • 4,187
  • 5
  • 25
  • 40
4

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.

nonybrighto
  • 8,752
  • 5
  • 41
  • 55
2

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 /

kamal pal
  • 4,187
  • 5
  • 25
  • 40
Hekmat
  • 153
  • 5
1

This should be the content of your constructor in AuthController

$this->middleware('web');
$this->middleware('guest', ['except' => 'logout']);
kamal pal
  • 4,187
  • 5
  • 25
  • 40
0

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

Rohit Suthar
  • 3,528
  • 1
  • 42
  • 48
Nassima Noufail
  • 177
  • 1
  • 2
0

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.

im_tsm
  • 1,965
  • 17
  • 29
0
/**
 * 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();
}
Mehran
  • 137
  • 3
  • 9