Logging a user out in Laravel is very simple, but there are a couple of best-practice steps to make sure the session is completely invalidated and CSRF-protected.
- Add a logout route (POST is recommended)
// routes/web.php
use App\Http\Controllers\Auth\LogoutController;
Route::post('/logout', LogoutController::class)
->middleware('auth')
->name('logout');
📝 Why POST?
• Laravel’s VerifyCsrfToken middleware protects POST, PUT, PATCH, DELETE requests.
• Browsers and proxies sometimes pre-fetch GET links—logging a user out unintentionally.
- Create the controller (or method) that actually logs the user out
// app/Http/Controllers/Auth/LogoutController.php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class LogoutController
{
public function __invoke(Request $request)
{
// 1) Log the user out of the guard (default is 'web')
Auth::logout();
// 2) Invalidate the current session
$request->session()->invalidate();
// 3) Regenerate the CSRF token (prevents “session fixation” replay)
$request->session()->regenerateToken();
// 4) Redirect wherever you like
return redirect('/'); // or ->route('login')
}
}
- Add a logout button in your Blade view
<form method="POST" action="{{ route('logout') }}">
@csrf
<button type="submit" class="btn btn-link">
Log Out
</button>
</form>
That’s it! When the button is pressed, Laravel:
- Verifies the CSRF token
- Calls your LogoutController
- Executes
Auth::logout() and kills the session
Quick notes / gotchas
• Multiple guards?
Auth::guard('admin')->logout() for a custom guard.
• Jetstream / Breeze / Fortify?
They already ship with the equivalent route & controller—just use the <x-dropdown-link href="{{ route('logout') }}" …> component Jetstream gives you, or the logout form Breeze scaffolds.
• Ajax logout?
Send a POST request to /logout with the CSRF token in headers.
• Legacy (GET) logout links?
If you must:
Route::get('/logout', function(Request $request){ … })->name('logout');
…but prefer POST for security reasons.
Now your users can sign out safely and completely. Happy coding! 🎉