Table of contents
In my previous post, we implement the authentication, now we will talk about Laravel auth logout. Logout is one of the important functionality to implement in a web application when users log in they should have an option to log out of their account and secure it.
Â
To shorten this post please follow my previous post here.
Â
Step 1: Create a route
Navigate routes/web.php then put the following code below:
Route::group(['middleware' => ['auth']], function() {
/**
* Logout Route
*/
Route::get('/logout', 'LogoutController@perform')->name('logout.perform');
});
Â
Step 2: Create a LogoutController
Navigate app/Http/Controllers directory then create a file called LogoutController.php then paste the code below:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
class LogoutController extends Controller
{
/**
* Log out account user.
*
* @return \Illuminate\Routing\Redirector
*/
public function perform()
{
Session::flush();
Auth::logout();
return redirect('login');
}
}
Â
Now you have the logout functionality for your Laravel Authentication I hope it helps.
Â
Thank you for reading.
Read next