Issue or inspect an Auth.js JWT
Custom server-side token handling
Auth.js · Pipe
Auth.js JWT utilities encode and decode the encrypted JWTs used by Auth.js. They use AUTH_SECRET or an explicit secret and are intended primarily for the app that issued them.
The @auth/core/jwt module provides helpers for issuing and reading Auth.js JWTs. Tokens are encrypted by default with A256CBC-HS512, using AUTH_SECRET or a supplied secret; they are intended for the application that created them rather than as general-purpose third-party API tokens.
encode<Payload>(params): Promise<string>
decode<Payload>(params): Promise<null | Payload>
getToken<R>(params): Promise<R extends true ? string : null | JWT>encode returns a JWT string; decode returns the decoded payload or null; getToken returns the payload or raw JWT, and returns null when no usable token is found.
encode returns a newly issued encrypted token rather than a plaintext payload.decode reads an Auth.js token and resolves its payload when the token and secret are valid.getToken can return structured claims or the original compact token, depending on its options.import { encode, decode } from "@auth/core/jwt"
const secret = process.env.AUTH_SECRET!
const token = await encode({
token: { sub: "user-123", role: "admin" },
secret,
})
const payload = await decode({ token, secret })
console.log(payload?.role)This creates an encrypted Auth.js JWT and then decodes it with the same secret.
Custom server-side token handling
Request authentication
Claim processing
The module exposes encode to issue a JWT and decode to read an Auth.js-issued JWT. Auth.js encrypts the JWT by default with A256CBC-HS512, deriving the key from the configured secret.
Follow-up: Why should an Auth.js JWT not normally be sent to an unrelated API? It is designed for use by the application that issued it, so an identity-provider access token is the appropriate credential for a third-party API.
getToken accepts an Auth.js request and returns either the decoded payload or, when requested, the raw JWT string. It searches cookies and the Authorization header for the token.
Follow-up: What happens when no usable token is found? The function returns null, including when a Bearer value has malformed percent-encoding.
The secret can come from the AUTH_SECRET environment variable or from the secret parameter passed to the helper. That secret is used to derive the encryption key for the token.
Follow-up: What deployment requirement follows from this? The application must provide a cryptographically secure AUTH_SECRET; the deployment documentation recommends a random value of at least 32 characters.
encodedecodegetTokenJWTAUTH_SECRETsession.strategy