Monday morning, Mrs. Dewi gets a report: Siti's account was used to log in from an unknown IP. When? From where? Did it work, or was it only an attempt? All of that can only be answered if there's a record.
That's what an authentication audit log is for, a permanent record of every important event in the login system: who did what, when, from where, and whether it worked. The record may only be added to, never changed or deleted. Picture a bank statement. Every transaction becomes a new line. When a transfer goes wrong, the bank doesn't erase the line, it adds a correcting line. Precisely because nothing can be erased, the statement can be trusted as evidence.
Which events get recorded
An auth audit log records every event that changes who can get in, or proves who got in:
action |
When it is written | Example at School Portal |
|---|---|---|
login |
every login attempt, successful or failed | Siti types a wrong password, then gets in |
logout |
the user signs out | Mr. Andi signs out of the school laptop |
mfa |
a 2FA code is checked, successful or failed | Mr. Andi's TOTP code is wrong once |
password_change |
the password is changed or reset | Siti changes her password |
role_change |
someone's role or permissions change | Mrs. Dewi changes Rizky's role to alumni |
token_revoke |
a session or token is revoked | Siti's two other sessions are revoked |
An ordinary page view is not an auth event; it belongs in the web server's access log. Watching the log for alerts, such as many failed logins from one IP, is covered in Security Logging & Monitoring in the Security track. This lesson builds the record itself.
Five questions on every line
Every line answers the same five questions:
- Who: the actor's
userId. In this example"user1"is Siti,"user2"Mr. Andi,"user3"Mrs. Dewi. Forrole_change, the person being changed goes inmetadata.target, because the actor is Mrs. Dewi. - What: the
action(the kind of event) and theresource(the endpoint or data touched). - When: the
timestampin UTC, ISO 8601 format, fromnew Date().toISOString().2026-10-09T00:00:20.000Zis Friday at 07:00:20 school time (UTC+7). UTC is used so logs from servers in any time zone sort together. - From where: the IP address and the user agent (the browser and its operating system), in
metadata. - The result:
success,trueorfalse(a boolean, not text), plus why it failed inmetadata.reason.
function createAuditLog(userId, action, resource, success, metadata) {
return {
userId: userId,
action: action,
resource: resource,
success: success,
metadata: metadata,
timestamp: new Date().toISOString(),
};
}
const entry = createAuditLog("user1", "login", "/auth/login", false, {
ip: "36.68.10.21",
userAgent: "Chrome 141 on Windows 11",
reason: "wrong_password",
});
console.log(entry.userId, entry.success, typeof entry.timestamp); // user1 false string
- Every argument is stored as it is.
successstays a boolean, so it can be filtered with===. - The
timestampis made on the server when the event happens, not sent by the client.toISOString()returns a string.
What must never go in the log
Logs are read by many people and many tools. Whatever is written there is open to all of them, and in an append-only log it can never be removed. So never record:
- Passwords, including wrong ones. A wrong password is often one letter away from the real one.
- Tokens, session IDs, OTP codes, recovery codes: anyone who reads them can use them.
To point at a revoked session, record its row id ("sess_1_1"), not the token. Pick the fields one by one; never store a whole request, because a login body carries the password.
Append-only: add, never change
An audit log is useful as evidence only if it can't be edited. If an admin, or someone using an admin's account, can delete lines, the trail of what happened can vanish with them. Make that impossible at the database, not only in the application code. In PostgreSQL, the application's database user gets permission to add and read, nothing more:
-- the table is created by portal_owner (a role only for migrations), not by portal_app
GRANT SELECT, INSERT ON audit_logs TO portal_app;
This permission means something only when portal_app is not the owner of the table. The table's owner, the role that ran the migration, always has every permission, UPDATE and DELETE included. So run migrations with another role, such as portal_owner, and let the application connect as portal_app.
When application code tries to change a line, the database refuses:
portal=> UPDATE audit_logs SET success = true WHERE id = 1;
ERROR: permission denied for table audit_logs
In Laravel, put a fence on the model too:
class AuditLog extends Model
{
public const UPDATED_AT = null; // a line is never updated
protected static function booted(): void
{
static::updating(fn () => false); // cancel every update
static::deleting(fn () => false); // cancel every delete
}
}
Then write the lines from Laravel's built-in auth events, such as Illuminate\Auth\Events\Failed for a failed login and Login for a successful one. For an extra layer, ship a copy of the log to a separate service your application server cannot delete from.
Query: every condition must match
Following a trail means filtering the log by several conditions at once. A condition that wasn't sent must not narrow the result:
function queryAuditLogs(logs, filters) {
return logs.filter((log) => {
if (filters.userId !== undefined && log.userId !== filters.userId) return false;
if (filters.action !== undefined && log.action !== filters.action) return false;
if (filters.success !== undefined && log.success !== filters.success) return false;
return true; // passed every condition that was sent
});
}
With this morning's 10 log lines at School Portal, exactly the ones in the Lab:
| Query | Result |
|---|---|
{ success: false } |
3: Siti's login 07:00:05, Mr. Andi's 2FA code 07:05:15, Siti's login 07:31:00 |
{ userId: "user1" } |
6: everything Siti did |
{ userId: "user1", success: false } |
2: Siti's two failed logins |
Now guess: if the filter is written if (filters.success && log.success !== filters.success) return false, then called with { success: false }, how many lines show up?
That filter writes filters.success && .... When filters.success is false, that expression is false at once, the condition is skipped, and all 10 lines show up. Compare !== undefined: a false value still counts as a condition. In the exercise, with its four sample logs, the same three queries give 2, 3 and 1.
⚠️ Why the log fails you when you need it
- Recording only failures. A successful login from a strange IP is exactly the sign that someone else is using the account. Record both.
- Storing passwords, tokens or OTP codes. Pick fields one by one, and record a session's id, not its token.
- A log table that allows UPDATE and DELETE. Revoke those permissions in the database; don't rely on code alone.
- Local time with no zone.
09/10/2026 07:00says nothing about its zone. Store ISO 8601 UTC. if (filters.success && ...). Afalsevalue is treated as "no condition". Check with!== undefined.
TL;DR: An auth audit log records logins (successful and failed), logouts, MFA, password changes, role changes and token revocations. Every line answers who (userId), what (action, resource), when (an ISO 8601 UTC timestamp from toISOString()), from where (IP, user agent) and the result (success as a boolean, plus why). Never record passwords, tokens, session IDs or OTP codes. Keep it append-only: the database allows only INSERT and SELECT for the app's user, which does not own the table, so the log can be trusted as evidence. A query filters by every condition sent at once, and a condition that wasn't sent (undefined) doesn't narrow the result.
⚡ Record successes and failures
Meaning: An auth audit log records logins (successful and failed), logouts, MFA, password changes, role changes and token revocations. A successful login from a strange IP is exactly the sign that someone else is using the account.
⚡ Five questions on every line
Meaning: Who (userId), what (action, resource), when (an ISO 8601 UTC timestamp from toISOString()), from where (IP, user agent), and the result (success as a boolean). Never record passwords, tokens, session IDs or OTP codes.
⚡ Add only
Meaning: Like a bank statement: a wrong transfer isn't erased, a correcting line is added. In the database the app's user gets only INSERT and SELECT, and another role owns the table, so the log can be trusted as evidence.
⚡ A condition that wasn't sent doesn't narrow
Write:
function queryAuditLogs(logs, filters) {
return logs.filter((log) => {
if (filters.userId !== undefined && log.userId !== filters.userId) return false;
if (filters.action !== undefined && log.action !== filters.action) return false;
if (filters.success !== undefined && log.success !== filters.success) return false;
return true; // passed every condition that was sent
});
}
Guess first: the filter is written if (filters.success && ...), then called with { success: false }. How many lines show up?
All of them. When filters.success is false, the condition is skipped at once. With !== undefined, a false value still counts as a condition.