@stotles/better-auth-audit-logs 0.4.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/dist/index.cjs +41 -9
- package/dist/index.js +42 -10
- package/package.json +4 -5
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
Audit log plugin for [Better Auth](https://better-auth.com). Automatically captures auth events with IP, user agent, and severity — zero config required.
|
|
8
8
|
|
|
9
|
-
**Requires** `better-auth >= 1.
|
|
9
|
+
**Requires** `better-auth >= 1.5.0` and `typescript >= 5`.
|
|
10
10
|
|
|
11
11
|
## Quick start
|
|
12
12
|
|
|
@@ -314,6 +314,10 @@ auditLog({
|
|
|
314
314
|
|
|
315
315
|
This plugin was inspired by the audit log design shared by [@Re4GD](https://github.com/Re4GD) in [better-auth/better-auth#1184](https://github.com/better-auth/better-auth/issues/1184). Additional inspiration from [@issamwahbi](https://github.com/issamwahbi) ([#3592](https://github.com/better-auth/better-auth/discussions/3592)) and [@ItsProless](https://github.com/ItsProless) ([#7952](https://github.com/better-auth/better-auth/discussions/7952)).
|
|
316
316
|
|
|
317
|
+
This plugin was originally maintained by [@ejirocodes](https://github.com/ejirocodes/better-auth-audit-logs).
|
|
318
|
+
|
|
319
|
+
This fork is maintained by [@Stotles](https://github.com/Stotles).
|
|
320
|
+
|
|
317
321
|
## License
|
|
318
322
|
|
|
319
323
|
[MIT](./LICENSE)
|
package/dist/index.cjs
CHANGED
|
@@ -415,6 +415,25 @@ function createBeforeHooks(opts, modelName) {
|
|
|
415
415
|
}
|
|
416
416
|
// src/hooks/after.ts
|
|
417
417
|
var import_api2 = require("better-auth/api");
|
|
418
|
+
function classifyRedirect(err) {
|
|
419
|
+
const isRedirect = err.statusCode >= 300 && err.statusCode < 400;
|
|
420
|
+
if (!isRedirect)
|
|
421
|
+
return null;
|
|
422
|
+
const location = new Headers(err.headers).get("location");
|
|
423
|
+
if (!location)
|
|
424
|
+
return { failed: false };
|
|
425
|
+
try {
|
|
426
|
+
const params = new URL(location, "http://localhost").searchParams;
|
|
427
|
+
const code = params.get("error");
|
|
428
|
+
if (!code) {
|
|
429
|
+
return { failed: false };
|
|
430
|
+
}
|
|
431
|
+
const description = params.get("error_description");
|
|
432
|
+
return { failed: true, error: { code, ...description ? { message: description } : {} } };
|
|
433
|
+
} catch {
|
|
434
|
+
return { failed: false };
|
|
435
|
+
}
|
|
436
|
+
}
|
|
418
437
|
function createAfterHooks(opts, modelName) {
|
|
419
438
|
return [
|
|
420
439
|
{
|
|
@@ -422,21 +441,34 @@ function createAfterHooks(opts, modelName) {
|
|
|
422
441
|
handler: import_api2.createAuthMiddleware(async (ctx) => {
|
|
423
442
|
try {
|
|
424
443
|
const path = ctx.path;
|
|
425
|
-
const
|
|
426
|
-
const status = isError ? "failed" : "success";
|
|
444
|
+
const returned = ctx.context.returned;
|
|
427
445
|
const user = ctx.context.newSession?.user ?? ctx.context.session?.user;
|
|
428
446
|
const pathConfig = opts.getPathConfig(path);
|
|
429
447
|
const metadata = {};
|
|
430
448
|
if (opts.capture.requestBody && ctx.body) {
|
|
431
449
|
metadata.requestBody = ctx.body;
|
|
432
450
|
}
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
451
|
+
let status = "success";
|
|
452
|
+
if (import_api2.isAPIError(returned)) {
|
|
453
|
+
const redirect = classifyRedirect(returned);
|
|
454
|
+
if (redirect) {
|
|
455
|
+
if (redirect.failed) {
|
|
456
|
+
status = "failed";
|
|
457
|
+
metadata.error = redirect.error;
|
|
458
|
+
}
|
|
459
|
+
} else {
|
|
460
|
+
status = "failed";
|
|
461
|
+
metadata.error = {
|
|
462
|
+
...returned.message && { message: returned.message },
|
|
463
|
+
...returned.status && { status: returned.status },
|
|
464
|
+
...returned.body?.code && { code: returned.body.code }
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
} else if (returned instanceof Error) {
|
|
468
|
+
status = "failed";
|
|
469
|
+
if (returned.message) {
|
|
470
|
+
metadata.error = { message: returned.message };
|
|
471
|
+
}
|
|
440
472
|
}
|
|
441
473
|
const entry = await buildLogEntry(path, status, {
|
|
442
474
|
userId: user?.id ?? null,
|
package/dist/index.js
CHANGED
|
@@ -344,7 +344,26 @@ function createBeforeHooks(opts, modelName) {
|
|
|
344
344
|
];
|
|
345
345
|
}
|
|
346
346
|
// src/hooks/after.ts
|
|
347
|
-
import { createAuthMiddleware as createAuthMiddleware2 } from "better-auth/api";
|
|
347
|
+
import { createAuthMiddleware as createAuthMiddleware2, isAPIError } from "better-auth/api";
|
|
348
|
+
function classifyRedirect(err) {
|
|
349
|
+
const isRedirect = err.statusCode >= 300 && err.statusCode < 400;
|
|
350
|
+
if (!isRedirect)
|
|
351
|
+
return null;
|
|
352
|
+
const location = new Headers(err.headers).get("location");
|
|
353
|
+
if (!location)
|
|
354
|
+
return { failed: false };
|
|
355
|
+
try {
|
|
356
|
+
const params = new URL(location, "http://localhost").searchParams;
|
|
357
|
+
const code = params.get("error");
|
|
358
|
+
if (!code) {
|
|
359
|
+
return { failed: false };
|
|
360
|
+
}
|
|
361
|
+
const description = params.get("error_description");
|
|
362
|
+
return { failed: true, error: { code, ...description ? { message: description } : {} } };
|
|
363
|
+
} catch {
|
|
364
|
+
return { failed: false };
|
|
365
|
+
}
|
|
366
|
+
}
|
|
348
367
|
function createAfterHooks(opts, modelName) {
|
|
349
368
|
return [
|
|
350
369
|
{
|
|
@@ -352,21 +371,34 @@ function createAfterHooks(opts, modelName) {
|
|
|
352
371
|
handler: createAuthMiddleware2(async (ctx) => {
|
|
353
372
|
try {
|
|
354
373
|
const path = ctx.path;
|
|
355
|
-
const
|
|
356
|
-
const status = isError ? "failed" : "success";
|
|
374
|
+
const returned = ctx.context.returned;
|
|
357
375
|
const user = ctx.context.newSession?.user ?? ctx.context.session?.user;
|
|
358
376
|
const pathConfig = opts.getPathConfig(path);
|
|
359
377
|
const metadata = {};
|
|
360
378
|
if (opts.capture.requestBody && ctx.body) {
|
|
361
379
|
metadata.requestBody = ctx.body;
|
|
362
380
|
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
381
|
+
let status = "success";
|
|
382
|
+
if (isAPIError(returned)) {
|
|
383
|
+
const redirect = classifyRedirect(returned);
|
|
384
|
+
if (redirect) {
|
|
385
|
+
if (redirect.failed) {
|
|
386
|
+
status = "failed";
|
|
387
|
+
metadata.error = redirect.error;
|
|
388
|
+
}
|
|
389
|
+
} else {
|
|
390
|
+
status = "failed";
|
|
391
|
+
metadata.error = {
|
|
392
|
+
...returned.message && { message: returned.message },
|
|
393
|
+
...returned.status && { status: returned.status },
|
|
394
|
+
...returned.body?.code && { code: returned.body.code }
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
} else if (returned instanceof Error) {
|
|
398
|
+
status = "failed";
|
|
399
|
+
if (returned.message) {
|
|
400
|
+
metadata.error = { message: returned.message };
|
|
401
|
+
}
|
|
370
402
|
}
|
|
371
403
|
const entry = await buildLogEntry(path, status, {
|
|
372
404
|
userId: user?.id ?? null,
|
package/package.json
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stotles/better-auth-audit-logs",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Audit log plugin for Better Auth. Captures auth lifecycle events, stores structured log entries, and exposes query endpoints with PII redaction and custom storage backends.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"author": "Ejiro Asiuwhu <ejiroasiuwhu10@gmail.com>",
|
|
8
7
|
"repository": {
|
|
9
8
|
"type": "git",
|
|
10
9
|
"url": "git+https://github.com/Stotles/better-auth-audit-logs.git"
|
|
@@ -61,9 +60,9 @@
|
|
|
61
60
|
"check": "tsc --noEmit && bun test"
|
|
62
61
|
},
|
|
63
62
|
"peerDependencies": {
|
|
64
|
-
"better-auth": ">=1.
|
|
65
|
-
"
|
|
66
|
-
"
|
|
63
|
+
"better-auth": ">=1.5.0",
|
|
64
|
+
"typescript": "^5",
|
|
65
|
+
"zod": ">=3.0.0"
|
|
67
66
|
},
|
|
68
67
|
"devDependencies": {
|
|
69
68
|
"@types/bun": "latest",
|