NextAuth (Auth.js)
Authentication for Next.js with OAuth providers, credentials, session cookies, callbacks, and route-handler integration.
Official Documentation ↗NextAuth 5/Auth.js with the App Router; the v4 API differs. Check the official docs for the latest API surface.Installation
npm install next-authpnpm add next-authyarn add next-authMethods
No methods match this filter.
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.
- This is a random value used by the library to encrypt tokens and email verification hashes. (https://authjs.dev/getting-started/installation)
- You can generate one via the official Auth.js CLI running: npx auth secret This will also add it to your .env file, respecting the framework conventions (eg.: Next.js’ .env.local ). (https://authjs.dev/getting-started/installation)
- Migrate to Better Auth Authentication About Auth.js Introduction Security Discord Community Download GitHub NPM Acknowledgements Contributors Auth.js © Better Auth Inc. (https://authjs.dev/getting-started/installation)
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.
Report a problem with this API docOpens a pre-filled issue on GitHub.