Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 739
Felipe Peña Avatar asked Dec 27 '15 11:12

Felipe Peña


People also ask

How do I log into Laravel with Auth?

How do I enable authentication in Laravel? You need to Install the laravel/ui Composer bundle and run php artisan ui vue –auth in a new Laravel application. After migrating your database, open http://your-app.test/register or any other URL that's assigned to your application on your browser.

How do I logout of all devices in Laravel?

This method requires the user to provide their current password, which your application should accept through an input form: use Illuminate\Support\Facades\Auth; Auth::logoutOtherDevices(request('password')); When the logoutOtherDevices method is invoked, the user's other sessions will be invalidated entirely, meaning ...


2 Answers

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.

like image 193
Aztecnologic Avatar answered Oct 08 '22 04:10

Aztecnologic


use below code

Auth::logout();

or

auth()->logout();
like image 32
Nimisha Molia Avatar answered Oct 08 '22 04:10

Nimisha Molia