Unable to logout from Laravel

Viewed 33

I am new in laravel(using version 8) and working on "Logout section" but unable to logout,still getting email in session (after logout),How can i logout ? Here is my view file

<a class="dropdown-item" href="{{ route('logout') }}">
<i class="typcn typcn-power text-primary"></i>
    Logout
</a>

Here is my Routes(web.php)

Route::get('logout', [AdminController::class, 'logout'])->name('logout'); 

Here is my controller code,How can i logout ? Thank you in advance.

function logout()
    {
        Auth()->logout();
        return redirect('/')->with('logout_message', 'You have been logged out');;
    }
1 Answers

You should not only logout, but also invalidate the session.

Use something like this, from the Laravel documentation:

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
 
/**
 * Log the user out of the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function logout(Request $request)
{
    Auth::logout();
 
    $request->session()->invalidate();
 
    $request->session()->regenerateToken();
 
    return redirect('/');
}
Related