Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Node.js example using EdgeDB Auth #112

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions nodejs-auth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
This is a sample project demonstrating how to use [EdgeDB
Auth](https://www.edgedb.com/docs/guides/auth/index) from a vanilla NodeJS HTTP server. It serves as
a blueprint for building all of the low-level pieces of interacting with the EdgeDB authentication
server.

Please see the [EdgeDB Auth guide](https://www.edgedb.com/docs/guides/auth/index) for more
information on how these different piece fit into the various available authentication flows.

## Getting Started

### Requirements

- Node.js v20
- npm v10
- EdgeDB CLI v4

#### Nix

You can use the included Nix flake to manage the binary dependencies of this project (Node, npm, and
the EdgeDB CLI). It also includes `httpie` which can be useful for testing endpoints.

### Install dependencies

```bash
npm install
```

### Initialize project

```bash
edgedb project init
```

### Configure Auth Extension

Follow the configuration guidance in [the
docs](https://www.edgedb.com/docs/guides/auth/index#extension-configuration). You can launch the
built-in UI using the EdgeDB CLI:

```bash
edgedb ui
```

### Start server

```bash
node index.js
```
29 changes: 29 additions & 0 deletions nodejs-auth/dbschema/default.esdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using extension auth;

module default {
global current_user := (
assert_single((
select User
filter .identity =
global ext::auth::ClientTokenIdentity
))
);

type User {
name: str;
required identity: ext::auth::Identity;
}

type Post {
required content: str;
required author: User;
access policy author_full_access
allow all
using (
.author ?= global current_user
);

access policy others_read_only
allow select;
}
}
23 changes: 23 additions & 0 deletions nodejs-auth/dbschema/migrations/00001.edgeql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
CREATE MIGRATION m13bcrrwlp5jdlmorad6tuv3uubb5jkbgd5f6dbhehhsbrw7jardvq
ONTO initial
{
CREATE EXTENSION pgcrypto VERSION '1.3';
CREATE EXTENSION auth VERSION '1.0';
CREATE TYPE default::User {
CREATE REQUIRED LINK identity: ext::auth::Identity;
CREATE PROPERTY name: std::str;
};
CREATE GLOBAL default::current_user := (std::assert_single((SELECT
default::User
FILTER
(.identity = GLOBAL ext::auth::ClientTokenIdentity)
)));
CREATE TYPE default::Post {
CREATE REQUIRED LINK author: default::User;
CREATE ACCESS POLICY author_full_access
ALLOW ALL USING ((.author ?= GLOBAL default::current_user));
CREATE ACCESS POLICY others_read_only
ALLOW SELECT ;
CREATE REQUIRED PROPERTY content: std::str;
};
};
2 changes: 2 additions & 0 deletions nodejs-auth/edgedb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[edgedb]
server-version = "4.x"
61 changes: 61 additions & 0 deletions nodejs-auth/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions nodejs-auth/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
description = "A Nix-flake-based Node.js development environment";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/master";
flake-utils.url = "github:numtide/flake-utils";
};

outputs = {
self,
nixpkgs,
flake-utils,
}:
flake-utils.lib.eachDefaultSystem (system: let
pkgs = import nixpkgs {inherit system;};
edgedb-dev = pkgs.edgedb.overrideAttrs (oldAttrs: rec {
version = "4.0.0-alpha.1+ddfbe70";
src = pkgs.fetchFromGitHub {
owner = "edgedb";
repo = "edgedb-cli";
rev = "243f510575f7d3fce8d1964c964dd5fdbf510299";
sha256 = "sha256-1G7Ci7iI8IbgWRUDPBhW9hVDR6l/VbrLJEkKzyQpIkw=";
fetchSubmodules = true;
};
cargoDeps = pkgs.rustPlatform.importCargoLock {
lockFile = src + "/Cargo.lock";
outputHashes = {
"edgedb-derive-0.5.1" = "sha256-9NhfmtuZcDG+ouDeUKM8HpboJYU8rT8Own5M13PrDU8=";
"edgeql-parser-0.1.0" = "sha256-c5xBuW47xXgy8VLR/P7DvVhLBd0rvI6P9w82IPPsTwo=";
"rexpect-0.5.0" = "sha256-vstAL/fJWWx7WbmRxNItKpzvgGF3SvJDs5isq9ym/OA=";
"rustyline-8.0.0" = "sha256-CrICwQbHPzS4QdVIEHxt2euX+g+0pFYe84NfMp1daEc=";
"serde_str-1.0.0" = "sha256-CMBh5lxdQb2085y0jc/DrV6B8iiXvVO2aoZH/lFFjak=";
"indexmap-2.0.0-pre" = "sha256-QMOmoUHE1F/sp+NeDpgRGqqacWLHWG02YgZc5vAdXZY=";
};
};
});
in {
devShell = pkgs.mkShell {
packages = [
(pkgs.nodejs_20.override {enableNpm = false;})
pkgs.nodePackages.npm
edgedb-dev
pkgs.httpie
];

shellHook = ''
echo "`${edgedb-dev}/bin/edgedb --version`"
echo "npm `${pkgs.nodePackages.npm}/bin/npm --version`"
echo "node `${pkgs.nodejs_20}/bin/node --version`"
'';
};
});
}
Loading