Skip to content
Migrating from NextAuth.js v4? Read our migration guide.
API reference
@auth/edgedb-adapter

@auth/edgedb-adapter

Official Edge DB adapter for Auth.js / NextAuth.js.

Installation

npm install edgedb @auth/edgedb-adapter
npm install @edgedb/generate --save-dev

EdgeDBAdapter()

EdgeDBAdapter(client): Adapter

To use this Adapter, you need to install edgedb, @edgedb/generate, and the separate @auth/edgedb-adapter package:

npm install edgedb @auth/edgedb-adapter
npm install @edgedb/generate --save-dev

Installation

First, ensure you have the EdgeDB CLI installed.

Follow the instructions below, or read the EdgeDB quickstart to install the EdgeDB CLI and initialize a project

Linux or macOS

curl --proto '=https' --tlsv1.2 -sSf https://sh.edgedb.com | sh

Windows

iwr https://ps1.edgedb.com -useb | iex

Check that the CLI is available with the edgedb --version command. If you get a Command not found error, you may need to open a new terminal window before the edgedb command is available.

Once the CLI is installed, initialize a project from the application’s root directory. You’ll be presented with a series of prompts.

edgedb project init

This process will spin up an EdgeDB instance and “link” it with your current directory. As long as you’re inside that directory, CLI commands and client libraries will be able to connect to the linked instance automatically, without additional configuration.

Setup

NextAuth.js configuration

Configure your NextAuth.js to use the EdgeDB Adapter:

pages/api/auth/[...nextauth].js
import NextAuth from "next-auth"
import GoogleProvider from "next-auth/providers/google"
import { EdgeDBAdapter } from "@auth/edgedb-adapter"
import { createClient } from "edgedb"
 
const client = createClient()
 
export default NextAuth({
  adapter: EdgeDBAdapter(client),
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    }),
  ],
})

Create the EdgeDB schema

Replace the contents of the auto-generated file in dbschema/default.esdl with the following:

This schema is adapted for use in EdgeDB and based upon our main schema

default.esdl
module default {
    type User {
        property name -> str;
        required property email -> str {
            constraint exclusive;
        };
        property emailVerified -> datetime;
        property image -> str;
        multi link accounts := .<user[is Account];
        multi link sessions := .<user[is Session];
        property createdAt -> datetime {
            default := datetime_current();
        };
    }
 
    type Account {
       required property userId := .user.id;
       required property type -> str;
       required property provider -> str;
       required property providerAccountId -> str {
        constraint exclusive;
       };
       property refresh_token -> str;
       property access_token -> str;
       property expires_at -> int64;
       property token_type -> str;
       property scope -> str;
       property id_token -> str;
       property session_state -> str;
       required link user -> User {
            on target delete delete source;
       };
       property createdAt -> datetime {
            default := datetime_current();
        };
       constraint exclusive on ((.provider, .providerAccountId));
    }
 
    type Session {
        required property sessionToken -> str {
            constraint exclusive;
        };
        required property userId := .user.id;
        required property expires -> datetime;
        required link user -> User {
            on target delete delete source;
        };
        property createdAt -> datetime {
            default := datetime_current();
        };
    }
 
    type VerificationToken {
        required property identifier -> str;
        required property token -> str {
            constraint exclusive;
        };
        required property expires -> datetime;
        property createdAt -> datetime {
            default := datetime_current();
        };
 
        constraint exclusive on ((.identifier, .token));
    }
}
 
# Disable the application of access policies within access policies
# themselves. This behavior will become the default in EdgeDB 3.0.
# See: https://www.edgedb.com/docs/reference/ddl/access_policies#nonrecursive
 
using future nonrecursive_access_policies;

Migrate the database schema

  1. Create a migration
edgedb migration create
  1. Apply the migration
edgedb migrate

To learn more about EdgeDB migrations check out the Migrations docs.

Generate the query builder

npx @edgedb/generate edgeql-js

This will generate the query builder so that you can write fully typed EdgeQL queries with TypeScript in a code-first way.

const query = e.select(e.User, () => ({
  id: true,
  email: true,
  emailVerified: true,
  name: true,
  image: true,
  filter_single: { email: "johndoe@example.com" },
}));
 
return await query.run(client);

Deploying

Deploy EdgeDB

First deploy an EdgeDB instance on your preferred cloud provider:

Find your instance’s DSN

The DSN is also known as a connection string. It will have the format edgedb://username:password@hostname:port. The exact instructions for this depend on which cloud provider your’e deploying to.

Set an environment variable

EDGEDB_DSN="edgedb://johndoe:supersecure@myhost.com:420"

Apply migrations

Use the DSN to apply migrations against your remote instance.

edgedb migrate --dsn <your-instance-dsn>

Set up a prebuild script

Add the following prebuild script to your package.json. When your hosting provider initializes the build, it will trigger this script which will generate the query builder. The npx @edgedb/generate edgeql-js command will read the value of the EDGEDB_DSN environment variable, connect to the database, and generate the query builder before your hosting provider starts building the project.

Parameters

ParameterType
clientClient

Returns

Adapter

Auth.js © Balázs Orbán and Team - 2024