{"library":"next-auth-hasura-adapter","title":"NextAuth.js Hasura Adapter","description":"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.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install next-auth-hasura-adapter"],"cli":null},"imports":["import { HasuraAdapter } from 'next-auth-hasura-adapter';","import type { HasuraAdapterOptions } from 'next-auth-hasura-adapter';","import NextAuth from 'next-auth';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"// pages/api/auth/[...nextauth].ts\nimport NextAuth from \"next-auth\";\nimport GitHubProvider from \"next-auth/providers/github\"; // Example provider\nimport { HasuraAdapter } from \"next-auth-hasura-adapter\";\n\n// Ensure your Hasura GraphQL endpoint and admin secret are set as environment variables.\n// Example: HASURA_GRAPHQL_ENDPOINT=https://your-hasura-instance/v1/graphql\n// Example: HASURA_GRAPHQL_ADMIN_SECRET=your_admin_secret_here\nconst HASURA_API_ENDPOINT = process.env.HASURA_GRAPHQL_ENDPOINT ?? '';\nconst HASURA_ADMIN_SECRET = process.env.HASURA_GRAPHQL_ADMIN_SECRET ?? '';\n\nif (!HASURA_API_ENDPOINT || !HASURA_ADMIN_SECRET) {\n  throw new Error(\"Missing Hasura GraphQL Endpoint or Admin Secret environment variables. Please set HASURA_GRAPHQL_ENDPOINT and HASURA_GRAPHQL_ADMIN_SECRET.\");\n}\n\nexport default NextAuth({\n  // Configure one or more authentication providers (e.g., GitHub)\n  providers: [\n    GitHubProvider({\n      clientId: process.env.GITHUB_ID ?? '', // Ensure GITHUB_ID is set in your .env\n      clientSecret: process.env.GITHUB_SECRET ?? '', // Ensure GITHUB_SECRET is set in your .env\n    }),\n    // Add more providers as needed (e.g., GoogleProvider, EmailProvider)\n  ],\n  // Use the Hasura Adapter to persist user data\n  adapter: HasuraAdapter({\n    endpoint: HASURA_API_ENDPOINT,\n    admin_secret: HASURA_ADMIN_SECRET,\n  }),\n  // Recommended for adapters like Hasura for stateless sessions\n  session: {\n    strategy: \"jwt\"\n  },\n  // Define callbacks to customize JWT and session objects\n  callbacks: {\n    async jwt({ token, user }) {\n      if (user) {\n        token.id = user.id; // Attach user ID to the JWT\n      }\n      // Custom Hasura claims can be added here if needed for authorization\n      return token;\n    },\n    async session({ session, token }) {\n      // Expose user ID to the client via the session object\n      if (session.user) {\n        session.user.id = token.id;\n      }\n      return session;\n    },\n  },\n  // NEXTAUTH_SECRET is required for any NextAuth.js application in production\n  secret: process.env.NEXTAUTH_SECRET ?? ''\n});\n","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}