Better Auth

Better Auth Integration

Automatically identify users on every request. Every wide event includes who made the request — userId, user profile, and session metadata — with zero manual work.

evlog/better-auth turns anonymous wide events into identified ones. Every request automatically includes who made it — no manual log.set({ user }) needed.

Prerequisites

Use Better Auth as a direct dependency in your app. evlog does not bundle Better Auth. The integration is tested against Better Auth >=1.6.9 (same major as the playground).

pnpm add better-auth

Add Better Auth user identification

Quick Start

One middleware, all requests identified:

import { createAuthMiddleware } from 'evlog/better-auth'

const identify = createAuthMiddleware(auth, {
  exclude: ['/api/auth/**'],
})

export default defineEventHandler(async (event) => {
  if (!event.context.log) return
  await identify(event.context.log, event.headers, event.path)
})

Your wide event now includes the user:

{
  "level": "info",
  "method": "POST",
  "path": "/api/checkout",
  "status": 200,
  "duration": "120ms",
  "requestId": "a5669202-7765-4f59-b6f0-b9f40ce71599",
  "cart": { "items": 3, "total": 9999 }
}

How It Works

The integration resolves the Better Auth session from request cookies, extracts safe user and session fields, and sets them on the logger. Auth routes are skipped by default.

RequestMiddlewareBetter AuthLoggerDrain

  1. Incoming request hits your middleware
  2. Middleware checks include/exclude patterns — skips if route doesn't match
  3. getSession(headers) resolves the session via Better Auth (timing is captured)
  4. identifyUser(log, session) sets userId, user, and session on the logger
  5. onIdentify or onAnonymous hook fires based on session result
  6. At request end, the wide event is emitted with full user context to your drain

Where to next

Identify User

The core building block — extract safe fields, mask emails, capture plugin data (organizations, roles, 2FA).

Middleware

Filter routes with include/exclude, react to identification with lifecycle hooks, and tune behaviour per app.

Client Sync

Mirror the user identity into client-side logs with setIdentity and the Better Auth client.

Performance

Watch session resolution time, enable session caching, and combine with the AI SDK integration.

Public API

ExportDescription
identifyUser(log, session)Core helper — extracts safe fields from a session and sets them on the logger. Returns true if identified.
createAuthMiddleware(auth)Returns an async (log, headers, path?) => Promise<boolean> function with route filtering, timing, and hooks.
createAuthIdentifier(auth)Nitro request hook factory for standalone Nitro apps. See Performance.
maskEmail(email)Mask an email: hugo@example.comh***@example.com.