Skip to content

Commit 015d2f9

Browse files
authored
Merge pull request #1 from rappasoft/develop
v1.0.0
2 parents a20ec1e + b7b4171 commit 015d2f9

34 files changed

+828
-321
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
github: :vendor_name
1+
github: rappasoft

.github/ISSUE_TEMPLATE/config.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
blank_issues_enabled: false
22
contact_links:
33
- name: Ask a question
4-
url: https://github.com/:vendor_slug/:package_slug/discussions/new?category=q-a
4+
url: https://github.com/rappasoft/laravel-authentication-log/discussions/new?category=q-a
55
about: Ask the community for help
66
- name: Request a feature
7-
url: https://github.com/:vendor_slug/:package_slug/discussions/new?category=ideas
7+
url: https://github.com/rappasoft/laravel-authentication-log/discussions/new?category=ideas
88
about: Share ideas for new features
99
- name: Report a bug
10-
url: https://github.com/:vendor_slug/:package_slug/issues/new
10+
url: https://github.com/rappasoft/laravel-authentication-log/issues/new
1111
about: Report a reproducable bug

.github/SECURITY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Security Policy
22

3-
If you discover any security related issues, please email author@domain.com instead of using the issue tracker.
3+
If you discover any security related issues, please email rappa819@gmail.com instead of using the issue tracker.

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Changelog
22

3-
All notable changes to `:package_name` will be documented in this file.
3+
All notable changes to `Laravel Authentication Log` will be documented in this file.
44

5-
## 1.0.0 - 202X-XX-XX
5+
## 1.0.0 - 2021-09-30
66

