Table of contents

Laravel 8 Logout

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.