2023-03-16 15:50:39 +01:00
|
|
|
<script lang="ts">
|
|
|
|
import { onMount } from "svelte";
|
2023-03-18 16:33:12 +01:00
|
|
|
import { Alert, Button, FormGroup, Icon, Input } from "sveltestrap";
|
2023-03-16 15:50:39 +01:00
|
|
|
|
|
|
|
import { goto } from "$app/navigation";
|
|
|
|
import type { APIError, MeUser } from "$lib/api/entities";
|
2023-03-18 15:19:53 +01:00
|
|
|
import { apiFetch, apiFetchClient } from "$lib/api/fetch";
|
2023-03-16 15:50:39 +01:00
|
|
|
import { userStore } from "$lib/store";
|
|
|
|
import type { PageData } from "./$types";
|
|
|
|
import ErrorAlert from "$lib/components/ErrorAlert.svelte";
|
2023-03-18 15:19:53 +01:00
|
|
|
import { addToast } from "$lib/toast";
|
2023-03-16 15:50:39 +01:00
|
|
|
|
|
|
|
interface SignupResponse {
|
|
|
|
user: MeUser;
|
|
|
|
token: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export let data: PageData;
|
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
if (!data.is_deleted && data.token && data.user) {
|
|
|
|
localStorage.setItem("pronouns-token", data.token);
|
|
|
|
localStorage.setItem("pronouns-user", JSON.stringify(data.user));
|
|
|
|
userStore.set(data.user);
|
|
|
|
goto("/");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let username = "";
|
|
|
|
let invite = "";
|
|
|
|
|
|
|
|
const signupForm = async () => {
|
|
|
|
try {
|
|
|
|
const resp = await apiFetch<SignupResponse>("/auth/mastodon/signup", {
|
|
|
|
method: "POST",
|
|
|
|
body: {
|
|
|
|
instance: data.instance,
|
|
|
|
ticket: data.ticket,
|
|
|
|
username: username,
|
|
|
|
invite_code: invite,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
localStorage.setItem("pronouns-token", resp.token);
|
|
|
|
localStorage.setItem("pronouns-user", JSON.stringify(resp.user));
|
|
|
|
userStore.set(resp.user);
|
|
|
|
goto("/");
|
|
|
|
} catch (e) {
|
|
|
|
data.error = e as APIError;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let deleteCancelled = false;
|
|
|
|
let deleteError: APIError | null = null;
|
|
|
|
const cancelDelete = async () => {
|
|
|
|
try {
|
|
|
|
await apiFetch<any>("/auth/cancel-delete", {
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"X-Delete-Token": data.token!,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
deleteCancelled = true;
|
|
|
|
deleteError = null;
|
|
|
|
} catch (e) {
|
|
|
|
deleteCancelled = false;
|
|
|
|
deleteError = e as APIError;
|
|
|
|
}
|
|
|
|
};
|
2023-03-18 15:19:53 +01:00
|
|
|
|
|
|
|
const linkAccount = async () => {
|
|
|
|
try {
|
|
|
|
const resp = await apiFetchClient<MeUser>("/auth/mastodon/add-provider", "POST", {
|
|
|
|
instance: data.instance,
|
|
|
|
ticket: data.ticket,
|
|
|
|
});
|
|
|
|
|
|
|
|
localStorage.setItem("pronouns-user", JSON.stringify(resp));
|
|
|
|
userStore.set(resp);
|
|
|
|
addToast({ header: "Linked account", body: "Successfully linked account!" });
|
2023-03-18 16:33:12 +01:00
|
|
|
await goto("/settings/auth");
|
2023-03-18 15:19:53 +01:00
|
|
|
} catch (e) {
|
|
|
|
data.error = e as APIError;
|
|
|
|
}
|
|
|
|
};
|
2023-03-16 15:50:39 +01:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<svelte:head>
|
2023-03-16 17:01:21 +01:00
|
|
|
<title>Log in with the Fediverse - pronouns.cc</title>
|
2023-03-16 15:50:39 +01:00
|
|
|
</svelte:head>
|
|
|
|
|
2023-03-16 17:01:21 +01:00
|
|
|
<h1>Log in with the Fediverse</h1>
|
2023-03-16 15:50:39 +01:00
|
|
|
|
|
|
|
{#if data.error}
|
|
|
|
<ErrorAlert error={data.error} />
|
|
|
|
{/if}
|
2023-03-18 15:19:53 +01:00
|
|
|
{#if data.ticket && $userStore}
|
|
|
|
<div>
|
2023-03-18 16:33:12 +01:00
|
|
|
<FormGroup floating label="Fediverse username">
|
|
|
|
<Input readonly value="{data.fediverse}@{data.instance}" />
|
|
|
|
</FormGroup>
|
2023-03-18 15:19:53 +01:00
|
|
|
</div>
|
2023-03-18 16:33:12 +01:00
|
|
|
<div class="my-2">
|
|
|
|
<FormGroup floating label="pronouns.cc username">
|
|
|
|
<Input readonly value={$userStore.name} />
|
|
|
|
</FormGroup>
|
2023-03-18 15:19:53 +01:00
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<Button on:click={linkAccount}>Link account</Button>
|
|
|
|
<Button color="secondary" href="/settings/auth">Cancel</Button>
|
|
|
|
</div>
|
|
|
|
{:else if data.ticket}
|
2023-03-16 15:50:39 +01:00
|
|
|
<form on:submit|preventDefault={signupForm}>
|
|
|
|
<div>
|
2023-03-18 16:33:12 +01:00
|
|
|
<FormGroup floating label="Fediverse username">
|
|
|
|
<Input readonly value="{data.fediverse}@{data.instance}" />
|
|
|
|
</FormGroup>
|
2023-03-16 15:50:39 +01:00
|
|
|
</div>
|
|
|
|
<div>
|
2023-03-18 16:33:12 +01:00
|
|
|
<FormGroup floating label="Username">
|
|
|
|
<Input id="username" name="username" bind:value={username} />
|
|
|
|
</FormGroup>
|
2023-03-16 15:50:39 +01:00
|
|
|
</div>
|
|
|
|
{#if data.require_invite}
|
|
|
|
<div>
|
2023-03-18 16:33:12 +01:00
|
|
|
<FormGroup floating label="Invite code">
|
|
|
|
<Input id="invite" name="invite" aria-describedby="invite-help" bind:value={invite} />
|
|
|
|
</FormGroup>
|
2023-03-16 15:50:39 +01:00
|
|
|
<div id="invite-help" class="form-text">
|
|
|
|
<Icon name="info-circle-fill" /> You currently need an invite code to sign up. You can get
|
|
|
|
one from an existing user.
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{/if}
|
2023-03-18 16:33:12 +01:00
|
|
|
<div class="form-text mb-1">
|
2023-03-16 15:50:39 +01:00
|
|
|
By signing up, you agree to the <a href="/page/tos">terms of service</a> and the
|
|
|
|
<a href="/page/privacy">privacy policy</a>.
|
|
|
|
</div>
|
|
|
|
<Button type="submit" color="primary">Sign up</Button>
|
|
|
|
</form>
|
|
|
|
{:else if data.is_deleted && data.token}
|
|
|
|
<p>Your account is pending deletion since {data.deleted_at}.</p>
|
|
|
|
<p>If you wish to cancel deletion, press the button below.</p>
|
|
|
|
<p>
|
|
|
|
<Button color="primary" on:click={cancelDelete} disabled={deleteCancelled}
|
|
|
|
>Cancel account deletion</Button
|
|
|
|
>
|
|
|
|
</p>
|
|
|
|
{#if deleteCancelled}
|
|
|
|
<Alert color="secondary" fade={false}>
|
|
|
|
Account deletion cancelled! You can now <a href="/auth/login">log in</a> again.
|
|
|
|
</Alert>
|
|
|
|
{/if}
|
|
|
|
{#if deleteError}
|
|
|
|
<ErrorAlert error={deleteError} />
|
|
|
|
{/if}
|
|
|
|
{:else}
|
|
|
|
Loading...
|
|
|
|
{/if}
|