|
3 | 3 | [](https://packagist.org/packages/rappasoft/laravel-authentication-log) |
4 | 4 | [](https://packagist.org/packages/rappasoft/laravel-authentication-log) |
5 | 5 |
|
6 | | -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. |
| 6 | +Laravel Authentication Log is a comprehensive package which tracks your user's authentication information such as login/logout time, IP, Browser, Location, Device Fingerprint, etc. It sends out notifications via mail, slack, or SMS for new devices and failed logins, detects suspicious activity, provides session management, and much more. |
| 7 | + |
| 8 | +## Features |
| 9 | + |
| 10 | +### Core Features |
| 11 | +- ✅ **Authentication Logging** - Tracks all login/logout attempts with IP, user agent, location, and timestamps |
| 12 | +- ✅ **Device Fingerprinting** - Reliable device identification using SHA-256 hashing |
| 13 | +- ✅ **New Device Detection** - Automatically detects and notifies users of new device logins |
| 14 | +- ✅ **Failed Login Tracking** - Logs and optionally notifies users of failed login attempts |
| 15 | +- ✅ **Location Tracking** - Optional GeoIP integration for location data |
| 16 | + |
| 17 | +### Advanced Features |
| 18 | +- 🔒 **Suspicious Activity Detection** - Automatically detects multiple failed logins, rapid location changes, and unusual login times |
| 19 | +- 📊 **Statistics & Insights** - Get comprehensive login statistics including total logins, failed attempts, unique devices, and more |
| 20 | +- 🔐 **Session Management** - View active sessions, revoke specific sessions, or logout all other devices |
| 21 | +- 🛡️ **Device Trust Management** - Mark devices as trusted, manage device names, and require trusted devices for sensitive actions |
| 22 | +- ⚡ **Rate Limiting** - Prevents notification spam with configurable rate limits |
| 23 | +- 🔔 **Webhook Support** - Send webhooks to external services for authentication events |
| 24 | +- 📤 **Export Functionality** - Export authentication logs to CSV or JSON format |
| 25 | +- 🎯 **Query Scopes** - Powerful query scopes for filtering logs (successful, failed, suspicious, recent, by IP, by device, etc.) |
| 26 | +- 🚦 **Middleware** - Protect routes with trusted device middleware |
7 | 27 |
|
8 | 28 | ## Documentation, Installation, and Usage Instructions |
9 | 29 |
|
10 | 30 | See the [documentation](https://rappasoft.com/docs/laravel-authentication-log) for detailed installation and usage instructions. |
11 | 31 |
|
12 | 32 | ## Version Compatibility |
13 | 33 |
|
14 | | - Laravel | Authentication Log |
15 | | -:---------|:------------------ |
16 | | - 8.x | 1.x |
17 | | - 9.x | 2.x |
18 | | - 10.x | 3.x |
19 | | - 11.x | 4.x |
20 | | - 12.x | 5.x |
| 34 | + Laravel | Authentication Log | Features |
| 35 | +:---------|:------------------|:-------- |
| 36 | + 8.x | 1.x | Basic logging only |
| 37 | + 9.x | 2.x | Basic logging only |
| 38 | + 10.x | 3.x | Basic logging only |
| 39 | + 11.x | 4.x | All features (device fingerprinting, suspicious activity, webhooks, etc.) |
| 40 | + 12.x | 4.x | All features (device fingerprinting, suspicious activity, webhooks, etc.) |
| 41 | + |
| 42 | +**Note:** Version 4.x requires Laravel 11.x or 12.x. For Laravel 10.x support, please use version 3.x. |
21 | 43 |
|
22 | 44 | ## Installation |
23 | 45 |
|
24 | 46 | ```bash |
25 | 47 | composer require rappasoft/laravel-authentication-log |
26 | 48 | ``` |
27 | 49 |
|
| 50 | +## Quick Start |
| 51 | + |
| 52 | +### 1. Add the Trait to Your User Model |
| 53 | + |
| 54 | +```php |
| 55 | +use Rappasoft\LaravelAuthenticationLog\Traits\AuthenticationLoggable; |
| 56 | + |
| 57 | +class User extends Authenticatable |
| 58 | +{ |
| 59 | + use AuthenticationLoggable; |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +### 2. Publish and Run Migrations |
| 64 | + |
| 65 | +**For new installations:** |
| 66 | +```bash |
| 67 | +php artisan vendor:publish --provider="Rappasoft\LaravelAuthenticationLog\LaravelAuthenticationLogServiceProvider" --tag="authentication-log-migrations" |
| 68 | +php artisan migrate |
| 69 | +``` |
| 70 | + |
| 71 | +**For existing installations (upgrading from v3.x or earlier):** |
| 72 | +```bash |
| 73 | +# Update the package |
| 74 | +composer update rappasoft/laravel-authentication-log |
| 75 | + |
| 76 | +# Publish the upgrade migration |
| 77 | +php artisan vendor:publish --provider="Rappasoft\LaravelAuthenticationLog\LaravelAuthenticationLogServiceProvider" --tag="authentication-log-migrations" |
| 78 | + |
| 79 | +# Run the migrations (the upgrade migration will only add columns if they don't exist) |
| 80 | +php artisan migrate |
| 81 | +``` |
| 82 | + |
| 83 | +The upgrade migration will safely add the new columns (`device_id`, `device_name`, `is_trusted`, `last_activity_at`, `is_suspicious`, `suspicious_reason`) to your existing `authentication_log` table without affecting existing data. |
| 84 | + |
| 85 | +### 3. Configure (Optional) |
| 86 | + |
| 87 | +```bash |
| 88 | +php artisan vendor:publish --provider="Rappasoft\LaravelAuthenticationLog\LaravelAuthenticationLogServiceProvider" --tag="authentication-log-config" |
| 89 | +``` |
| 90 | + |
| 91 | +## Usage Examples |
| 92 | + |
| 93 | +### Get User Statistics |
| 94 | + |
| 95 | +```php |
| 96 | +$user = User::find(1); |
| 97 | + |
| 98 | +// Get comprehensive statistics |
| 99 | +$stats = $user->getLoginStats(); |
| 100 | +// Returns: total_logins, failed_attempts, unique_devices, unique_ips, last_30_days, etc. |
| 101 | + |
| 102 | +// Or get individual stats |
| 103 | +$totalLogins = $user->getTotalLogins(); |
| 104 | +$failedAttempts = $user->getFailedAttempts(); |
| 105 | +$uniqueDevices = $user->getUniqueDevicesCount(); |
| 106 | +``` |
| 107 | + |
| 108 | +### Session Management |
| 109 | + |
| 110 | +```php |
| 111 | +// Get all active sessions |
| 112 | +$activeSessions = $user->getActiveSessions(); |
| 113 | +$sessionCount = $user->getActiveSessionsCount(); |
| 114 | + |
| 115 | +// Revoke a specific session |
| 116 | +$user->revokeSession($sessionId); |
| 117 | + |
| 118 | +// Revoke all other sessions (keep current device) |
| 119 | +$user->revokeAllOtherSessions($currentDeviceId); |
| 120 | + |
| 121 | +// Revoke all sessions |
| 122 | +$user->revokeAllSessions(); |
| 123 | +``` |
| 124 | + |
| 125 | +### Device Management |
| 126 | + |
| 127 | +```php |
| 128 | +// Get all user devices |
| 129 | +$devices = $user->getDevices(); |
| 130 | + |
| 131 | +// Trust a device |
| 132 | +$user->trustDevice($deviceId); |
| 133 | + |
| 134 | +// Untrust a device |
| 135 | +$user->untrustDevice($deviceId); |
| 136 | + |
| 137 | +// Update device name |
| 138 | +$user->updateDeviceName($deviceId, 'My iPhone'); |
| 139 | + |
| 140 | +// Check if device is trusted |
| 141 | +if ($user->isDeviceTrusted($deviceId)) { |
| 142 | + // Device is trusted |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +### Query Scopes |
| 147 | + |
| 148 | +```php |
| 149 | +use Rappasoft\LaravelAuthenticationLog\Models\AuthenticationLog; |
| 150 | + |
| 151 | +// Filter successful logins |
| 152 | +$successfulLogins = AuthenticationLog::successful()->get(); |
| 153 | + |
| 154 | +// Filter failed logins |
| 155 | +$failedLogins = AuthenticationLog::failed()->get(); |
| 156 | + |
| 157 | +// Filter by IP address |
| 158 | +$ipLogs = AuthenticationLog::fromIp('192.168.1.1')->get(); |
| 159 | + |
| 160 | +// Filter recent logs (last 7 days) |
| 161 | +$recentLogs = AuthenticationLog::recent(7)->get(); |
| 162 | + |
| 163 | +// Filter suspicious activities |
| 164 | +$suspicious = AuthenticationLog::suspicious()->get(); |
| 165 | + |
| 166 | +// Filter active sessions |
| 167 | +$activeSessions = AuthenticationLog::active()->get(); |
| 168 | + |
| 169 | +// Filter trusted devices |
| 170 | +$trustedDevices = AuthenticationLog::trusted()->get(); |
| 171 | + |
| 172 | +// Filter by device ID |
| 173 | +$deviceLogs = AuthenticationLog::fromDevice($deviceId)->get(); |
| 174 | + |
| 175 | +// Filter for specific user |
| 176 | +$userLogs = AuthenticationLog::forUser($user)->get(); |
| 177 | +``` |
| 178 | + |
| 179 | +### Suspicious Activity Detection |
| 180 | + |
| 181 | +```php |
| 182 | +// Detect suspicious activity |
| 183 | +$suspiciousActivities = $user->detectSuspiciousActivity(); |
| 184 | + |
| 185 | +// Returns array of suspicious activities: |
| 186 | +// [ |
| 187 | +// [ |
| 188 | +// 'type' => 'multiple_failed_logins', |
| 189 | +// 'count' => 5, |
| 190 | +// 'message' => '5 failed login attempts in the last hour' |
| 191 | +// ], |
| 192 | +// [ |
| 193 | +// 'type' => 'rapid_location_change', |
| 194 | +// 'countries' => ['US', 'UK'], |
| 195 | +// 'message' => 'Login from multiple countries within an hour' |
| 196 | +// ] |
| 197 | +// ] |
| 198 | +``` |
| 199 | + |
| 200 | +### Middleware for Trusted Devices |
| 201 | + |
| 202 | +```php |
| 203 | +// In your routes file |
| 204 | +Route::middleware(['auth', 'device.trusted'])->group(function () { |
| 205 | + // These routes require a trusted device |
| 206 | + Route::get('/sensitive-action', [Controller::class, 'sensitiveAction']); |
| 207 | +}); |
| 208 | +``` |
| 209 | + |
| 210 | +### Export Logs |
| 211 | + |
| 212 | +```bash |
| 213 | +# Export all logs to CSV |
| 214 | +php artisan authentication-log:export --format=csv |
| 215 | + |
| 216 | +# Export logs from last 30 days |
| 217 | +php artisan authentication-log:export --format=csv --days=30 |
| 218 | + |
| 219 | +# Export logs for specific user |
| 220 | +php artisan authentication-log:export --format=json --user=1 |
| 221 | + |
| 222 | +# Specify output file |
| 223 | +php artisan authentication-log:export --format=csv --output=logs.csv |
| 224 | +``` |
| 225 | + |
| 226 | +### Webhook Configuration |
| 227 | + |
| 228 | +Add webhooks to your `config/authentication-log.php`: |
| 229 | + |
| 230 | +```php |
| 231 | +'webhooks' => [ |
| 232 | + [ |
| 233 | + 'url' => 'https://example.com/webhook', |
| 234 | + 'events' => ['login', 'failed', 'new_device', 'suspicious'], |
| 235 | + 'headers' => [ |
| 236 | + 'Authorization' => 'Bearer your-token', |
| 237 | + ], |
| 238 | + ], |
| 239 | +], |
| 240 | +``` |
| 241 | + |
| 242 | +## Configuration |
| 243 | + |
| 244 | +The package includes comprehensive configuration options: |
| 245 | + |
| 246 | +- **Notifications** - Configure new device and failed login notifications with rate limiting |
| 247 | +- **Suspicious Activity** - Configure thresholds and detection rules |
| 248 | +- **Webhooks** - Set up webhook endpoints for external integrations |
| 249 | +- **Database** - Customize table name and database connection |
| 250 | + |
| 251 | +See the [configuration documentation](https://rappasoft.com/docs/laravel-authentication-log/start/configuration) for all available options. |
| 252 | + |
28 | 253 | ## Testing |
29 | 254 |
|
30 | 255 | ```bash |
|
0 commit comments