Audit logging
Record session activity for compliance or security investigations.
Auth.js · Enum
The Auth.js session event is an asynchronous notification hook for session activity. Use it for side effects such as audit logging; its message varies with the authentication flow and session strategy.
The session event is an asynchronous handler invoked when Auth.js processes a session event. It receives a message whose contents depend on the authentication flow and session strategy, so it is suitable for audit logging rather than changing the response.
events: { session: (message) => Awaitable<void> }| Name | Type | Description |
|---|---|---|
message | object | Event message whose fields depend on the authentication flow and session strategy; it can include a user, JWT contents, or an adapter session. |
Awaitable<void>; the event does not return a response.
token field.session field.import NextAuth from "next-auth";
export const { handlers, auth } = NextAuth({
providers: [],
events: {
async session(message) {
console.log("Session event", message);
},
},
});Registers an asynchronous handler that can record session activity for auditing.
Record session activity for compliance or security investigations.
Capture session-related details for operational diagnostics.
Send session activity to an internal monitoring or analytics pipeline.
The session event is configured under the Auth.js events option as an asynchronous function receiving a message. It runs when a session event occurs and is intended for side effects such as audit logging, not for returning a response.
Follow-up: How should the handler be designed for different session strategies? Inspect the message according to the configured flow because it may contain a JWT token or an adapter-backed session object.
Its signature is (message) => Awaitable<void>. The callback does not produce a response; it receives event information and completes asynchronously.
Follow-up: What if application code needs to alter data exposed in the session? That is a different concern from this event handler and should use the appropriate session configuration callback.
The message depends on the authentication flow, including whether the application uses OAuth or email authentication and JWT or database sessions. It typically contains a user and/or token-related information.
Follow-up: Which session-specific values can appear? With JWT sessions the message can contain token; with database-persisted sessions it can contain session from the adapter.
eventssignIn eventsignOut eventcreateUser eventlinkAccount eventupdateUser eventJWT sessionsdatabase sessions