How to manually log users out of your application in laravel

Created at 09-Sep-2020 , By samar

 By using this method you can simply destroy the current logged in user session . To manually log users out of your application, you may use the logout method on the Auth facade. This will clear the authentication information in the user's session.

This article is also helpful for those developer who logged in but do not have logged out option in their application or any situation in which they want to logged out their current logged in user session. They can simply use this method to logged out from the current user's session. 

For this we have to create a route  and a controller method .

Copy and paste this code to your web.php file under < yourproject/routes/ > folder or may be different as per your laravel version. 

Route::get('/logout-user-session', 'LogoutController@Logout');

To create LogoutController file you can run php artisan command.

php artisan make:controller LogoutController

Copy and paste this code in your LogoutController file after the namespace and before the class defination.

use Illuminate\Support\Facades\Auth;

After that you have to creat logout method in your LogoutController file. You can simply copy and paste below code in your LogoutController file. 

public function Logout(){

     Auth::logout();
     return redirect('/login');

}

You have to simply hit /logout-user-session  and now you are logged out of your current logged in user session.

 

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.