Laravel Authentication Log Documentation

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

Displaying the Log

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 plugin then here is an example table:

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.

1<?php
2 
3namespace App\Http\Livewire;
4 
5use App\Models\User;
6use Illuminate\Database\Eloquent\Builder;
7use Jenssegers\Agent\Agent;
8use Rappasoft\LaravelLivewireTables\DataTableComponent;
9use Rappasoft\LaravelLivewireTables\Views\Column;
10use Rappasoft\LaravelAuthenticationLog\Models\AuthenticationLog as Log;
11 
12class AuthenticationLog extends DataTableComponent
13{
14 public string $defaultSortColumn = 'login_at';
15 public string $defaultSortDirection = 'desc';
16 public string $tableName = 'authentication-log-table';
17 
18 public User $user;
19 
20 public function mount(User $user)
21 {
22 if (! auth()->user() || ! auth()->user()->isAdmin()) {
23 $this->redirectRoute('frontend.index');
24 }
25 
26 $this->user = $user;
27 }
28 
29 public function columns(): array
30 {
31 return [
32 Column::make('IP Address', 'ip_address')
33 ->searchable(),
34 Column::make('Browser', 'user_agent')
35 ->searchable()
36 ->format(function($value) {
37 $agent = tap(new Agent, fn($agent) => $agent->setUserAgent($value));
38 return $agent->platform() . ' - ' . $agent->browser();
39 }),
40 Column::make('Location')
41 ->searchable(function (Builder $query, $searchTerm) {
42 $query->orWhere('location->city', 'like', '%'.$searchTerm.'%')
43 ->orWhere('location->state', 'like', '%'.$searchTerm.'%')
44 ->orWhere('location->state_name', 'like', '%'.$searchTerm.'%')
45 ->orWhere('location->postal_code', 'like', '%'.$searchTerm.'%');
46 })
47 ->format(fn ($value) => $value && $value['default'] === false ? $value['city'] . ', ' . $value['state'] : '-'),
48 Column::make('Login At')
49 ->sortable()
50 ->format(fn($value) => $value ? timezone()->convertToLocal($value) : '-'),
51 Column::make('Login Successful')
52 ->sortable()
53 ->format(fn($value) => $value === true ? 'Yes' : 'No'),
54 Column::make('Logout At')
55 ->sortable()
56 ->format(fn($value) => $value ? timezone()->convertToLocal($value) : '-'),
57 Column::make('Cleared By User')
58 ->sortable()
59 ->format(fn($value) => $value === true ? 'Yes' : 'No'),
60 ];
61 }
62 
63 public function query(): Builder
64 {
65 return Log::query()
66 ->where('authenticatable_type', User::class)
67 ->where('authenticatable_id', $this->user->id);
68 }
69}
1<livewire:authentication-log :user="$user" />

Example:

Example Log Table