7-
- initial release
7+
- Initial Release

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) :vendor_name <author@domain.com>
3+
Copyright (c) Anthony Rappa <rappa819@gmail.com>
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 232 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,265 @@
1-
# :package_description
1+
![Package Logo](https://banners.beyondco.de/Laravel%20Authentication%20Log.png?theme=dark&packageManager=composer+require&packageName=rappasoft%2Flaravel-authentication-log&pattern=hideout&style=style_1&description=Log+user+authentication+details+and+send+new+device+notifications.&md=1&showWatermark=0&fontSize=100px&images=lock-closed)
22

3-
[![Latest Version on Packagist](https://img.shields.io/packagist/v/:vendor_slug/:package_slug.svg?style=flat-square)](https://packagist.org/packages/:vendor_slug/:package_slug)
4-
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/:vendor_slug/:package_slug/run-tests?label=tests)](https://github.com/:vendor_slug/:package_slug/actions?query=workflow%3Arun-tests+branch%3Amain)
5-
[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/:vendor_slug/:package_slug/Check%20&%20fix%20styling?label=code%20style)](https://github.com/:vendor_slug/:package_slug/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
6-
[![Total Downloads](https://img.shields.io/packagist/dt/:vendor_slug/:package_slug.svg?style=flat-square)](https://packagist.org/packages/:vendor_slug/:package_slug)
3+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/rappasoft/laravel-authentication-log.svg?style=flat-square)](https://packagist.org/packages/rappasoft/laravel-authentication-log)
4+
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/rappasoft/laravel-authentication-log/run-tests?label=tests)](https://github.com/rappasoft/laravel-authentication-log/actions?query=workflow%3Arun-tests+branch%3Amain)
5+
[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/rappasoft/laravel-authentication-log/Check%20&%20fix%20styling?label=code%20style)](https://github.com/rappasoft/laravel-authentication-log/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
6+
[![Total Downloads](https://img.shields.io/packagist/dt/rappasoft/laravel-authentication-log.svg?style=flat-square)](https://packagist.org/packages/rappasoft/laravel-authentication-log)
77

8-
---
9-
This repo can be used to scaffold a Laravel package. Follow these steps to get started:
10-
11-
1. Press the "Use template" button at the top of this repo to create a new repo with the contents of this skeleton
12-
2. Run "php ./configure.php" to run a script that will replace all placeholders throughout all the files
13-
3. Remove this block of text.
14-
4. Have fun creating your package.
15-
5. If you need help creating a package, consider picking up our <a href="https://laravelpackage.training">Laravel Package Training</a> video course.
16-
---
17-
18-
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
19-
20-
## Support us
21-
22-
[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/:package_name.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/:package_name)
23-
24-
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.
279

2810
## Installation
2911

3012
You can install the package via composer:
3113

3214
```bash
33-
composer require :vendor_slug/:package_slug
15+
composer require rappasoft/laravel-authentication-log
16+
```
17+
18+
If you want the location features you must also install `torann/geoip`:
19+
20+
```bash
21+
composer require torann/geoip
3422
```
3523

3624
You can publish and run the migrations with:
3725

3826
```bash
39-
php artisan vendor:publish --provider="VendorName\Skeleton\SkeletonServiceProvider" --tag=":package_slug-migrations"
27+
php artisan vendor:publish --provider="Rappasoft\LaravelAuthenticationLog\LaravelAuthenticationLogServiceProvider" --tag="authentication-log-migrations"
4028
php artisan migrate
4129
```
4230

31+
You can publish the view/email files with:
32+
```bash
33+
php artisan vendor:publish --provider="Rappasoft\LaravelAuthenticationLog\LaravelAuthenticationLogServiceProvider" --tag="authentication-log-views"
34+
```
35+
4336
You can publish the config file with:
4437
```bash
45-
php artisan vendor:publish --provider="VendorName\Skeleton\SkeletonServiceProvider" --tag=":package_slug-config"
38+
php artisan vendor:publish --provider="Rappasoft\LaravelAuthenticationLog\LaravelAuthenticationLogServiceProvider" --tag="authentication-log-config"
4639
```
4740

4841
This is the contents of the published config file:
4942

5043
```php
5144
return [
45+
// The database table name
46+
// You can change this if the database keys get too long for your driver
47+
'table_name' => 'authentication_log',
48+
49+
'notifications' => [
50+
'new-device' => [
51+
// Send the NewDevice notification
52+
'enabled' => env('NEW_DEVICE_NOTIFICATION', true),
53+
54+
// Use torann/geoip to attempt to get a location
55+
'location' => true,
56+
57+
// The Notification class to send
58+
'template' => \Rappasoft\LaravelAuthenticationLog\Notifications\NewDevice::class,
59+
],
60+
'failed-login' => [
61+
// Send the FailedLogin notification
62+
'enabled' => env('FAILED_LOGIN_NOTIFICATION', false),
63+
64+
// Use torann/geoip to attempt to get a location
65+
'location' => true,
66+
67+
// The Notification class to send
68+
'template' => \Rappasoft\LaravelAuthenticationLog\Notifications\FailedLogin::class,
69+
],
70+
],
71+
72+
// When the clean-up command is run, delete old logs greater than `purge` days
73+
// Don't schedule the clean-up command if you want to keep logs forever.
74+
'purge' => 365,
5275
];
5376
```
5477

78+
If you installed `torann/geoip` you should also publish that config file to set your defaults:
79+
80+
```
81+
php artisan vendor:publish --provider="Torann\GeoIP\GeoIPServiceProvider" --tag=config
82+
```
83+
84+
## Configuration
85+
86+
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+
55101
## Usage
56102

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.
166+
57167
```php
58-
$skeleton = new VendorName\Skeleton();
59-
echo $skeleton->echoPhrase('Hello, VendorName!');
168+
'purge' => 365,
60169
```
61170

171+
You can also schedule the command at an interval:
172+
173+
```php
174+
$schedule->command('authentication-log:purge')->monthly();
175+
```
176+
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+
259+
Example:
260+
261+
![Example Log Table](https://imgur.com/B4DlN4W.png)
262+
62263
## Testing
63264

64265
```bash
@@ -79,7 +280,8 @@ Please review [our security policy](../../security/policy) on how to report secu
79280

80281
## Credits
81282

82-
- [:author_name](https://github.com/:author_username)
283+
- [Anthony Rappa](https://github.com/rappasoft)
284+
- [yadahan/laravel-authentication-log](https://github.com/yadahan/laravel-authentication-log)
83285
- [All Contributors](../../contributors)
84286

85287
## License

0 commit comments

Comments
 (0)