Laravel Tutorials

Managing Multi-Device Sessions with Laravel's Device Logout Feature

Published
Managing Multi-Device Sessions with Laravel's Device Logout Feature image

Laravel offers a robust security feature through Auth::logoutOtherDevices() that enables users to terminate their sessions across all devices except the current one. This capability is particularly valuable for maintaining account security in applications handling sensitive data.

You can implement this feature for proactive security measures, like responding to suspicious activities:

public function secureSessions(Request $request)
{
    Auth::logoutOtherDevices($request->password);
    
    return back()->with('status', 'All other device sessions terminated');
}

The implementation requires the auth.session middleware for proper session management:

Route::middleware(['auth', 'auth.session'])->group(function () {
    // Protected routes
});

Here's a practical implementation for password updates with multi-device logout:

class SecurityController extends Controller
{
    public function updatePassword(Request $request)
    {
        $validated = $request->validate([
            'current_password' => 'required',
            'new_password' => 'required|min:8|confirmed'
        ]);
        if (!Hash::check($request->current_password, Auth::user()->password)) {
            return back()->withErrors([
                'current_password' => 'Invalid password provided'
            ]);
        }
        Auth::logoutOtherDevices($request->current_password);
        Auth::user()->update([
            'password' => Hash::make($request->new_password)
        ]);
        return redirect('/dashboard')
            ->with('status', 'Password updated and other devices logged out');
    }
}

This approach provides users with greater control over their account security while helping prevent unauthorised access through forgotten active sessions.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
Slow Job Alerts and Replica Commit IDs in Laravel 13.40 image

Slow Job Alerts and Replica Commit IDs in Laravel 13.40

Read article
Flock: A Free, Native Herd Alternative for Laravel image

Flock: A Free, Native Herd Alternative for Laravel

Read article
Live Props for Real-Time Updates in Inertia.js v3.11 image

Live Props for Real-Time Updates in Inertia.js v3.11

Read article
Cooldown Dependency Policy now in Composer 2.11 image

Cooldown Dependency Policy now in Composer 2.11

Read article
Email Sending and Testing for Laravel in One Account image

Email Sending and Testing for Laravel in One Account

Read article
Decide with Jev: Tie Every AI Check to the Exact Draft image

Decide with Jev: Tie Every AI Check to the Exact Draft

Read article