diff --git a/src/routes/auth/register/+page.server.ts b/src/routes/auth/register/+page.server.ts new file mode 100644 index 00000000..14b0e421 --- /dev/null +++ b/src/routes/auth/register/+page.server.ts @@ -0,0 +1,54 @@ +import type { Actions, PageServerLoad } from './$types' +import { fail, redirect } from '@sveltejs/kit' + +export const load: PageServerLoad = async ({ locals, url }) => { + if (locals.session.isAuthenticated) { + redirect(302, url.searchParams.get('next') ?? '/me') + } + return {} +} + +export const actions: Actions = { + default: async ({ request, fetch, url }) => { + const form = await request.formData() + const username = String(form.get('username') ?? '') + const email = String(form.get('email') ?? '') + const password = String(form.get('password') ?? '') + const password_confirmation = String(form.get('password_confirmation') ?? '') + + // Basic validation + if (!username || !email || !password || !password_confirmation) { + return fail(400, { + error: 'All fields are required', + username, + email + }) + } + + if (password !== password_confirmation) { + return fail(400, { + error: "Passwords don't match", + username, + email + }) + } + + const res = await fetch('/auth/signup', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ username, email, password, password_confirmation }) + }) + + if (res.ok) { + redirect(303, url.searchParams.get('next') ?? '/me') + } + + const j = await res.json().catch(() => ({})) + return fail(res.status, { + error: j.error ?? 'Registration failed', + details: j.details, + username, + email + }) + } +} diff --git a/src/routes/auth/register/+page.svelte b/src/routes/auth/register/+page.svelte new file mode 100644 index 00000000..59501224 --- /dev/null +++ b/src/routes/auth/register/+page.svelte @@ -0,0 +1,326 @@ + + + + {m.auth_register_title()} | Granblue Party + + + + { + isSubmitting = true + return async ({ update }) => { + isSubmitting = false + await update() + } + }} + > + + + + + + + + + {#if form?.error} + {form.error} + {/if} + + + {isSubmitting ? m.auth_register_submitting() : m.auth_register_submit()} + + + + {#snippet footer()} + + {m.auth_register_hasAccount()} + {m.auth_register_login()} + + {/snippet} + + +
{form.error}
+ {m.auth_register_hasAccount()} + {m.auth_register_login()} +