API Reference

NextAuth (Auth.js)

Authentication for Next.js with OAuth providers, credentials, session cookies, callbacks, and route-handler integration.

Official Documentation ↗
CompatibilityThis documentation covers version NextAuth 5/Auth.js with the App Router; the v4 API differs. Check the official docs for the latest API surface.

Installation

npmnpm install next-auth
pnpmpnpm add next-auth
yarnyarn add next-auth

Methods

NextAuth provides authentication flows for Next.js: provider redirects, callback handling, session cookies, and helpers for server and client code. It does not replace per-resource authorization.

Shared server-only configuration

Define providers, callbacks, and session strategy in one server-only module. Export handlers, auth, signIn, and signOut. The route handler re-exports the handlers. Keep provider secrets in environment variables and never import the auth module into client components.

Protect server work

Use await auth() in server components, route handlers, and server actions. A client-side session check only hides UI; a direct request can still reach the server. Combine authentication with ownership and role checks for every sensitive operation.

Session strategy

JWT sessions avoid a session-store lookup but revocation is delayed until expiry. Database sessions allow immediate logout and revocation but add a store dependency. Choose deliberately and document the logout behavior.

Security notes

Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://authjs.dev/getting-started/installation · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:51:07.989Z.

FAQ

Does NextAuth replace authorization checks in route handlers?
No. It authenticates the session. Each route handler or server action must still check whether the user may access the specific resource.
Where should auth configuration live?
In a shared server-only module that exports handlers, auth, signIn, and signOut. Import it in route handlers and server code; never expose secrets to the client.
What session strategy should I use?
JWT sessions avoid a database lookup per request but are harder to revoke. Database sessions allow immediate revocation at the cost of a store. Choose based on logout, revocation, and scaling requirements.