If you have been using the excellent
spatie/laravel-activitylog to track user activities in your Laravel application, then it is only make sense to also extend it to log all related authentication events. This includes tracking when a user:
- Logging in to the site
- Logging out from the site (including logging out from the current device or from the other devices)
- Failing to log in
- Registering to the site
- Verifying their email address
- Resetting their password
To get started, create a new event listener that we will then register in the
EventServiceProvider for all related auth events.
We can then register the newly created event listener in our
EventServiceProvider like so:
We are choosing these events simply because all of them has the same public property
$user that we will use to log the events. Once we have the listener registered, then we simply log these events via the
activity() helper provided by the package.
The listener class is fairly simple.
We simply convert the event name to the kebab-case version of it, so for example
OtherDeviceLogout will be stored as
login and so on. We also set the causer and subject to the same user.
You can see other available methods available in the official documentation but for our use cases, this is sufficient for our purposes.
The next time users are doing the above events, it will be recorded accordingly to the database.
Bonus - Checking user last login
To fetch the last login timestamp for a specific user, we can use the
Spatie/ActivityLog/Models\Activity to do so. Assuming the user is stored in the
$user variable, then retrieving the last login timestamp will be as simple as fetching the most recent related activity as below:
$activity = Activity::forSubject($user)->forEvent('login')->latest()->first();
The timestamp will be available as the
$activity->created_at property as you would expect.