Skip to content

Commit f200554

Browse files
committed
Update readme
1 parent aa1e5ab commit f200554

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,88 @@ You can also schedule the command at an interval:
174174
$schedule->command('authentication-log:purge')->monthly();
175175
```
176176

177+
### Displaying The Log
178+
179+
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()) {
206+
$this->redirectRoute('frontend.index');
207+
}
208+
209+
$this->user = $user;
210+
}
211+
212+
public function columns(): array
213+
{
214+
return [
215+
Column::make('IP Address', 'ip_address')
216+
->searchable(),
217+
Column::make('Browser', 'user_agent')
218+
->searchable()
219+
->format(function($value) {
220+
$agent = tap(new Agent, fn($agent) => $agent->setUserAgent($value));
221+
return $agent->platform() . ' - ' . $agent->browser();
222+
}),
223+
Column::make('Location')
224+
->searchable(function (Builder $query, $searchTerm) {
225+
$query->orWhere('location->city', 'like', '%'.$searchTerm.'%')
226+
->orWhere('location->state', 'like', '%'.$searchTerm.'%')
227+
->orWhere('location->state_name', 'like', '%'.$searchTerm.'%')
228+
->orWhere('location->postal_code', 'like', '%'.$searchTerm.'%');
229+
})
230+
->format(fn($value) => $value['default'] === false ? $value['city'] . ', ' . $value['state'] : '-'),
231+
Column::make('Login At')
232+
->sortable()
233+
->format(fn($value) => $value ? timezone()->convertToLocal($value) : '-'),
234+
Column::make('Login Successful')
235+
->sortable()
236+
->format(fn($value) => $value === true ? 'Yes' : 'No'),
237+
Column::make('Logout At')
238+
->sortable()
239+
->format(fn($value) => $value ? timezone()->convertToLocal($value) : '-'),
240+
Column::make('Cleared By User')
241+
->sortable()
242+
->format(fn($value) => $value === true ? 'Yes' : 'No'),
243+
];
244+
}
245+
246+
public function query(): Builder
247+
{
248+
return Log::query()
249+
->where('authenticatable_type', User::class)
250+
->where('authenticatable_id', $this->user->id);
251+
}
252+
}
253+
```
254+
255+
```html
256+
<livewire:authentication-log :user="$user" />
257+
```
258+
177259
## Testing
178260

179261
```bash

0 commit comments

Comments
 (0)