NextAuth.js Hasura Adapter

2.0.0 · active · verified Wed Apr 22

The `next-auth-hasura-adapter` is a specialized adapter designed to integrate NextAuth.js authentication with a Hasura GraphQL engine backend. It is currently at version 2.0.0 and functions as a peer dependency of `next-auth` (specifically compatible with `next-auth` v4.x and above), meaning its release cycle is inherently tied to the evolution of the core NextAuth.js library. This package facilitates the storage and retrieval of authentication-related data, such as users, accounts, sessions, and verification tokens, directly within a PostgreSQL database managed by Hasura. It distinguishes itself by providing a `HasuraAdapter` instance that connects to a Hasura endpoint via GraphQL, eliminating the need for separate ORM configurations or database setups for NextAuth's internal data. Developers must apply a provided SQL schema to their Hasura-connected database to ensure the necessary tables and relationships are present for the adapter to function correctly. This makes it an ideal choice for projects already leveraging Hasura as their backend.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure NextAuth.js with the Hasura Adapter in a Next.js API route, including setting up providers, adapter options using environment variables, and basic JWT session handling for user ID propagation.

// pages/api/auth/[...nextauth].ts
import NextAuth from "next-auth";
import GitHubProvider from "next-auth/providers/github"; // Example provider
import { HasuraAdapter } from "next-auth-hasura-adapter";

// Ensure your Hasura GraphQL endpoint and admin secret are set as environment variables.
// Example: HASURA_GRAPHQL_ENDPOINT=https://your-hasura-instance/v1/graphql
// Example: HASURA_GRAPHQL_ADMIN_SECRET=your_admin_secret_here
const HASURA_API_ENDPOINT = process.env.HASURA_GRAPHQL_ENDPOINT ?? '';
const HASURA_ADMIN_SECRET = process.env.HASURA_GRAPHQL_ADMIN_SECRET ?? '';

if (!HASURA_API_ENDPOINT || !HASURA_ADMIN_SECRET) {
  throw new Error("Missing Hasura GraphQL Endpoint or Admin Secret environment variables. Please set HASURA_GRAPHQL_ENDPOINT and HASURA_GRAPHQL_ADMIN_SECRET.");
}

export default NextAuth({
  // Configure one or more authentication providers (e.g., GitHub)
  providers: [
    GitHubProvider({
      clientId: process.env.GITHUB_ID ?? '', // Ensure GITHUB_ID is set in your .env
      clientSecret: process.env.GITHUB_SECRET ?? '', // Ensure GITHUB_SECRET is set in your .env
    }),
    // Add more providers as needed (e.g., GoogleProvider, EmailProvider)
  ],
  // Use the Hasura Adapter to persist user data
  adapter: HasuraAdapter({
    endpoint: HASURA_API_ENDPOINT,
    admin_secret: HASURA_ADMIN_SECRET,
  }),
  // Recommended for adapters like Hasura for stateless sessions
  session: {
    strategy: "jwt"
  },
  // Define callbacks to customize JWT and session objects
  callbacks: {
    async jwt({ token, user }) {
      if (user) {
        token.id = user.id; // Attach user ID to the JWT
      }
      // Custom Hasura claims can be added here if needed for authorization
      return token;
    },
    async session({ session, token }) {
      // Expose user ID to the client via the session object
      if (session.user) {
        session.user.id = token.id;
      }
      return session;
    },
  },
  // NEXTAUTH_SECRET is required for any NextAuth.js application in production
  secret: process.env.NEXTAUTH_SECRET ?? ''
});

view raw JSON →