-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add edgedb auth helper for svelte projects
- Loading branch information
Showing
4 changed files
with
844 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`; | ||
} | ||
} |
Oops, something went wrong.