Audit logging
Record successful authentication activity for security review.
Auth.js · Enum
events.signIn is an asynchronous Auth.js event handler for work triggered by a sign-in. It is intended for side effects such as audit logging, not for controlling whether authentication succeeds.
The events.signIn handler runs asynchronously after a sign-in-related event and receives a message whose contents depend on the authentication flow and session strategy. Use it for side effects such as audit logging; it does not decide whether access is allowed.
signIn: (message) => Awaitable<void>Awaitable<void>
events.signIn for side effects such as recording successful sign-ins or creating audit entries.import NextAuth from "next-auth";
export const { handlers, auth } = NextAuth({
providers: [],
events: {
async signIn(message) {
console.log("User signed in", message.user?.id);
},
},
});Register an asynchronous handler to record each completed sign-in event.
Record successful authentication activity for security review.
Inspect authentication payloads while integrating providers or session strategies.
Send a notification or update an external system after sign-in.
signIn event?events.signIn is an asynchronous event handler configured under events. It is useful for audit logging and does not return a response.
Follow-up: How would you decide whether a user may sign in? Use the callbacks.signIn callback, whose return value can continue, stop, or redirect the flow.
signIn event message contain?The handler receives a message object. Its exact contents depend on the authentication flow and session strategy, but it commonly includes a user and/or JWT information.
Follow-up: What should production code do with that variability? Treat optional fields defensively and log only the data required for the audit record.
Configure it in the Auth.js options as events: { signIn: async (message) => { ... } }. The handler can perform asynchronous work such as writing an audit entry.
Follow-up: What is the corresponding authorization mechanism? Configure callbacks.signIn, not events.signIn, when access control is required.
eventscallbacks.signIncreateUserlinkAccountsessionsignOutupdateUser