PHP code example of iresis / login-audit

1. Go to this page and download the library: Download iresis/login-audit library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

iresis / login-audit example snippets


return [
    'enabled' => true,

    'events' => [
        'login' => true,
        'logout' => true,
        'failed' => true,
        'other_device_logout' => true,
    ],

    'models' => [
        'log' => \Iresis\LoginAudit\Models\LoginAuditLog::class,
        'session' => \Iresis\LoginAudit\Models\LoginAuditSession::class,
    ],

    'retention' => [
        'logs_days' => 90,
        'sessions_days' => 90,
    ],
];

class TenantLoginAuditLog extends \Iresis\LoginAudit\Models\LoginAuditLog
{
    protected static function booted(): void
    {
        static::creating(fn ($log) => $log->tenant_id ??= currentTenantId());
    }
}

// config/login-audit.php
'models' => [
    'log' => \App\Models\TenantLoginAuditLog::class,
    'session' => \App\Models\TenantLoginAuditSession::class,
],

use Iresis\LoginAudit\Concerns\HasLoginAudit;

class User extends Authenticatable
{
    use HasLoginAudit;
}

$user->loginAudits;          // all recorded login/logout/failed events for this user
$user->auditSessions;        // all recorded sessions
$user->activeAuditSessions;  // sessions that haven't been logged out
$user->auditDevices();       // sessions grouped by device (browser/platform/device type)

use Iresis\LoginAudit\Facades\LoginAudit;

LoginAudit::sessionsFor($user);
LoginAudit::activeSessionsFor($user);
LoginAudit::devicesFor($user); // Collection<DeviceSummary>

LoginAudit::revokeSession($session);
LoginAudit::revokeOtherSessions($user, exceptSessionId: $currentSessionId);

Route::middleware(['auth', 'login-audit.activity'])->group(function () {
    // ...
});

Schedule::command('login-audit:prune')->daily();
bash
php artisan vendor:publish --tag="login-audit"
bash
php artisan vendor:publish --tag="login-audit-config"
bash
php artisan vendor:publish --tag="login-audit-migrations"
php artisan migrate
bash
# List active sessions (optionally filtered)
php artisan login-audit:sessions
php artisan login-audit:sessions --user=1 --guard=web

# Revoke a specific session
php artisan login-audit:sessions --revoke=<session_id>

# Prune logs/sessions past their configured retention
php artisan login-audit:prune