DEV Community

Code And Deploy
Code And Deploy

Posted on • Updated on

Laravel 8 Logout For Your Authenticated User

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/laravel-8-logout-for-your-authenticated-user

Advanced Laravel SAAS Starter Kit with CRUD Generator

Advanced Laravel SAAS Starter Kit with CRUD Generator - GET YOUR COPY NOW!

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');
});
Enter fullscreen mode Exit fullscreen mode

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');
    }
}
Enter fullscreen mode Exit fullscreen mode

Now you have the logout functionality for your Laravel Authentication. I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/laravel-8-logout-for-your-authenticated-user if you want to download this code.

Advanced Laravel SAAS Starter Kit with CRUD Generator

Advanced Laravel SAAS Starter Kit with CRUD Generator - GET YOUR COPY NOW!

Happy coding :)

Top comments (0)