Laravel 5.2: Auth::logout() is not working
Solution 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.
Solution 2
use below code
Auth::logout();
or
auth()->logout();
Solution 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.
Solution 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 /
Solution 5
This should be the content of your constructor in AuthController
$this->middleware('web');
$this->middleware('guest', ['except' => 'logout']);
Felipe Peña
Felipe Peña is an entrepreneur, musician and iOS developer, currently building and creating new mobile apps and businesses. With more than 8 years of experience in software development, he has been supported and financed by public and private entities such as Wayra (Teléfonica incubator) and CORFO (chilean government). When he’s not in front of his PC, he enjoys helping other startups and entrepreneurs, listening to new music or cooking.
Updated on May 01, 2020Comments
-
Felipe Peña almost 4 years
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 forAuth::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.
-
Felipe Peña over 8 yearsThanks. I will check this later and if it works, I will accept it as the answer.
-
tenub over 8 years@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.
-
lorey about 8 yearsThanks. 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.
-
Tayyab Hussain about 8 yearsIn addition to above answer, You can customize redirection after logging out by adding below code in AuthController protected $redirectAfterLogout = 'auth/login';
-
Mouagip about 8 yearsAdding
getLogout
to theexcept
array did the trick for me.