ffeat: add basic signups

This commit is contained in:
Xyphyn committed 2023-08-18 16:42:40 -07:00
1 parent dcf1fba1df
commit b785ff9ed8
5 files changed
+267

No files matched your search

+5
View File
@@ -2,6 +2,7 @@
import { goto } from '$app/navigation'
import { setUser } from '$lib/auth.js'
import Button from '$lib/components/input/Button.svelte'
import Link from '$lib/components/input/Link.svelte'
import TextInput from '$lib/components/input/TextInput.svelte'
import Card from '$lib/components/ui/Card.svelte'
import { toast } from '$lib/components/ui/toasts/toasts.js'
@@ -118,5 +119,9 @@
>
Log in
</Button>
<hr class="dark:border-zinc-700" />
<p class="text-sm text-center opacity-80">
Don't have an account? <Link href="/signup" highlight>Sign up</Link>
</p>
</form>
</Card>
+58
View File
@@ -0,0 +1,58 @@
<script lang="ts">
import { goto } from '$app/navigation'
import Button from '$lib/components/input/Button.svelte'
import TextInput from '$lib/components/input/TextInput.svelte'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import { DEFAULT_INSTANCE_URL } from '$lib/instance.js'
import { validateInstance } from '$lib/lemmy.js'
let instance: string = ''
let validating: boolean = false
</script>
<svelte:head>
<title>Sign up | Choose Instance</title>
</svelte:head>
<div class="mx-auto max-w-xl flex flex-col gap-4">
<h1 class="font-bold text-2xl">Sign Up</h1>
<h2 class="font-bold text-xl">Choose an instance</h2>
<form
class="flex flex-col gap-4"
on:submit|preventDefault={async () => {
if (instance != '') {
validating = true
if (await validateInstance(instance.trim())) {
goto(`/signup/${instance}`)
} else {
toast({
content: 'Could not contact that instance URL',
type: 'error',
})
}
validating = false
}
}}
>
<TextInput
bind:value={instance}
label="Instance URL"
required
placeholder={DEFAULT_INSTANCE_URL}
on:input={() => {
instance = instance.toLowerCase().replaceAll(' ', '')
}}
/>
<Button
submit
color="primary"
size="lg"
loading={validating}
disabled={validating}
>
Go
</Button>
</form>
</div>
+8
View File
@@ -0,0 +1,8 @@
import { LINKED_INSTANCE_URL } from "$lib/instance.js";
import { redirect } from "@sveltejs/kit";
export const load = () => {
if (LINKED_INSTANCE_URL) {
throw redirect(300, `/signup/${LINKED_INSTANCE_URL}`)
}
}
+189
View File
@@ -0,0 +1,189 @@
<script lang="ts">
import { goto } from '$app/navigation'
import { page } from '$app/stores'
import { setUser } from '$lib/auth.js'
import Button from '$lib/components/input/Button.svelte'
import TextArea from '$lib/components/input/TextArea.svelte'
import TextInput from '$lib/components/input/TextInput.svelte'
import Markdown from '$lib/components/markdown/Markdown.svelte'
import Avatar from '$lib/components/ui/Avatar.svelte'
import Card from '$lib/components/ui/Card.svelte'
import Spinner from '$lib/components/ui/loader/Spinner.svelte'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import { getClient } from '$lib/lemmy.js'
import type { GetCaptchaResponse } from 'lemmy-js-client'
import {
ExclamationTriangle,
Icon,
QuestionMarkCircle,
} from 'svelte-hero-icons'
export let data
let captchaRequired = data.site_view.local_site.captcha_enabled
let email: string | undefined = ''
$: if (email == '') email = undefined
let username = '',
password = '',
passwordVerify = '',
captcha: GetCaptchaResponse | null = null,
verifyCaptcha: string | undefined = undefined,
application: string | undefined = undefined,
submitting: boolean = false,
honeypot: string | undefined = undefined
const getCaptcha = async () => (captcha = await getClient().getCaptcha())
$: captchaAudio = captcha?.ok?.wav
? `data:audio/wav;base64,${captcha.ok.wav}`
: ''
async function submit() {
submitting = true
try {
const res = await getClient().register({
username: username,
email: email,
password: password,
password_verify: passwordVerify,
show_nsfw: true,
answer: application,
captcha_answer: verifyCaptcha,
captcha_uuid: captcha?.ok?.uuid,
honeypot,
})
toast({
content: `Signed up.${
res.verify_email_sent ? ' A verification email was sent.' : ''
}`,
type: 'success',
})
if (res?.jwt) {
await setUser(res.jwt, $page.params.instance, username)
toast({ content: 'Successfully logged in.', type: 'success' })
goto('/')
} else {
throw new Error('Invalid credentials')
}
} catch (err) {
toast({
content: err as any,
type: 'error',
})
}
submitting = false
}
</script>
<svelte:head>
<title>Sign up</title>
</svelte:head>
<form
class="flex flex-col gap-4 max-w-2xl mx-auto h-full"
on:submit|preventDefault={submit}
>
<span class="flex gap-4 items-center font-bold text-xl text-center mx-auto">
{#if data.site_view.site.icon}
<Avatar width={48} url={data.site_view.site.icon} />
{/if}
{data.site_view.site.name}
</span>
<h1 class="font-bold text-3xl">Create account</h1>
{#if data.site_view.local_site.registration_mode == 'Closed'}
<p class="mx-auto text-left">Registrations are closed for this instance.</p>
{:else}
<TextInput
bind:value={email}
label="Email"
required={data.site_view.local_site.require_email_verification}
type="email"
placeholder="nigerianprince@notascam.com"
/>
<TextInput
bind:value={username}
label="Username"
required
placeholder="BobTheDestroyer"
/>
<TextInput
bind:value={password}
label="Password"
required
type="password"
/>
<TextInput
bind:value={passwordVerify}
label="Confirm Password"
required
type="password"
/>
{#if data.site_view.local_site.registration_mode == 'RequireApplication'}
<Card
class="p-4 dark:text-yellow-200 text-yellow-800"
cardColor="warning"
>
<Icon src={ExclamationTriangle} mini size="20" />
To join this instance, you need to fill out this application, and wait to
be accepted.
</Card>
{#if data.site_view.local_site.application_question}
<Markdown source={data.site_view.local_site.application_question} />
{/if}
<TextArea
label="Application"
required
placeholder="i like this instance's icon "
bind:value={application}
/>
{/if}
{#if captchaRequired}
<div>
<div class="block my-1 font-bold text-sm">Captcha</div>
{#await getCaptcha()}
<Spinner width={32} />
{:then}
{#if captcha?.ok}
<div class="flex flex-col gap-4">
<img
src="data:image/png;base64,{captcha.ok.png}"
alt="Captcha"
class="w-max"
/>
<audio controls src={captchaAudio} />
<TextInput
required
bind:value={verifyCaptcha}
placeholder="i guess i'm a robot"
/>
</div>
{:else}
<div class="flex gap-2">
<Icon src={QuestionMarkCircle} mini size="24" />
No captcha was returned :(
</div>
{/if}
{/await}
</div>
{/if}
<input type="dn" name="honeypot" bind:value={honeypot} class="hidden" />
<Button
submit
color="primary"
size="lg"
loading={submitting}
disabled={submitting}
class="mt-auto"
>
Submit
</Button>
{/if}
</form>
+7
View File
@@ -0,0 +1,7 @@
import { getClient } from '$lib/lemmy.js'
export async function load({ params, fetch }) {
const site = await getClient(params.instance, fetch).getSite()
return site
}