Audit sign-ins and sign-outs
Security monitoring
Auth.js · Pipe
Auth.js events are optional asynchronous lifecycle handlers for side effects such as audit logging. They observe authentication activity without returning an authentication response.
The optional events configuration maps authentication lifecycle event names to asynchronous handlers. Handlers receive an event-specific message, perform side effects such as audit logging, and do not produce an authentication response.
events: { createUser?: (message) => Awaitable<void>; linkAccount?: (message) => Awaitable<void>; session?: (message) => Awaitable<void>; signIn?: (message) => Awaitable<void>; signOut?: (message) => Awaitable<void>; updateUser?: (message) => Awaitable<void>; }| Name | Type | Description |
|---|---|---|
events | { createUser?: (message) => Awaitable<void>; linkAccount?: (message) => Awaitable<void>; session?: (message) => Awaitable<void>; signIn?: (message) => Awaitable<void>; signOut?: (message) => Awaitable | Optional lifecycle handlers: createUser, linkAccount, session, signIn, signOut, and updateUser; each accepts a message and returns Awaitable<void>. |
Event handlers return Awaitable<void>; the events configuration itself does not return an authentication response.
import NextAuth from "next-auth";
export const { handlers, auth } = NextAuth({
providers: [],
events: {
async signIn({ user }) {
console.log("Signed in:", user.email);
},
},
});This registers an asynchronous sign-in event handler that can be replaced with audit-log persistence.
Security monitoring
Account lifecycle tracking
Operational observability
Events are asynchronous handlers configured under events. Unlike callbacks, they do not return a response and are intended for side effects such as audit logging.
Follow-up: What should an event handler do with its message? Inspect it according to the specific event and flow, because the message shape is not uniform.
The documented event names are createUser, linkAccount, session, signIn, signOut, and updateUser. Each handler receives a message and resolves to void.
Follow-up: How would you record a sign-out? Add an asynchronous signOut handler and persist the relevant event details to an audit system.
The message commonly contains a user object and/or JWT contents, but the exact data depends on whether the flow uses OAuth or email authentication and JWT or database sessions.
Follow-up: Why must code branch defensively? The same event name can receive different message contents across authentication and session configurations.
callbackscreateUserlinkAccountsessionsignInsignOutupdateUserEventError