You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[](https://packagist.org/packages/:vendor_slug/:package_slug)
[](https://packagist.org/packages/rappasoft/laravel-authentication-log)
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
25
-
26
-
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
8
+
Laravel Authentication Log is a package which tracks your user's authentication information such as login/logout time, IP, Browser, Location, etc. as well as sends out notifications via mail, slack, or sms for new devices and failed logins.
You must add the `AuthenticationLoggable` and `Notifiable` traits to the models you want to track.
87
+
88
+
```php
89
+
use Illuminate\Notifications\Notifiable;
90
+
use Rappasoft\LaravelAuthenticationLog\Traits\AuthenticationLoggable;
91
+
use Illuminate\Foundation\Auth\User as Authenticatable;
92
+
93
+
class User extends Authenticatable
94
+
{
95
+
use Notifiable, AuthenticationLoggable;
96
+
}
97
+
```
98
+
99
+
The package will listen for Laravel's Login, Logout, Failed, and OtherDeviceLogout events.
100
+
55
101
## Usage
56
102
103
+
Get all authentication logs for the user:
104
+
```php
105
+
User::find(1)->authentications;
106
+
```
107
+
108
+
Get the user's last login information:
109
+
```php
110
+
User::find(1)->lastLoginAt();
111
+
112
+
User::find(1)->lastSuccessfulLoginAt();
113
+
114
+
User::find(1)->lastLoginIp();
115
+
116
+
User::find(1)->lastSuccessfulLoginIp();
117
+
```
118
+
119
+
Get the user's previous login time & IP address (ignoring the current login):
120
+
```php
121
+
auth()->user()->previousLoginAt();
122
+
123
+
auth()->user()->previousLoginIp();
124
+
```
125
+
126
+
### Notifications
127
+
128
+
Notifications may be sent on the `mail`, `nexmo`, and `slack` channels but by **default notify via email**.
129
+
130
+
You may define a `notifyAuthenticationLogVia` method on your authenticatable models to determine which channels the notification should be delivered on:
131
+
132
+
```php
133
+
public function notifyAuthenticationLogVia()
134
+
{
135
+
return ['nexmo', 'mail', 'slack'];
136
+
}
137
+
```
138
+
139
+
You must install the [Slack](https://laravel.com/docs/8.x/notifications#routing-slack-notifications) and [Nexmo](https://laravel.com/docs/8.x/notifications#routing-sms-notifications) drivers to use those routes and follow their documentation on setting it up for your specific authenticatable models.
140
+
141
+
#### New Device Notifications
142
+
143
+
Enabled by default, they use the `\Rappasoft\LaravelAuthenticationLog\Notifications\NewDevice` class which can be overridden in the config file.
144
+
145
+
#### Failed Login Notifications
146
+
147
+
Disabled by default, they use the `\Rappasoft\LaravelAuthenticationLog\Notifications\FailedLogin` class which can be overridden in the config file.
148
+
149
+
#### Location
150
+
151
+
If the `torann/geoip` package is installed, it will attempt to include location information to the notifications by default.
152
+
153
+
You can turn this off within the configuration for each template.
154
+
155
+
**Note:** By default when working locally, no location will be recorded because it will send back the `default address` from the `geoip` config file. You can override this behavior in the email templates.
156
+
157
+
## Purging Old Logs
158
+
159
+
You may clear the old authentication log records using the `authentication-log:purge` Artisan command:
160
+
161
+
```
162
+
php artisan authentication-log:purge
163
+
```
164
+
165
+
Records that are older than the number of days specified in the `purge` option in your `config/authentication-log.php` will be deleted.
You can set up your own views and paginate the logs using the user relationship as normal, or if you also use my [Livewire Tables](https://github.com/rappasoft/laravel-livewire-tables) plugin then here is an example table:
180
+
181
+
**Note:** This example uses the `jenssegers/agent` package which is included by default with Laravel Jetstream as well as `jamesmills/laravel-timezone` for displaying timezones in the users local timezone. Both are optional, modify the table to fit your needs.
182
+
183
+
```php
184
+
<?php
185
+
186
+
namespace App\Http\Livewire;
187
+
188
+
use App\Models\User;
189
+
use Illuminate\Database\Eloquent\Builder;
190
+
use Jenssegers\Agent\Agent;
191
+
use Rappasoft\LaravelLivewireTables\DataTableComponent;
192
+
use Rappasoft\LaravelLivewireTables\Views\Column;
193
+
use Rappasoft\LaravelAuthenticationLog\Models\AuthenticationLog as Log;
194
+
195
+
class AuthenticationLog extends DataTableComponent
196
+
{
197
+
public string $defaultSortColumn = 'login_at';
198
+
public string $defaultSortDirection = 'desc';
199
+
public string $tableName = 'authentication-log-table';
200
+
201
+
public User $user;
202
+
203
+
public function mount(User $user)
204
+
{
205
+
if (! auth()->user() || ! auth()->user()->isAdmin()) {
0 commit comments