Coder Social home page Coder Social logo

usrlastly's Introduction

UsrLastly

This nifty little package makes it easy to store and retrieve a user's 'last seen' status.

It's a light implementation that's easy to extend.

It uses a middleware to check if a user is logged in, if so it will store the user's current visit.

Installation

First add the package to your composer file by runningthis command:

composer require happydemon/usrlastly

This will install the latest version.

Next add the service provider in your app.php config:

    'providers' => [
        ...
        'HappyDemon\UsrLastly\UsrLastlyServiceProvider',
    ]

Next publish the config file:

php artisan vendor:publish

Now it's time to register LastSeenMiddleware as global middleware in app/Http/Kernel.php:

	protected $middleware = [
		...
		'HappyDemon\UsrLastly\Middleware\LastSeenMiddleware'
	];

Lastly let's move on to your user model, open it and add the required trait:

Above your class reference the trait

use HappyDemon\UsrLastly\LastSeenTrait as LastSeen;

and in your user model add

use LastSeen;

Storages

I've bundled 2 types of storage, this is where the 'last seen status' gets stored.

You can either store this status in your database or with Redis.

Database

To let this package do its work you'll first need to publish and run a database migration

php artisan vendor:publish --provider="HappyDemon\UsrLastly\UsrLastlyServiceProvider" --tag="migrations"
php artisan migrate

That should have prepared your database and you should be ready.

Redis

This storage requires even less setup, all you have to do is open up config/userlastly.php and change the storage key to redis

User provider

I'm sure not everybody is using Laravel's built-in Auth to handle user authentication so I've extracted this bit too, at this moment though it's the only auth implemented.

This is something that's used by the package, not something other developers should worry about.

We can do this in 3 steps:

  • Create a class that implements HappyDemon\UsrLastly\User with a getUser() method that returns a logged in user or false
  • Binding that class to laravel's container
  • Change config/userlastly.php's user_provider key to name of that binding.

You can check the docs for the worked out example.

Retrieving 'last seen' status

Load your user like you would normally and call lastSeen(), as an example I'll just grab the first user in the database:

$user = User::find(1);

dd($user->lastSeen());

That method should return an object with 2 properties:

  • date which is a Carbon date object
  • request, an array which holds information on where the user was

usrlastly's People

Contributors

happydemon avatar marijang avatar

Stargazers

Fomin Vasyl avatar Hamed Panjeh avatar Ignacio Muñoz Fernández avatar Kevin Dierkx avatar C-Bass avatar Summer avatar Nicolae avatar Gabriel Rausch avatar Diego Vargas avatar  avatar  avatar  avatar Rudolf Halas avatar Keith avatar

Watchers

 avatar  avatar Simonas avatar

usrlastly's Issues

Trait

It is good to add in trait getter

public function getLastSeenAttribute()
{
return app('UsrLastlyRepository')
->retrieve($this);
}

Thanks:D

COmposer

composer update not working

always download old version? Can you increment version?

Thanks:D

Middleware

In your middleware

if($user= app('UsrLastlyUser')->getUser() != false)

$user value is "true" so when you call app('UsrLastlyRepository')->store($user, $request);
he dosent know $user->getKey()

i set in middleware

$user = app('UsrLastlyUser')->getUser()
if($user != false)

and then works

Thanks

Docs

  • Introduction/Installation
  • Storages
    • Create your own
  • User providers
    • Create your own

redis

in Redis.php you need to add flag true
public function retrieve($user)
{
$data = json_decode($this->storage->get('usr.' . $user->getKey() . '.lastSeen'));

becomes
public function retrieve($user)
{
$data = json_decode($this->storage->get('usr.' . $user->getKey() . '.lastSeen'),true);

Thanks for great package:D

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.