Implement OAuth callbacks on new frontend
Diff
chartered-web/config-example.toml | 2 +-
book/src/getting-started/installation.md | 2 +-
book/src/guide/config-reference.md | 2 +-
chartered-frontend/src/stores/auth.ts | 25 +++++++++++++++++++++++++
chartered-frontend/src/routes/(unauthed)/login/oauth/+page.svelte | 19 +++++++++++++++++++
5 files changed, 47 insertions(+), 3 deletions(-)
@@ -1,7 +1,7 @@
bind_address = "127.0.0.1:8888"
database_uri = "sqlite:///tmp/chartered.db"
storage_uri = "file:///tmp/chartered"
frontend_base_uri = "http://localhost:1234/"
frontend_base_uri = "http://localhost:5173/"
encryption_key = "thisisanexamplekeydontuseme4prod"
[auth.password]
@@ -34,7 +34,7 @@
bind_address = "127.0.0.1:8080"
database_uri = "postgres://user:password@localhost/chartered"
storage_uri = "s3://s3-eu-west-1.amazonaws.com/my-cool-crate-store/"
frontend_base_uri = "http://localhost:1234/"
frontend_base_uri = "http://localhost:5173/"
[auth.password]
enabled = true
@@ -80,7 +80,7 @@
storage_uri = "s3://s3-eu-west-1.amazonaws.com/my-cool-crate-store/" # or file:///var/lib/chartered
frontend_base_uri = "http://localhost:1234/"
frontend_base_uri = "http://localhost:5173/"
[auth.password]
enabled = true # enables password auth
@@ -140,6 +140,31 @@
}
/**
* Attempt to log the user in using the OAuth callback throwing an error if an error occurred.
*
* @param params URL search parameters
*/
export async function handleOAuthCallback(params: string) {
const result = await fetch(`${BASE_URL}/a/-/web/v1/auth/login/oauth/complete${params}`);
const json: LoginResult = await result.json();
if (json.error) {
throw new Error(json.error);
}
auth.set({
auth_key: json.key,
expires: Date.parse(json.expires),
picture_url: json.picture_url,
uuid: json.user_uuid,
});
}
/**
* Successful response type of /web/v1/auth/login/oauth/[provider]/begin, contains the URL
* the user needs to visit to complete the OAuth flow.
*/
@@ -1,0 +1,19 @@
<script type="typescript">
import { page } from '$app/stores';
import { handleOAuthCallback } from '../../../../stores/auth';
import Spinner from '../../../../components/Spinner.svelte';
import ErrorAlert from '../../../../components/ErrorAlert.svelte';
const callback = handleOAuthCallback($page.url.search);
</script>
<div class="h-[18rem]">
{#await callback}
<Spinner />
{:then x}
<Spinner />
{:catch error}
<ErrorAlert showClose={false}>{error}</ErrorAlert>
{/await}
</div>