anubra266/browser-sessions

Manage Browser Sessions in a Laravel Application

v1.1.2 2020-12-25 21:43 UTC

This package is auto-updated.

Last update: 2024-04-07 10:27:57 UTC


README

GitHub Workflow Status Latest Version on Packagist License Total Downloads Author

Manage User Accounts' Browser Sessions in a Laravel Application.

Contents

Features

  • List Browser Sessions
  • Logout All Browser Sessions

Installation

You can install the package via composer:

composer require anubra266/browser-sessions

Edit config/session.php and change the Driver

'driver' => 'database'

Create Session Migration and Migrate

php artisan session:table

php artisan migrate

Make sure that the Illuminate\Session\Middleware\AuthenticateSession middleware is present and un-commented in your app/Http/Kernel.php class web middleware group:

'web' => [
    // ...
    \Illuminate\Session\Middleware\AuthenticateSession::class,
    // ...
],

You can publish the config file with:

php artisan vendor:publish --provider="Anubra266\BrowserSessions\BrowserSessionsServiceProvider" --tag="config"

Usage

Get Sessions

Backend

# SettingsController.php

//Get a collection of sessions
public function showSessions(){
    // This method accepts the request instance
    $sessions = BrowserSessions::collect(request());
    //Pass the collection to your view
    return view('sessions', ["sessions" => $sessions->all()]);

}

Output Format

{
    'agent' :{
        'is_desktop' : boolean,
        'platform' : string,
        'browser' : string,
    },
    'ip_address' : string,
    'is_current_device' : boolean,
    'last_active' : string,
}

Logout All Sessions

Send a Post Request to the named route browser.sessions.logout.

<form action="{{route('browser.sessions.logout')}}" method="post">
    @csrf
    <input type="password" placeholder="Enter your password" name="password" />
    <button>Logout All Devices</button>
</form>

NB:

  • You can change this named route by changing the value of the logoutAllSessions key in config/browser-sessions.php.
  • Validation errors are returned in errorBag named logoutOtherBrowserSessions. You can change this by editing the value of the errorBag key in config/browser-sessions.php.

Testing

composer test

Credits

License

The MIT License (MIT). Please see License File for more information.