Auth.js (NextAuth.js)

repository·main·Indexed 12 days ago

https://github.com/nextauthjs/next-auth

A set of open-source, framework-agnostic and runtime-agnostic packages providing authentication for modern web applications. It supports OAuth, Email, and Passkeys, and provides dedicated integrations for Next.js, Express (@auth/express), SvelteKit (@auth/sveltekit), and Qwik City, as well as various database adapters including Drizzle, Firebase, Cloudflare D1, and Azure Table Storage.

Tokens
195.8K
Snippets
770
Records
929
Agent score
97%

What's inside Auth.js

  1. What is Auth.js?

    main

    Auth.js is a runtime-agnostic authentication library built on standard Web APIs. It is designed to integrate with various modern JavaScript frameworks to provide a secure, private, and extensible authentication experience.

    Note: This documentation specifically covers next-auth@5.0.0-beta and later, as well as other framework-specific packages under the @auth/* namespace. For legacy next-auth@4.x.y documentation, refer to next-auth.js.org.

  2. Overview of Auth.js Core

    main
    Auth.js core (@auth/core) is the underlying authentication engine designed for the web. It provides a framework-agnostic authentication logic that can be used across various web environments. It is written in TypeScript and focuses on being secure by default and allowing developers to own their data.
  3. Overview of Auth.js

    main

    Auth.js (formerly NextAuth.js) is an open-source, full-stack authentication solution built on standard Web APIs. It is designed to work with any framework, on any platform, and in any JavaScript runtime (e.g., Node.js, Docker, Serverless).

    Key capabilities include:

    • Flexible Sign-in: Supports OAuth 2.0+, OIDC, Email/Passwordless, and Passkeys/WebAuthn.
    • Database Agnostic: Can be used with or without a database. If used with a database, it supports MySQL, MariaDB, Postgres, Microsoft SQL Server, MongoDB, SQLite, GraphQL, and more via adapters.
    • Secure by Default: Implements CSRF protection on POST routes, uses restrictive cookie policies, and encrypts JSON Web Tokens (JWE) using A256CBC-HS512 by default.
    • Type Safe: Built with TypeScript for robust developer experience.
  4. Overview of Framework and Database Integrations

    main

    NextAuth.js provides integrations for various web frameworks and databases. Most modern integrations are published under the @auth/* scope and are built upon the @auth/core library.

    Framework Integrations

    • NextAuth.js (next-auth): Available in v4 (maintenance mode) and v5 (beta).
    • Qwik Auth (@auth/qwik): Released.
    • Express Auth (@auth/express): Experimental Release.
    • SolidStart Auth (@auth/solid-start): Experimental Release.
    • SvelteKit Auth (@auth/sveltekit): Experimental Release.
    • Astro, Fastify, Nuxt, and Remix: Currently in development (Open PRs).

    Database Adapters

    • @auth/*-adapter: The current standard. These are released and fully compatible with next-auth and all @auth/* framework libraries.
    • @next-auth/*-adapter: Legacy adapters. Maintenance has stopped; users should migrate to the @auth/*-adapter versions.
  5. Overview of Official Database Adapters

    main
    Auth.js provides official adapters distributed as individual packages under the @auth/ namespace. These adapters act as the bridge between Auth.js and your database. If your specific database is not supported, you can create a custom adapter by following the official guide and ensuring it implements the required Auth.js models.
  6. Overview of NextAuth.js

    main
    NextAuth.js is an authentication solution specifically designed for Next.js applications. It provides a secure, flexible, and easy-to-use way to implement authentication, allowing developers to own their data while benefiting from secure-by-default patterns. It is written in TypeScript and supports various authentication providers and database adapters.
  7. E2E Testing Strategies for Auth.js

    main

    Automated End-to-End (E2E) testing of authentication can be difficult because OAuth providers often trigger security verification steps (like geographic location checks or new user-agent alerts) when accessed from datacenters or CI environments.

    To achieve consistent and repeatable testing, use one of these two strategies:

    1. Run a local OAuth provider: Use software like Keycloak to host your own identity provider. This provides a stable, controlled environment for testing real OAuth flows.
    2. Use the Credentials provider in development: Enable a Credentials provider that is conditionally added to your configuration only when process.env.NODE_ENV === 'development'. This avoids the overhead of a separate provider but requires strict environment checks to ensure it is never enabled in production.
  8. New features in NextAuth.js v5

    main

    NextAuth.js v5 introduces several key improvements:

    • App Router-first: Optimized for Next.js App Router, though pages/ is still supported.
    • Universal auth() function: A single method to authenticate in almost any environment (Server Components, Middleware, API Routes, etc.), replacing getServerSession, getSession, withAuth, getToken, and useSession.
    • Edge compatibility: Built to work in Edge runtimes.
    • Simplified setup: Shared configuration and inferred environment variables.
    • New account() callback: Available on providers for enhanced data handling.
    • OAuth support on preview deployments: Improved security for preview environments.
  9. Authentication methods in Auth.js

    main

    Auth.js supports four primary authentication methods:

    1. OAuth authentication: Social logins such as Google, GitHub, LinkedIn, etc.
    2. Magic Links: Passwordless email-based authentication using providers like Resend, Sendgrid, Nodemailer, or Forward Email.
    3. Credentials: Traditional username/password authentication or integration with existing external APIs.
    4. WebAuthn: Modern authentication using Passkeys and other biometric/hardware standards.