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.
npm install next-auth-hasura-adapterVerified import paths — ran on the pinned version, not inferred.
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.
Ensure your `next-auth` dependency is `^4.0.0` or later. Upgrade `next-auth` to a compatible version if necessary.
Execute the SQL script found in `src/data/nextauth.sql` (or provided in the README) against your database. Ensure all tables (`accounts`, `sessions`, `users`, `verification_tokens`) and their relationships are correctly set up.
Store `HASURA_GRAPHQL_ADMIN_SECRET` in your `.env.local` file or equivalent secure environment variable configuration for your deployment platform, and access it via `process.env.HASURA_GRAPHQL_ADMIN_SECRET` on the server.
Verify that the `endpoint` URL configured for the `HasuraAdapter` is the exact GraphQL endpoint URL. For example, `process.env.HASURA_GRAPHQL_ENDPOINT`.
Add `secret: process.env.NEXTAUTH_SECRET` to your `NextAuth` configuration. Generate a strong, random string for `NEXTAUTH_SECRET` and store it securely in your environment variables.
Ensure `endpoint` and `admin_secret` are correctly supplied to the `HasuraAdapter` function, typically through environment variables like `process.env.HASURA_GRAPHQL_ENDPOINT` and `process.env.HASURA_GRAPHQL_ADMIN_SECRET`.
Execute the SQL script `src/data/nextauth.sql` (or the one provided in the README) against your PostgreSQL database to create all necessary tables and relationships (e.g., `accounts`, `sessions`, `users`, `verification_tokens`).
Add the `adapter: HasuraAdapter(...)` configuration to your `NextAuth` options in `pages/api/auth/[...nextauth].ts` or equivalent API route.
Ensure you are using `import { HasuraAdapter } from 'next-auth-hasura-adapter';` and that the package is properly installed via `npm install next-auth-hasura-adapter`.