Skip to main content

@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

Create a migration

edgedb migration create

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.

For example

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);

// Return type:
// {
// id: string;
// email: string;
// emailVerified: Date | null;
// image: string | null;
// name: string | null;
// } | null

Deploying

Deploy EdgeDB

First deploy an EdgeDB instance on your preferred cloud provider:

AWS

Google Cloud

Azure

DigitalOcean

Fly.io

Docker (cloud-agnostic)

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 you are deploying to.

Set an environment variable

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

Update the client

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()
+ const client = createClient({ dsn: process.env.EDGEDB_DSN })

export default NextAuth({
adapter: EdgeDBAdapter(client),
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
],
})

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.

package.json
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
+ "prebuild": "npx @edgedb/generate edgeql-js"
},

Parameters

client: Client

Returns

Adapter