Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/JitPackJoyride/lucia-adapter-edgedb

Lucia adapter for EdgeDB
https://github.com/JitPackJoyride/lucia-adapter-edgedb

adapter edgedb lucia lucia-auth

Last synced: about 1 month ago
JSON representation

Lucia adapter for EdgeDB

Lists

README

        

# `@jitpackjoyride/lucia-adapter-edgedb`

[EdgeDB](https://www.edgedb.com/) adapter for Lucia v2.

**[Lucia documentation](https://lucia-auth.com)**

**[Changelog](https://github.com/JitPackJoyride/lucia-adapter-edgedb/blob/main/CHANGELOG.md)**

## Installation

```
bun add @jitpackjoyride/lucia-adapter-edgedb (recommended)
npm install @jitpackjoyride/lucia-adapter-edgedb
pnpm add @jitpackjoyride/lucia-adapter-edgedb
yarn add @jitpackjoyride/lucia-adapter-edgedb
```

## Usage

Do the usual EdgeDB setup, such as `edgedb project init`. Then, add this to your schema in `dbschema/default.esdl`:

```esdl
module default {
type User {
link auth_keys := . ({
...e.User["*"],
}));
type UserInDb = $infer[number];
type User = Omit;

const sessionSelectQuery = e.select(e.UserSession, () => ({
...e.UserSession["*"],
}));
type SessionInDb = $infer[number];
type Session = Omit;

///
declare namespace Lucia {
type Auth = import("./auth/lucia").Auth;
// NOTE: Keep this in sync with the database schema of User
type DatabaseUserAttributes = User;
// NOTE: Keep this in sync with the database schema of UserSession
type DatabaseSessionAttributes = Session;
}
```

When you're initialising the EdgeDB client, you need to do something like this:

```typescript
// src/edgedb.ts
import * as edgedb from "edgedb";

const client = edgedb.createClient().withConfig({
allow_user_specified_id: true,
});

export default client;
```

Note the `allow_user_specified_id` option. This is required for allowing the `id` field to be set by the user or by Lucia. Read the [Gotchas](#using-authsetuser-or-authsetsession) section for more information.

## Gotchas

### Using `auth.setUser` or `auth.setSession`

When calling either `auth.setUser` or `auth.setSession`, it is highly recommended to generate your own random uuid for the `id` field. You can do this with `uuidv4` from `uuid` or `crypto.randomUUID` from `crypto`.

Example:

```typescript
auth.setUser({
userId: crypto.randomUUID(),
// ... other fields
});
```

This is because Lucia's default id generator is random strings, but EdgeDB uses uuids. If you don't pass your own uuid, then the id will be a random string, which will make it hard to query the database.

## Testing

Not yet implemented.