Auth.js (NextAuth.js)
Overview
Auth.js is an open-source set of packages for authentication built on standard Web APIs, usable with any framework on any JS runtime. It was formerly known as NextAuth.js; the next-auth package serves Next.js, while sibling packages (@auth/sveltekit, @auth/express, Qwik, etc.) target other frameworks. Read this when wiring sign-in flows, OAuth providers, or sessions into a JS/TS app.
Official sources
Install / setup
npm install next-auth@beta
After installing, generate the required secret:
npx auth secret
(For other frameworks the official install page lists @auth/sveltekit, @auth/express, and the Qwik qwik add auth flow.)
Core concepts
- Providers: pluggable sign-in methods — OAuth/OIDC providers, email (magic link), and credentials. Configure them in the Auth.js config object.
- Sessions: Auth.js issues a session that can be a JWT (default, stateless) or database-backed via an adapter; you read it on server and client.
- Adapters: connect Auth.js to your database (Prisma, Drizzle, etc.) to persist users, accounts, and sessions.
- Callbacks: hooks (
signIn, jwt, session, redirect) let you customize token contents, authorization, and redirects.
AUTH_SECRET: required environment variable used to encrypt tokens and sign cookies; generated with npx auth secret.
- Framework packages: the core is framework-agnostic; you install the package matching your framework (
next-auth, @auth/sveltekit, @auth/express).
Best practices
- Always set a strong
AUTH_SECRET (use npx auth secret) and keep it out of source control. See https://authjs.dev/getting-started/installation.
- Prefer OAuth/OIDC or email providers over the Credentials provider; the docs note credentials are inherently less secure and bypass much of Auth.js's built-in protection.
- Use an adapter when you need to persist users/accounts or link multiple providers to one user.
- Restrict access using the
signIn/authorized callbacks rather than ad-hoc checks scattered through your app.
Common pitfalls
- Installing the stable major when following current docs → the v5 (Auth.js) getting-started uses
next-auth@beta; mixing v4 docs with v5 config breaks setup.
- Forgetting
AUTH_SECRET in production → sign-in fails or sessions cannot be decrypted; set it in the deployment environment.
- Assuming Auth.js is Next.js-only → it is framework-agnostic; use the correct package for your framework.
Examples
// auth.ts (Next.js, Auth.js v5)
import NextAuth from "next-auth"
import GitHub from "next-auth/providers/github"
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [GitHub],
})
Further reading
Related skills
- ../auth0 — hosted OAuth/OIDC platform you can plug in as a provider
- ../clerk — alternative hosted auth with prebuilt UI
1---2name: authjs3description: Framework-agnostic authentication for JS/TS apps (Auth.js, formerly NextAuth.js) — consult when adding OAuth/OIDC, email, or credentials sign-in.4domain: stack5category: auth6tags: [auth, oauth, oidc, nextauth, nextjs, sveltekit, sessions]7official_sources:8 - https://authjs.dev/9 - https://github.com/nextauthjs/next-auth10verified: 2026-06-1611metadata:12 source: https://skillmd.com/skills/gabriel7silva/authjs13---1415# Auth.js (NextAuth.js)1617## Overview18Auth.js is an open-source set of packages for authentication built on standard Web APIs, usable with any framework on any JS runtime. It was formerly known as NextAuth.js; the `next-auth` package serves Next.js, while sibling packages (`@auth/sveltekit`, `@auth/express`, Qwik, etc.) target other frameworks. Read this when wiring sign-in flows, OAuth providers, or sessions into a JS/TS app.1920## Official sources21- Docs: https://authjs.dev/22- Repo: https://github.com/nextauthjs/next-auth23- Install / download: https://authjs.dev/getting-started/installation2425## Install / setup26```bash27npm install next-auth@beta28```29After installing, generate the required secret:30```bash31npx auth secret32```33(For other frameworks the official install page lists `@auth/sveltekit`, `@auth/express`, and the Qwik `qwik add auth` flow.)3435## Core concepts36- Providers: pluggable sign-in methods — OAuth/OIDC providers, email (magic link), and credentials. Configure them in the Auth.js config object.37- Sessions: Auth.js issues a session that can be a JWT (default, stateless) or database-backed via an adapter; you read it on server and client.38- Adapters: connect Auth.js to your database (Prisma, Drizzle, etc.) to persist users, accounts, and sessions.39- Callbacks: hooks (`signIn`, `jwt`, `session`, `redirect`) let you customize token contents, authorization, and redirects.40- `AUTH_SECRET`: required environment variable used to encrypt tokens and sign cookies; generated with `npx auth secret`.41- Framework packages: the core is framework-agnostic; you install the package matching your framework (`next-auth`, `@auth/sveltekit`, `@auth/express`).4243## Best practices44- Always set a strong `AUTH_SECRET` (use `npx auth secret`) and keep it out of source control. See https://authjs.dev/getting-started/installation.45- Prefer OAuth/OIDC or email providers over the Credentials provider; the docs note credentials are inherently less secure and bypass much of Auth.js's built-in protection.46- Use an adapter when you need to persist users/accounts or link multiple providers to one user.47- Restrict access using the `signIn`/`authorized` callbacks rather than ad-hoc checks scattered through your app.4849## Common pitfalls50- Installing the stable major when following current docs → the v5 (Auth.js) getting-started uses `next-auth@beta`; mixing v4 docs with v5 config breaks setup.51- Forgetting `AUTH_SECRET` in production → sign-in fails or sessions cannot be decrypted; set it in the deployment environment.52- Assuming Auth.js is Next.js-only → it is framework-agnostic; use the correct package for your framework.5354## Examples55```ts56// auth.ts (Next.js, Auth.js v5)57import NextAuth from "next-auth"58import GitHub from "next-auth/providers/github"5960export const { handlers, signIn, signOut, auth } = NextAuth({61 providers: [GitHub],62})63```6465## Further reading66- https://authjs.dev/getting-started — official getting-started guides per framework67- https://authjs.dev/getting-started/providers — provider catalog6869## Related skills70- ../auth0 — hosted OAuth/OIDC platform you can plug in as a provider71- ../clerk — alternative hosted auth with prebuilt UI