Skip to content

Commit

Permalink
auth-svelte: rename edgedb to gel
Browse files Browse the repository at this point in the history
  • Loading branch information
diksipav committed Dec 11, 2024
1 parent 90389b0 commit df51d85
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 36 deletions.
14 changes: 7 additions & 7 deletions packages/auth-sveltekit/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@edgedb/auth-sveltekit",
"description": "Helper library to integrate the EdgeDB Auth extension with Sveltekit.",
"name": "@gel/auth-sveltekit",
"description": "Helper library to integrate the Gel Auth extension with Sveltekit.",
"version": "0.2.3",
"author": "EdgeDB <info@edgedb.com>",
"author": "Gel <info@gel.com>",
"type": "module",
"repository": {
"type": "git",
"url": "https://github.com/edgedb/edgedb-js.git",
"url": "https://github.com/gel/gel-js.git",
"directory": "packages/auth-sveltekit"
},
"license": "Apache-2.0",
Expand All @@ -24,16 +24,16 @@
"@repo/tsconfig": "*",
"@sveltejs/kit": "^2.5.10",
"@types/node": "^20.12.13",
"edgedb": "*",
"gel": "*",
"svelte": "^4.2.17",
"typescript": "^5.5.2",
"vite": "^5.2.12"
},
"peerDependencies": {
"edgedb": "^1.3.6"
"gel": "^1.3.6"
},
"dependencies": {
"@edgedb/auth-core": "^0.2.1"
"@gel/auth-core": "^0.2.1"
},
"exports": {
"./*": "./dist/*.js"
Expand Down
37 changes: 17 additions & 20 deletions packages/auth-sveltekit/readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @edgedb/auth-sveltekit
# @gel/auth-sveltekit

This library provides a set of utilities to help you integrate authentication into your [SvelteKit](https://kit.svelte.dev/) application.
It supports authentication with various OAuth providers, as well as email/password authentication.
Expand All @@ -8,15 +8,16 @@ It supports authentication with various OAuth providers, as well as email/passwo
This package is published on npm and can be installed with your package manager of choice.

```bash
npm install @edgedb/auth-sveltekit
npm install @gel/auth-sveltekit
```

## Setup

**Prerequisites**:

- Node v18+
- **Note**: Due to using the `crypto` global, you will need to start Node with `--experimental-global-webcrypto`. You can add this option to your `NODE_OPTIONS` environment variable, like `NODE_OPTIONS='--experimental-global-webcrypto'` in the appropriate `.env` file.
- Before adding EdgeDB auth to your SvelteKit app, you will first need to enable the `auth` extension in your EdgeDB schema, and have configured the extension with some providers (you can do this in CLI or EdgeDB UI). Refer to the auth extension docs for details on how to do this.
- Before adding Gel auth to your SvelteKit app, you will first need to enable the `auth` extension in your Gel schema, and have configured the extension with some providers (you can do this in CLI or Gel UI). Refer to the auth extension docs for details on how to do this.

### Client auth helper

Expand All @@ -25,9 +26,7 @@ Initialize the client auth helper by passing configuration options to `createCli
```ts
// src/lib/auth.ts

import createClientAuth, {
type AuthOptions,
} from "@edgedb/auth-sveltekit/client";
import createClientAuth, { type AuthOptions } from "@gel/auth-sveltekit/client";

export const options: AuthOptions = {
baseUrl: "http://localhost:5173",
Expand All @@ -43,18 +42,18 @@ The available auth config options are as follows:

- `baseUrl: string`, _required_, The url of your application; needed for various auth flows (eg. OAuth) to redirect back to.
- `authRoutesPath?: string`, The path to the auth route handlers, defaults to `'auth'`, see below for more details.
- `authCookieName?: string`, The name of the cookie where the auth token will be stored, defaults to `'edgedb-session'`.
- `pkceVerifierCookieName?: string`: The name of the cookie where the verifier for the PKCE flow will be stored, defaults to `'edgedb-pkce-verifier'`
- `authCookieName?: string`, The name of the cookie where the auth token will be stored, defaults to `'gel-session'`.
- `pkceVerifierCookieName?: string`: The name of the cookie where the verifier for the PKCE flow will be stored, defaults to `'gel-pkce-verifier'`
- `passwordResetUrl?: string`: The url of the the password reset page; needed if you want to enable password reset emails in your app.
&nbsp;

### EdgeDB client
### Gel client

Lets create an EdgeDB client that you will need for creating server auth client.
Lets create an Gel client that you will need for creating server auth client.

```ts
// src/lib/server/auth.ts
import { createClient } from "edgedb";
import { createClient } from "gel";

export const client = createClient({
//Note: when developing locally you will need to set tls security to insecure, because the dev server uses self-signed certificates which will cause api calls with the fetch api to fail.
Expand All @@ -64,13 +63,11 @@ export const client = createClient({

### Server auth client

Create the server auth client in a handle hook. Firstly call `serverAuth` passing to it EdgeDB client you created in the previous step, along with configuration options from step 1. This will give you back the `createServerRequestAuth` and `createAuthRouteHook`. You should call `createServerRequestAuth` inside a handle to attach the server client to `event.locals`. Calling `createAuthRouteHook` will give you back a hook so you should just invoke it inside `sequence` and pass your auth route handlers to it.
Create the server auth client in a handle hook. Firstly call `serverAuth` passing to it Gel client you created in the previous step, along with configuration options from step 1. This will give you back the `createServerRequestAuth` and `createAuthRouteHook`. You should call `createServerRequestAuth` inside a handle to attach the server client to `event.locals`. Calling `createAuthRouteHook` will give you back a hook so you should just invoke it inside `sequence` and pass your auth route handlers to it.
You can now access the server auth in all actions and load functions through `event.locals`.

```ts
import serverAuth, {
type AuthRouteHandlers,
} from "@edgedb/auth-sveltekit/server";
import serverAuth, { type AuthRouteHandlers } from "@gel/auth-sveltekit/server";
import { redirect, type Handle } from "@sveltejs/kit";
import { sequence } from "@sveltejs/kit/hooks";
import { client } from "$lib/server/auth";
Expand All @@ -79,7 +76,7 @@ import { options } from "$lib/auth";

const { createServerRequestAuth, createAuthRouteHook } = serverAuth(
client,
options
options,
);

const createServerAuthClient: Handle = ({ event, resolve }) => {
Expand All @@ -100,14 +97,14 @@ const authRouteHandlers: AuthRouteHandlers = {

export const handle = sequence(
createServerAuthClient,
createAuthRouteHook(authRouteHandlers)
createAuthRouteHook(authRouteHandlers),
);
```

\* If you use typescript you need to update `Locals` type with `auth` so that auth is correctly recognized throughout the project:

```ts
import type { ServerRequestAuth } from "@edgedb/auth-sveltekit/server";
import type { ServerRequestAuth } from "@gel/auth-sveltekit/server";

declare global {
namespace App {
Expand Down Expand Up @@ -135,7 +132,7 @@ In any of them you can define what to do in case of success or error. Every hand

### UI

Now we just need to setup the UI to allow your users to sign in/up, etc. The easiest way to get started is to use the EdgeDB Auth's builtin UI. Or alternatively you can implement your own custom UI.
Now we just need to setup the UI to allow your users to sign in/up, etc. The easiest way to get started is to use the Gel Auth's builtin UI. Or alternatively you can implement your own custom UI.

**Builtin UI**

Expand All @@ -154,7 +151,7 @@ To help with implementing your own custom auth UI, the `auth` object has a numbe
- `emailPasswordResetPassword(data: { reset_token: string; password: string } | FormData)`
- `signout()`
- `isPasswordResetTokenValid(resetToken: string)`: Checks if a password reset token is still valid.
- `getOAuthUrl(providerName: string)`: This method takes the name of an OAuth provider (make sure you configure providers you need in the auth ext config first using CLI or EdgeDB UI) and returns a link that will initiate the OAuth sign in flow for that provider. You will also need to configure the `onOAuthCallback` auth route handler.
- `getOAuthUrl(providerName: string)`: This method takes the name of an OAuth provider (make sure you configure providers you need in the auth ext config first using CLI or Gel UI) and returns a link that will initiate the OAuth sign in flow for that provider. You will also need to configure the `onOAuthCallback` auth route handler.

## Usage

Expand Down
8 changes: 4 additions & 4 deletions packages/auth-sveltekit/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { BuiltinOAuthProviderNames } from "@edgedb/auth-core";
import { WebAuthnClient } from "@edgedb/auth-core/webauthn";
import type { BuiltinOAuthProviderNames } from "@gel/auth-core";
import { WebAuthnClient } from "@gel/auth-core/webauthn";

export interface AuthOptions {
baseUrl: string;
Expand All @@ -23,9 +23,9 @@ export function getConfig(options: AuthOptions) {
return {
baseUrl,
authRoutesPath,
authCookieName: options.authCookieName ?? "edgedb-session",
authCookieName: options.authCookieName ?? "gel-session",
pkceVerifierCookieName:
options.pkceVerifierCookieName ?? "edgedb-pkce-verifier",
options.pkceVerifierCookieName ?? "gel-pkce-verifier",
passwordResetPath: options.passwordResetPath,
magicLinkFailurePath: options.magicLinkFailurePath,
authRoute: `${baseUrl}/${authRoutesPath}`,
Expand Down
10 changes: 5 additions & 5 deletions packages/auth-sveltekit/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
type RequestEvent,
type Handle,
} from "@sveltejs/kit";
import type { Client } from "edgedb";
import type { Client } from "gel";
import {
Auth,
builtinOAuthProviderNames,
Expand All @@ -16,19 +16,19 @@ import {
PKCEError,
InvalidDataError,
OAuthProviderFailureError,
EdgeDBAuthError,
GelAuthError,
MagicLinkFailureError,
type AuthenticationResponseJSON,
type RegistrationResponseJSON,
} from "@edgedb/auth-core";
} from "@gel/auth-core";
import {
ClientAuth,
getConfig,
type AuthConfig,
type AuthOptions,
} from "./client.js";

export * from "@edgedb/auth-core/errors";
export * from "@gel/auth-core/errors";
export type { TokenData, AuthOptions, Client };

export type BuiltinProviderNames =
Expand Down Expand Up @@ -574,7 +574,7 @@ async function handleAuthRoutes(
if (error) {
const desc = searchParams.get("error_description");
return onBuiltinUICallback({
error: new EdgeDBAuthError(error + (desc ? `: ${desc}` : "")),
error: new GelAuthError(error + (desc ? `: ${desc}` : "")),
});
}
const code = searchParams.get("code");
Expand Down

0 comments on commit df51d85

Please sign in to comment.