Allow or reject sign-in based on user or profile data
Access control
Auth.js · Pipe
Auth.js callbacks are asynchronous hooks that control authentication actions, including sign-in decisions, JWT contents, redirects, and client-visible session data. Configure them inside the callbacks option.
Auth.js callbacks are asynchronous functions configured under callbacks. They run during authentication-related actions, letting you control sign-in decisions, token contents, redirects, and the session data exposed to the client.
callbacks: {
jwt: (params) => Awaitable<null | JWT>;
redirect: (params) => Awaitable<string>;
session: (params) => Awaitable<Session | DefaultSession>;
signIn: (params) => Awaitable<string | boolean>;
}| Name | Type | Description |
|---|---|---|
params | object | Callback-specific context such as token, user, session, profile, account, trigger, or URL, depending on the callback. |
Callback-dependent Awaitable values: jwt returns null or JWT; redirect returns a string; session returns Session or DefaultSession; signIn returns string or boolean.
import NextAuth from "next-auth"
export const { handlers, auth } = NextAuth({
providers: [],
callbacks: {
async session({ session, token }) {
session.user.id = token.id
return session
},
},
})The session callback copies a value from the token into the session returned to the client.
Access control
JWT sessions
Client session shaping
Navigation control
Callbacks are asynchronous functions configured in Auth.js to control what happens during authentication actions. They can implement access controls and integrate with databases or APIs.
Follow-up: Which callback controls client-visible session data? The session callback does, and its return value is exposed to the client.
The jwt callback runs when a JWT is created at sign-in or updated when the client accesses a session. Values returned by it are saved in the JWT and can then be forwarded by the session callback.
Follow-up: How would you expose a token property? Return that property from the session callback as part of the session object.
The signIn callback determines whether sign-in continues. Returning true continues, returning false or throwing stops the flow and redirects to the error page, while returning a string redirects to that URL.
Follow-up: How can access be restricted by email domain? Inspect the profile in signIn and return whether its email ends with the permitted domain.
jwt callbacksession callbacksignIn callbackredirect callbackauthorized callbackJWTAuthConfig