Hasura Adapter - NextAuth.js
This directly uses code from Jason Brown's next-auth-hasura-adapter
Overview
This is the Hasura Adapter for next-auth
. This package can only be used in conjunction with the primary next-auth
package. It is not a standalone package.
This is the migration to set up the Hasura database:
CREATE EXTENSION IF NOT EXISTS pgcrypto;
SET check_function_bodies = false;
CREATE TABLE public.accounts (
id uuid DEFAULT public.gen_random_uuid() NOT NULL,
type text NOT NULL,
provider text NOT NULL,
"providerAccountId" text NOT NULL,
refresh_token text,
access_token text,
expires_at bigint,
token_type text,
scope text,
id_token text,
session_state text,
oauth_token_secret text,
oauth_token text,
"userId" uuid NOT NULL,
refresh_token_expires_in integer
);
CREATE TABLE public.sessions (
id uuid DEFAULT public.gen_random_uuid() NOT NULL,
"sessionToken" text NOT NULL,
"userId" uuid NOT NULL,
expires timestamp with time zone
);
CREATE TABLE public.users (
id uuid DEFAULT public.gen_random_uuid() NOT NULL,
name text,
email text,
"emailVerified" timestamp with time zone,
image text
);
CREATE TABLE public.verification_tokens (
token text NOT NULL,
identifier text NOT NULL,
expires timestamp with time zone
);
ALTER TABLE ONLY public.accounts
ADD CONSTRAINT accounts_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.sessions
ADD CONSTRAINT sessions_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.users
ADD CONSTRAINT users_email_key UNIQUE (email);
ALTER TABLE ONLY public.users
ADD CONSTRAINT users_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.verification_tokens
ADD CONSTRAINT verification_tokens_pkey PRIMARY KEY (token);
ALTER TABLE ONLY public.accounts
ADD CONSTRAINT "accounts_userId_fkey" FOREIGN KEY ("userId") REFERENCES public.users(id) ON UPDATE RESTRICT ON DELETE CASCADE;
ALTER TABLE ONLY public.sessions
ADD CONSTRAINT "sessions_userId_fkey" FOREIGN KEY ("userId") REFERENCES public.users(id) ON UPDATE RESTRICT ON DELETE CASCADE;
Getting Started
- Add this adapter to your
pages/api/[...nextauth].js
next-auth configuration object.
import NextAuth from "next-auth"
import {HasuraAdapter} from 'utils/next-auth/adapters/hasura-adapter'
// For more information on each option (and a full list of options) go to
// https://next-auth.js.org/configuration/options
export default NextAuth({
// https://next-auth.js.org/configuration/providers
providers: [],
adapter: HasuraAdapter({
endpoint: process.env.HASURA_GRAPHQL_ENDPOINT,
adminSecret: process.env.HASURA_ADMIN_SECRET,
})
})