Skip to content

Commit

Permalink
Add edgedb auth helper for svelte projects
Browse files Browse the repository at this point in the history
  • Loading branch information
diksipav committed Jan 22, 2024
1 parent c991a82 commit 6277bb7
Show file tree
Hide file tree
Showing 4 changed files with 844 additions and 0 deletions.
36 changes: 36 additions & 0 deletions packages/auth-svelte/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@edgedb/auth-svelte",
"description": "Helper library to integrate the EdgeDB Auth extension with Svelte.",
"version": "0.1.0-alpha.1",
"author": "EdgeDB <[email protected]>",
"type": "module",
"repository": {
"type": "git",
"url": "https://github.com/edgedb/edgedb-js.git",
"directory": "packages/auth-svelte"
},
"license": "Apache-2.0",
"sideEffects": false,
"files": [
"/dist"
],
"scripts": {
"build": "tsc --project tsconfig.json"
},
"devDependencies": {
"@types/node": "^20.8.4",
"edgedb": "^1.3.6",
"typescript": "^5.2.2"
},
"peerDependencies": {
"edgedb": "^1.3.6"
},
"dependencies": {
"@edgedb/auth-core": "^0.1.0-beta.1",
"@sveltejs/kit": "^2.3.2",
"cookie": "^0.6.0"
},
"exports": {
"./*": "./dist/*.js"
}
}
56 changes: 56 additions & 0 deletions packages/auth-svelte/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import type { BuiltinOAuthProviderNames } from "@edgedb/auth-core";

export interface SvelteAuthOptions {
baseUrl: string;
authRoutesPath?: string;
authCookieName?: string;
pkceVerifierCookieName?: string;
passwordResetPath?: string;
}

type OptionalOptions = "passwordResetPath";

export default function createClientAuth(options: SvelteAuthOptions) {
return new SvelteClientAuth(options);
}

export class SvelteClientAuth {
protected readonly options: Required<
Omit<SvelteAuthOptions, OptionalOptions>
> &
Pick<SvelteAuthOptions, OptionalOptions>;

/** @internal */
constructor(options: SvelteAuthOptions) {
this.options = {
baseUrl: options.baseUrl.replace(/\/$/, ""),
authRoutesPath: options.authRoutesPath?.replace(/^\/|\/$/g, "") ?? "auth",
authCookieName: options.authCookieName ?? "edgedb-session",
pkceVerifierCookieName:
options.pkceVerifierCookieName ?? "edgedb-pkce-verifier",
passwordResetPath: options.passwordResetPath,
};
}

protected get _authRoute() {
return `${this.options.baseUrl}/${this.options.authRoutesPath}`;
}

getOAuthUrl(providerName: BuiltinOAuthProviderNames) {
return `${this._authRoute}/oauth?${new URLSearchParams({
provider_name: providerName,
}).toString()}`;
}

getBuiltinUIUrl() {
return `${this._authRoute}/builtin/signin`;
}

getBuiltinUISignUpUrl() {
return `${this._authRoute}/builtin/signup`;
}

getSignoutUrl() {
return `${this._authRoute}/signout`;
}
}
Loading

0 comments on commit 6277bb7

Please sign in to comment.