Code Monkey home page Code Monkey logo

browser-sessions's Introduction

Latest Version on Packagist Total Downloads Packagist PHP Version Laravel Version

Warning

This package can only be used with the database driver for the Sessions. This is how it is done in Jetstream, so keep this in mind as it may turn you off knowing you need to manage sessions in the database.

Logout Other Browser Sessions

This package allows you to log out sessions that are active on other devices.

You may find this useful if you have logged in on a different device, or you have let someone else use your account, or you have forgotten to log out of a public computer. It can especially be useful if you see suspicious device activity on your account.

Note

This code has been extracted from Laravel Jetstream and cannot be used outside a Laravel application.

Installation

You can install the package via Composer:

composer require cjmellor/browser-sessions

Usage

Retrieving A User's Current Sessions

Use the BrowserSessions facade to retrieve all the current user's sessions:

BrowserSessions::sessions();

This will return an object with some information about each session:

[
  {
    "device": {
      "browser": "Safari",
      "desktop": true,
      "mobile": false,
      "platform": "OS X"
    },
    "ip_address": "127.0.0.1",
    "is_current_device": true,
    "last_active": "1 second ago"
  }
]

Logging Out Other Browser Sessions

Use the BrowserSessions facade to log out all the user's other browser sessions:

BrowserSessions::logoutOtherBrowserSessions();

Note

A password must be sent along to the method to confirm the user's identity. Only then will the sessions be removed. See below on how you would implement this.

Views

The package does not come with any pre-defined views to use. Here is an example though on how this could be implemented

In your routes/web.php file add the following route:

Route::delete('logout-browser-sessions', function () {
    BrowserSessions::logoutOtherBrowserSessions();

    return back()->with('status', 'Logged out of other browser sessions.');
})->name('logout-browser-sessions');

Then in your view, you can add a form to submit a DELETE request to the above route:

<form method="POST" action="{{ route('logout-browser-sessions') }}">
    @csrf
    @method('DELETE')
    
    <x-text-input label="Password" name="password" placeholder="Enter password" type="password" />
    
    <button type="submit">Logout Other Sessions</button>
</form>

Retrieve the Users' Last Activity

Get the users' last activity by using the getUserLastActivity method:

BrowserSessions::getUserLastActivity();

You can also view the date in a human-readable format:

BrowserSessions::getUserLastActivity(human: true);

Credits

License

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

browser-sessions's People

Contributors

cjmellor avatar dependabot[bot] avatar github-actions[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.