next-auth

Complete, open-source authentication for Next.js — OAuth, email, and credentials with almost no boilerplate.

next-auth

React

Complete, open-source authentication for Next.js — OAuth, email, and credentials with almost no boilerplate.

Fit

Bundle (gzip)

82.5 kB

282.8 kB raw

Install size includes transitive dependencies. Bundle size is gzipped browser payload.

Freshness

The Problem

Rolling your own auth means handling OAuth callbacks, session cookies, CSRF, token rotation, and secure storage — a lot of surface area to get wrong. Hosted providers solve it but add cost, lock-in, and a redirect flow you don't fully control.

What It Does

NextAuth (Auth.js) drops into a Next.js app and handles sign-in with 80+ OAuth providers, magic-link email, and credentials out of the box. It manages secure, encrypted session cookies (or database sessions), CSRF protection, and callbacks, exposing a simple useSession hook and server helpers.

Installation

bash
npm install next-auth

Usage Example

js
// app/api/auth/[...nextauth]/route.js
import NextAuth from 'next-auth';
import GitHub from 'next-auth/providers/github';

const handler = NextAuth({
  providers: [
    GitHub({ clientId: process.env.GITHUB_ID, clientSecret: process.env.GITHUB_SECRET }),
  ],
});

export { handler as GET, handler as POST };

Related packages