|
| 1 | +# Laravel Auth-log |
| 2 | +User authentication log for Laravel 5 and 6. |
| 3 | +This package allows you to log user's authentication and force its logout if necessary! |
| 4 | + |
| 5 | +## System requirements |
| 6 | + |
| 7 | +Set `session.driver` value |
| 8 | +> To use this package the only allowed values of `session.driver` are `file`, `database`, `redis` (at the moment). |
| 9 | +
|
| 10 | +## Install` |
| 11 | + |
| 12 | +You can install the package via composer: |
| 13 | +```sh |
| 14 | +$ composer require biscolab/laravel-authlog |
| 15 | +``` |
| 16 | +Laravel 5.5 (or greater) uses package auto-discovery, so doesn't require you to manually add the Service Provider, but if you don't use auto-discovery `AuthLogServiceProvider` must be registered in `config/app.php`: |
| 17 | +```php |
| 18 | +'providers' => [ |
| 19 | + ... |
| 20 | + Biscolab\LaravelAuthLog\AuthLogServiceProvider::class, |
| 21 | +]; |
| 22 | +``` |
| 23 | +You can use the facade for shorter code. Add `AuthLog` to your aliases: |
| 24 | +```php |
| 25 | +'aliases' => [ |
| 26 | + ... |
| 27 | + 'AuthLog' => Biscolab\LaravelAuthLog\Facades\AuthLog::class, |
| 28 | +]; |
| 29 | +``` |
| 30 | + |
| 31 | +## Publish package |
| 32 | +Create `config/authlog.php` configuration file using the following artisan command: |
| 33 | +```sh |
| 34 | +$ php artisan vendor:publish --provider="Biscolab\LaravelAuthLog\AuthLogServiceProvider" |
| 35 | +``` |
| 36 | + |
| 37 | +## Configuration |
| 38 | + |
| 39 | +Edit `config/authlog.php` |
| 40 | + |
| 41 | +| Variable name | Type | Description | Default value | |
| 42 | +|----------------|-----------------|---------------------|-----------------| |
| 43 | +| `safe_mode` | `bool` | Avoid to force logout by yourself | `true` | |
| 44 | +| `enabled` | `bool` | If `true` the package is active and user's authentication will be logged | `true` | |
| 45 | +| `skip_ip` | `string` | A whitelist of IP addresses (CSV format) that, if recognized, disable the package | `''` | |
| 46 | +| `table_name` | `string` | The name of the AuthLog database table | `authlog` | |
| 47 | +| `authlog_model` | `string` | AuthLog class. You can change ìt **BUT** your custom class **MUST** implements `'Biscolab\LaravelAuthLog\Models\AuthLogInterface'` | `'Biscolab\LaravelAuthLog\Models\AuthLog'` | |
| 48 | +| `session_model` | `string` | Session class. You can change ìt **BUT** your custom class **MUST** implements `'Biscolab\LaravelAuthLog\Models\SessionInterface'` | `'Biscolab\LaravelAuthLog\Models\Session'` | |
| 49 | +| `session_auth_log_id_key` | `string` | Session key used to store your AuthLog ID | `'auth_log_id'` | |
| 50 | +| `add_auth_log_id_to_ajax_response` | `bool` | If `true` AuthLog ID will be added to your AJAX responses | `true` | |
| 51 | +| `ajax_response_auth_log_id_name` | `string` | AJAX response key used to send your AuthLog ID | `'auth_log_id'` | |
| 52 | +| `add_auth_log_id_header_to_http_response` | `bool` | If `true` AuthLog ID will be added to your response headers | `true` | |
| 53 | +| `auth_log_id_header_nameauth_log_id_header_name` | `string` | AuthLog ID header name | `'X-Auth-Log-Id'` | |
| 54 | + |
| 55 | +> Remember to run the `php artisan config:cache` command |
| 56 | +
|
| 57 | +## Database |
| 58 | + |
| 59 | +Run migrations |
| 60 | + |
| 61 | +```sh |
| 62 | +php artisan migrate |
| 63 | +``` |
| 64 | + |
| 65 | +AuthLog database table will be created. |
| 66 | + |
| 67 | + |
| 68 | +## Middleware |
| 69 | + |
| 70 | +### Register `AuthLogMiddleware |
| 71 | +Register `AuthLogMiddleware` in `app/Http/Kernel.php`. This middleware will handle user authentication session ID. |
| 72 | + |
| 73 | +```php |
| 74 | + |
| 75 | +protected $routeMiddleware = [ |
| 76 | + ... |
| 77 | + 'auth.log' => \Biscolab\LaravelAuthLog\Middleware\AuthLogMiddleware::class |
| 78 | +]; |
| 79 | + |
| 80 | +```` |
| 81 | + |
| 82 | +### Add `AuthLogMiddleware` to routes |
| 83 | + |
| 84 | +```php |
| 85 | +Route::group(['middleware' => ['auth.log']], function() { |
| 86 | + |
| 87 | + // Your routes |
| 88 | +}); |
| 89 | +``` |
| 90 | + |
| 91 | +## Handle logged users |
| 92 | +### Artisan Command |
| 93 | + |
| 94 | +To handle auth sessions type the following artisan command |
| 95 | + |
| 96 | +```sh |
| 97 | +php artisan authlog:logged |
| 98 | +``` |
| 99 | + |
| 100 | +The list of logged users will be shown |
| 101 | + |
| 102 | +```sh |
| 103 | ++--------+------------------------------------------+-----------------------+---------------------+ |
| 104 | +| Log ID | Session ID | User | Logged @ | |
| 105 | ++--------+------------------------------------------+-----------------------+---------------------+ |
| 106 | +| 604 | teq4LmVM4u4sdhFTKnGsKeWs3IBOLAIOXB1c4ioy | Roberto Belotti (#22) | 2019-09-25 22:56:33 | |
| 107 | ++--------+------------------------------------------+-----------------------+---------------------+ |
| 108 | + |
| 109 | +Type Log ID to kill session. Type "exit" to quit: |
| 110 | +``` |
| 111 | +
|
| 112 | +Now you can either quit typing `exit` or force user logout typing the specific Log ID, in this case `604`. |
| 113 | +
|
| 114 | +``` |
| 115 | + > 604 |
| 116 | + |
| 117 | +Session "teq4LmVM4u4sdhFTKnGsKeWs3IBOLAIOXB1c4ioy" deleted. "Roberto Belotti" user logged out |
| 118 | +No logged user, please type "exit" to quit |
| 119 | +``` |
0 commit comments