Download the PHP package dev-moez/auth-last-activity without Composer
On this page you can find all versions of the php package dev-moez/auth-last-activity. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dev-moez/auth-last-activity
More information about dev-moez/auth-last-activity
Files in dev-moez/auth-last-activity
Package auth-last-activity
Short Description A package to record user last activity for multi guard applications
License MIT
Informations about the package auth-last-activity
Auth Last Activity
👱 🕑 Multi-auth last activity laravel package
Table of contents
- :book: Introduction
- :sparkles: Features
- :toolbox: Installation
- :gear: Configuration
- :rocket: Usage
- Getting user's last activity
- Getting a user status
- Getting online users
- Getting offline users
- Getting online users with period
- Getting user online status using
cache
:book: Introduction
- Getting user's last activity
- Getting a user status
- Getting online users
- Getting offline users
- Getting online users with period
- Getting user online status using
cache
AuthLastActivity is a Laravel package that supports multiple authentication guards to record the last activity for your application users.
:sparkles: Features
- Supports multiple authentication guards
- Adjustable database connection
- Ability to set seconds for the status of online users
:toolbox: Installation
You can install the package via composer:
The package will automatically register itself.
And to publish the config and the migration files needed for the package, run the following artisan command:
Or Manually:
You can publish the config file using the following command:
This is the contents of the published config file:
You can publish the migration file using the following command:
After publishing the migration file, you can create the user_last_activities
table by running the migrations:
:gear: Configuration
Step 1
To start using the package you have to add the middleware AuthLastActivityMiddleware
to your Kernel.php
which is located in your project's app\Http
directory.
And to make sure that all requests via web or api are considered, you should add that middleware in both arrays web
& api
, like so:
Step 2
Add HasLastActivity
trait to each authenticatable model you have in your laravel project, like User
and any other model you would use for a different guard for authentication, example on User
model:
After doing this, you are ready to go :rocket:!
:rocket: Usage
Getting User Last Activity
Once you have installed and configured the package, it will automatically track the last activity time of authenticated users through your application. You can access the last activity time using the lastActivity
one-to-one relation on the User
model - or any other authenticatable models e.g: Admin
...etc, like so:
This will return a collection of the last activity data:
authenticatable_type
is the authenticatable model used for the recordauthenticatable_id
is the authenticatable id used for the recordlast_activity_url
is the last URL the user has visitedlast_activity_time
is the datetime for the last activity for the useruser_agent
is user agent data from requestip_address
is the IP Address that user has used for his last visited urlheaders
is the request headers, in case of you needed itis_mobile
is a boolean attribute to let you know if the last activity has been done via a mobile device or not.request_source
is an enum value ofapi
orweb
previous_url
is the URL that the user visited before his last activity's URL.
Getting User Status
To get the online status of a user you can use a method called isOnline
to do so:
Getting Online Users
To get all online users you can use a static method called getOnline
, like:
Getting Offline Users
To get all offline users you can use a static method called getOffline
, like:
Please note that getting online and offline users are based on the seconds
value of online-period
which you can find in the auth-last-activity.php
config file.
Getting Online Users Within Period
To get online users within a custom period of seconds you can use the static method of activeWithin(int $seconds)
giving the seconds that you want to use.
Again, this will return the eloquent builder not the collection for the User
model within the last two minutes. If you want the collection then follow the previous code with ->get()
:
Getting User Online Status Using cache
To get the online status of a user using cache
, you can do by passing true
to the argument of viaCahce
in the isOnline
method:
Or in your code you can use the following piece of code to achieve that:
What is the authenticatable name?
It's the kebab/lowercase of your model's base name.
Let's say your project has two authenticatable models:
User
the default authenticatable model for users in any laravel projectAdmin
which you created for another guard just for administrators
Then for the first case the $authenticatable_name
should be 'user' and for the second case the $authenticatable_name
should be 'admin'.
And the $authenticatable_id
is the primary key for this record in the database for sure.
Examples:
- 'online-auth-user-10'
- 'online-auth-admin-1000'
You can get the authenticatable name for any authenticatable model using a static method called getAuthenticatableName()
.
:question: Why this package returns the Eloquent Builder not a Collection?
This approach is suitable for large datasets as it retrieves only the necessary data from the database, making it faster and more efficient.
So if you would like to make any further operations such as filtering, mapping ...etc, it would be better to perform on a builder not a collection, after finishing you can call get()
method to return the collection.
Updating
Want to update to the latest version?
Uninstallation
License
The MIT License (MIT).
Contributing
Contributions are welcome! If you would like to contribute to the package with a new feature or any other enhancement, please fork the repository and submit a pull request.
- Fork the Project
- Create your feature branch (git checkout -b feature/new-feature-name)
- Commit your changes (git commit -m 'Add and extra feature')
- Push to the branch (git push origin feature/new-feature-name)
- Open a pull request
And be sure that any contributions or comments you make are highly appreciated.
Contact
Abdelrahman Moez (aka Moez) - [email protected]