Managing authentication securely is crucial for modern web apps. In this guide, we’ll create a custom admin login system using Laravel's guard, and enable a powerful security feature:
Logging out from all other devices on login using:
Let’s walk through it step by step.
📦 Step 1: Install Laravel and Setup Session
Configure .env:
Then run:
👤 Step 2: Create Admin Model and Table
Update migration file:
Run migration:
⚙️ Step 3: Add Admin Guard in config/auth.php
🧭 Step 4: Create Admin Login Controller
✅ app/Http/Controllers/Admin/Auth/LoginController.php
🛣️ Step 5: Define Admin Routes
routes/web.php
🖼️ Step 6: Blade Views
📄 resources/views/admin/login.blade.php
📄 resources/views/admin/dashboard.blade.php
🔐 Final Notes
Auth::guard('admin')->logoutOtherDevices($password) ensures that only the current session remains active after login or manual logout.- This works only if session driver is set to
database. - Always validate the password before using
logoutOtherDevices(). - Works for any guard (
web, admin, etc.).
Tags
Laravel
