X Enterprises
fastify-xauth-better

auditLog.log(event, data)

Writes a structured audit event to the AuthAuditLog Prisma model.

auditLog.log(event, data)

Writes a structured audit event to the AuthAuditLog table. Event name must be drawn from the 30-event vocabulary (see table below). IP address and user agent are automatically extracted from the request when provided.

Signature

instance.auditLog.log(
  event: string,
  data?: {
    userId?: string
    targetId?: string
    metadata?: Record<string, unknown>
    request?: FastifyRequest
  }
): Promise<AuthAuditLog>

Params

NameTypeRequiredDescription
eventstringYesOne of the 30 allowed audit event names (see table below)
data.userIdstringNoID of the user performing the action
data.targetIdstringNoID of the affected resource or user
data.metadataobjectNoArbitrary JSON — stored as Json in Prisma
data.requestFastifyRequestNoFastify request — used to capture IP and user-agent automatically

Allowed events

The full 30-event vocabulary (ALLOWED_EVENTS):

CategoryEvents
Authauth.login.success, auth.login.failed, auth.logout, auth.signup
Passwordauth.password.changed, auth.password.reset, auth.password.reset.requested, auth.password.reset.completed
Emailauth.email.verified
2FAauth.2fa.enabled, auth.2fa.disabled
Sessionauth.session.created, auth.session.deleted, auth.session.revoked
Tokenauth.token.created, auth.token.revoked
Accountauth.account.linked, auth.account.banned
Org membership (auth)auth.org.joined, auth.org.left, auth.org.role.changed
Organizationorg.created, org.updated, org.deleted
Org membersorg.member.added, org.member.removed, org.member.role.changed
Org invitationsorg.invitation.sent, org.invitation.accepted, org.invitation.revoked

The auditLog.events config option is a subscription filter over this vocabulary — events not listed are silently skipped (log() returns null). The default subscription list covers 19 of these events.

Returns

Promise<AuthAuditLog> — the created Prisma record.

Throws

  • Error: Invalid audit event: {event}. Allowed events: ... — the event string is not in the allowed vocabulary

Examples

Log a successful login

const userAuth = fastify.xAuthBetter.get("user");

fastify.post("/api/login-webhook", async (request) => {
  const { userId } = request.body;

  await userAuth.auditLog.log("auth.login.success", {
    userId,
    metadata: { method: "oauth", provider: "google" },
    request,
  });

  return { ok: true };
});

Log an org role change with target user

await userAuth.auditLog.log("auth.org.role.changed", {
  userId: request.user.id,          // who made the change
  targetId: memberId,               // who was changed
  metadata: {
    orgId: request.organization.id,
    previousRole: "member",
    newRole: "admin",
  },
  request,
});

See also

AI Context

package: "@xenterprises/fastify-xauth-better"
method: fastify.xAuthBetter.get(name).auditLog.log(event, data)
use-when: Write a structured audit event to the AuthAuditLog table
events: 30-event vocabulary (auth.login.success/failed, auth.logout, auth.signup, auth.password.*, auth.email.verified, auth.2fa.*, auth.session.*, auth.token.*, auth.account.*, auth.org.*, org.*, org.member.*, org.invitation.*) — auditLog.events config is a subscription filter (default: 19 events)
Copyright © 2026