|
| 1 | +<script lang="ts"> |
| 2 | + import { goto } from "$app/navigation" |
| 3 | + import { Button } from "$lib/components/ui/button/index.js" |
| 4 | + import { Input } from "$lib/components/ui/input/index.js" |
| 5 | + import { Label } from "$lib/components/ui/label/index.js" |
| 6 | + import Logo from "$lib/images/logo.svg" |
| 7 | + import Github from "$lib/images/github.svg" |
| 8 | + import Google from "$lib/images/Google.svg" |
| 9 | + import { createMutation } from "@tanstack/svelte-query" |
| 10 | + import { z } from "@undb/zod" |
| 11 | + import { defaults, superForm } from "sveltekit-superforms" |
| 12 | + import { zodClient } from "sveltekit-superforms/adapters" |
| 13 | + import * as Form from "$lib/components/ui/form" |
| 14 | + import { Separator } from "$lib/components/ui/separator" |
| 15 | + import PasswordInput from "$lib/components/ui/input/password-input.svelte" |
| 16 | + import { LoaderCircleIcon } from "lucide-svelte" |
| 17 | +
|
| 18 | + export let redirect: string | null |
| 19 | + export let invitationId: string | null |
| 20 | + export let email: string | null |
| 21 | + export let githubEnabled: boolean |
| 22 | + export let googleEnabled: boolean |
| 23 | + export let onSuccess: () => void = () => {} |
| 24 | +
|
| 25 | + const schema = z.object({ |
| 26 | + email: z.string().email(), |
| 27 | + password: z.string().min(6), |
| 28 | + username: z.string().min(2).optional(), |
| 29 | + }) |
| 30 | +
|
| 31 | + const formSchema = schema |
| 32 | + .merge( |
| 33 | + z.object({ |
| 34 | + confirmPassword: z.string(), |
| 35 | + }), |
| 36 | + ) |
| 37 | + .refine((data) => data.password === data.confirmPassword, { |
| 38 | + message: "Passwords do not match", |
| 39 | + }) |
| 40 | +
|
| 41 | + type SignupSchema = z.infer<typeof schema> |
| 42 | +
|
| 43 | + let signupError = false |
| 44 | +
|
| 45 | + const signupMutation = createMutation({ |
| 46 | + mutationFn: async (input: SignupSchema) => { |
| 47 | + try { |
| 48 | + const { ok } = await fetch("/api/signup", { |
| 49 | + method: "POST", |
| 50 | + body: JSON.stringify({ ...input, invitationId }), |
| 51 | + }) |
| 52 | + if (!ok) { |
| 53 | + throw new Error("Failed to signup") |
| 54 | + } |
| 55 | + } catch (error) { |
| 56 | + signupError = true |
| 57 | + } |
| 58 | + }, |
| 59 | + onMutate(variables) { |
| 60 | + signupError = false |
| 61 | + }, |
| 62 | + async onSuccess(data, variables, context) { |
| 63 | + onSuccess() |
| 64 | + }, |
| 65 | + onError(error, variables, context) { |
| 66 | + signupError = true |
| 67 | + }, |
| 68 | + }) |
| 69 | +
|
| 70 | + const form = superForm( |
| 71 | + defaults( |
| 72 | + { |
| 73 | + email: email || "", |
| 74 | + password: "", |
| 75 | + confirmPassword: "", |
| 76 | + username: "", |
| 77 | + }, |
| 78 | + zodClient(formSchema), |
| 79 | + ), |
| 80 | + { |
| 81 | + SPA: true, |
| 82 | + dataType: "json", |
| 83 | + validators: zodClient(formSchema), |
| 84 | + resetForm: false, |
| 85 | + invalidateAll: false, |
| 86 | + onUpdate(event) { |
| 87 | + if (!event.form.valid) { |
| 88 | + console.log(event.form.errors) |
| 89 | + return |
| 90 | + } |
| 91 | +
|
| 92 | + const { confirmPassword, ...data } = event.form.data |
| 93 | + $signupMutation.mutate(data) |
| 94 | + }, |
| 95 | + }, |
| 96 | + ) |
| 97 | + const { enhance, form: formData, allErrors } = form |
| 98 | + $: disabled = $allErrors.length > 0 || $signupMutation.isPending |
| 99 | +</script> |
| 100 | + |
| 101 | +<form method="POST" use:enhance> |
| 102 | + <div class="grid gap-2"> |
| 103 | + <div class="grid gap-2"> |
| 104 | + <Form.Field {form} name="username"> |
| 105 | + <Form.Control let:attrs> |
| 106 | + <Form.Label for="username">Username</Form.Label> |
| 107 | + <Input {...attrs} placeholder="Enter your display username" id="username" bind:value={$formData.username} /> |
| 108 | + </Form.Control> |
| 109 | + <Form.Description /> |
| 110 | + <Form.FieldErrors /> |
| 111 | + </Form.Field> |
| 112 | + </div> |
| 113 | + <div class="grid gap-2"> |
| 114 | + <Form.Field {form} name="email"> |
| 115 | + <Form.Control let:attrs> |
| 116 | + <Form.Label for="email">Email</Form.Label> |
| 117 | + <Input {...attrs} id="email" type="email" placeholder="Enter your work email" bind:value={$formData.email} /> |
| 118 | + </Form.Control> |
| 119 | + <Form.Description /> |
| 120 | + <Form.FieldErrors /> |
| 121 | + </Form.Field> |
| 122 | + </div> |
| 123 | + <div class="grid gap-2"> |
| 124 | + <Form.Field {form} name="password"> |
| 125 | + <Form.Control let:attrs> |
| 126 | + <div class="flex justify-between"> |
| 127 | + <Label for="password">Password</Label> |
| 128 | + </div> |
| 129 | + <PasswordInput |
| 130 | + {...attrs} |
| 131 | + id="password" |
| 132 | + type="password" |
| 133 | + placeholder="******" |
| 134 | + bind:value={$formData.password} |
| 135 | + /> |
| 136 | + </Form.Control> |
| 137 | + <Form.Description /> |
| 138 | + <Form.FieldErrors /> |
| 139 | + </Form.Field> |
| 140 | + </div> |
| 141 | + <div class="grid gap-2"> |
| 142 | + <Form.Field {form} name="confirmPassword"> |
| 143 | + <Form.Control let:attrs> |
| 144 | + <div class="flex justify-between"> |
| 145 | + <Label for="confirmPassword">Confirm Password</Label> |
| 146 | + </div> |
| 147 | + <PasswordInput |
| 148 | + {...attrs} |
| 149 | + id="confirmPassword" |
| 150 | + type="password" |
| 151 | + placeholder="******" |
| 152 | + bind:value={$formData.confirmPassword} |
| 153 | + /> |
| 154 | + </Form.Control> |
| 155 | + <Form.Description /> |
| 156 | + <Form.FieldErrors /> |
| 157 | + </Form.Field> |
| 158 | + </div> |
| 159 | + <Button {disabled} type="submit" class="w-full"> |
| 160 | + {#if $signupMutation.isPending} |
| 161 | + <LoaderCircleIcon class="mr-2 h-5 w-5 animate-spin" /> |
| 162 | + {/if} |
| 163 | + Create an account |
| 164 | + </Button> |
| 165 | + </div> |
| 166 | + <div class="mt-4 text-center text-sm"> |
| 167 | + Already have an account? |
| 168 | + {#if invitationId} |
| 169 | + <a href={`/login?invitationId=${invitationId}`} class="underline"> Sign in </a> |
| 170 | + {:else if redirect} |
| 171 | + <a href={`/login?redirect=${encodeURIComponent(redirect)}`} class="underline"> Sign in </a> |
| 172 | + {:else} |
| 173 | + <a href="/login" class="underline"> Sign in </a> |
| 174 | + {/if} |
| 175 | + </div> |
| 176 | + {#if !invitationId && (githubEnabled || googleEnabled)} |
| 177 | + <Separator class="my-6" /> |
| 178 | + <div class="space-y-2"> |
| 179 | + {#if githubEnabled} |
| 180 | + <Button href="/login/github" variant="secondary" class="w-full"> |
| 181 | + <img class="mr-2 h-4 w-4" src={Github} alt="github" /> |
| 182 | + Login with Github |
| 183 | + </Button> |
| 184 | + {/if} |
| 185 | + {#if googleEnabled} |
| 186 | + <Button href="/login/google" variant="secondary" class="w-full"> |
| 187 | + <img class="mr-2 h-4 w-4" src={Google} alt="google" /> |
| 188 | + Login with Google |
| 189 | + </Button> |
| 190 | + {/if} |
| 191 | + </div> |
| 192 | + {/if} |
| 193 | +</form> |
0 commit comments