I'm currently available for full time hire! Inquire Here

Laravel Authentication Log Documentation

🎉 Enjoying this package? Consider sponsoring me on GitHub or buying me a beer.

Configuration

Publishing Assets

You can publish and run the migrations with:

1php artisan vendor:publish --provider="Rappasoft\LaravelAuthenticationLog\LaravelAuthenticationLogServiceProvider" --tag="authentication-log-migrations"
2php artisan migrate

You can publish the view/email files with:

1php artisan vendor:publish --provider="Rappasoft\LaravelAuthenticationLog\LaravelAuthenticationLogServiceProvider" --tag="authentication-log-views"

You can publish the config file with:

1php artisan vendor:publish --provider="Rappasoft\LaravelAuthenticationLog\LaravelAuthenticationLogServiceProvider" --tag="authentication-log-config"

This is the contents of the published config file:

1return [
2 // The database table name
3 // You can change this if the database keys get too long for your driver
4 'table_name' => 'authentication_log',
5 
6 // The database connection where the authentication_log table resides. Leave empty to use the default
7 'db_connection' => null,
8 
9 // The events the package listens for to log (as of v1.3)
10 'events' => [
11 'login' => \Illuminate\Auth\Events\Login::class,
12 'failed' => \Illuminate\Auth\Events\Failed::class,
13 'logout' => \Illuminate\Auth\Events\Logout::class,
14 'logout-other-devices' => \Illuminate\Auth\Events\OtherDeviceLogout::class,
15 ],
16 
17 'notifications' => [
18 'new-device' => [
19 // Send the NewDevice notification
20 'enabled' => env('NEW_DEVICE_NOTIFICATION', true),
21 
22 // Use torann/geoip to attempt to get a location
23 'location' => true,
24 
25 // The Notification class to send
26 'template' => \Rappasoft\LaravelAuthenticationLog\Notifications\NewDevice::class,
27 ],
28 'failed-login' => [
29 // Send the FailedLogin notification
30 'enabled' => env('FAILED_LOGIN_NOTIFICATION', false),
31 
32 // Use torann/geoip to attempt to get a location
33 'location' => true,
34 
35 // The Notification class to send
36 'template' => \Rappasoft\LaravelAuthenticationLog\Notifications\FailedLogin::class,
37 ],
38 ],
39 
40 // When the clean-up command is run, delete old logs greater than `purge` days
41 // Don't schedule the clean-up command if you want to keep logs forever.
42 'purge' => 365,
43];

If you installed torann/geoip you should also publish that config file to set your defaults:

1php artisan vendor:publish --provider="Torann\GeoIP\GeoIPServiceProvider" --tag=config

Setting up your model

You must add the AuthenticationLoggable and Notifiable traits to the models you want to track.

1use Illuminate\Notifications\Notifiable;
2use Rappasoft\LaravelAuthenticationLog\Traits\AuthenticationLoggable;
3use Illuminate\Foundation\Auth\User as Authenticatable;
4 
5class User extends Authenticatable
6{
7 use Notifiable, AuthenticationLoggable;
8}

The package will listen for Laravel's Login, Logout, Failed, and OtherDeviceLogout events.

Overriding default Laravel events

If you would like to listen to your own events you may override them in the package config (as of v1.2).

Example event override

You may notice that Laravel fires a Login event when the session renews if the user clicked 'remember me' when logging in. This will produce empty login rows each time which is not what we want. The way around this is to fire your own Login event instead of listening for Laravels.

You can create a Login event that takes the user:

1<?php
2 
3namespace App\Domains\Auth\Events;
4 
5use Illuminate\Queue\SerializesModels;
6 
7class Login
8{
9 use SerializesModels;
10 
11 public $user;
12 
13 public function __construct($user)
14 {
15 $this->user = $user;
16 }
17}

Then override it in the package config:

1// The events the package listens for to log
2'events' => [
3 'login' => \App\Domains\Auth\Events\Login::class,
4 ...
5],

Then call it where you login your user:

1event(new Login($user));

Now the package will only register actual login events, and not session re-authentications.

Overriding in Fortify

If you are working with Fortify and would like to register your own Login event, you can append a class to the authentication stack:

In FortifyServiceProvider:

1Fortify::authenticateThrough(function () {
2 return array_filter([
3 ...
4 FireLoginEvent::class,
5 ]);
6});

FireLoginEvent is just a class that fires the event:

1<?php
2 
3namespace App\Domains\Auth\Actions;
4 
5use App\Domains\Auth\Events\Login;
6 
7class FireLoginEvent
8{
9 public function handle($request, $next)
10 {
11 if ($request->user()) {
12 event(new Login($request->user()));
13 }
14 
15 return $next($request);
16 }
17}