mirror of https://github.com/TZGyn/shortener
added nanoid and signup form action
parent
c49faab6ae
commit
6fae7de4a8
@ -1,62 +1,32 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { Button } from '$lib/components/ui/button';
|
import * as Form from '$lib/components/ui/form';
|
||||||
import { Input } from '$lib/components/ui/input';
|
import { formSchema, type FormSchema } from '../schema';
|
||||||
import { Label } from '$lib/components/ui/label';
|
import type { SuperValidated } from 'sveltekit-superforms';
|
||||||
import { Loader2 } from 'lucide-svelte';
|
|
||||||
import { cn } from '$lib/utils';
|
|
||||||
|
|
||||||
let className: string | undefined | null = undefined;
|
export let form: SuperValidated<FormSchema>;
|
||||||
export { className as class };
|
|
||||||
|
|
||||||
let isLoading = false;
|
|
||||||
async function onSubmit(event: SubmitEvent) {
|
|
||||||
isLoading = true;
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
isLoading = false;
|
|
||||||
}, 3000);
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class={cn('grid gap-6', className)} {...$$restProps}>
|
<Form.Root method="POST" {form} schema={formSchema} let:config>
|
||||||
<form on:submit|preventDefault={onSubmit}>
|
<Form.Field {config} name="email">
|
||||||
<div class="grid gap-4">
|
<Form.Item>
|
||||||
<div class="grid gap-1">
|
<Form.Label>Email</Form.Label>
|
||||||
<Label for="email">Email</Label>
|
<Form.Input placeholder="name@example.com" />
|
||||||
<Input
|
<Form.Validation />
|
||||||
id="email"
|
</Form.Item>
|
||||||
placeholder="name@example.com"
|
</Form.Field>
|
||||||
type="email"
|
<Form.Field {config} name="password">
|
||||||
autocapitalize="none"
|
<Form.Item>
|
||||||
autocomplete="email"
|
<Form.Label>Password</Form.Label>
|
||||||
autocorrect="off"
|
<Form.Input type="password" placeholder="••••••••" />
|
||||||
disabled={isLoading}
|
<Form.Validation />
|
||||||
/>
|
</Form.Item>
|
||||||
</div>
|
</Form.Field>
|
||||||
<div class="grid gap-1">
|
<Form.Field {config} name="password_confirm">
|
||||||
<Label for="password">Password</Label>
|
<Form.Item>
|
||||||
<Input
|
<Form.Label>Password Confirm</Form.Label>
|
||||||
id="password"
|
<Form.Input type="password" placeholder="••••••••" />
|
||||||
placeholder="••••••••"
|
<Form.Validation />
|
||||||
type="password"
|
</Form.Item>
|
||||||
disabled={isLoading}
|
</Form.Field>
|
||||||
/>
|
<Form.Button class="w-full">Sign Up</Form.Button>
|
||||||
</div>
|
</Form.Root>
|
||||||
<div class="grid gap-1">
|
|
||||||
<Label for="password_confirm">Password Confirm</Label>
|
|
||||||
<Input
|
|
||||||
id="password_confirm"
|
|
||||||
placeholder="••••••••"
|
|
||||||
type="password"
|
|
||||||
disabled={isLoading}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<Button disabled={isLoading} type="submit" class="flex gap-2">
|
|
||||||
{#if isLoading}
|
|
||||||
<Loader2 class="animate-spin" />
|
|
||||||
{/if}
|
|
||||||
Sign Up
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
|
|||||||
@ -0,0 +1,71 @@
|
|||||||
|
import type { PageServerLoad, Actions } from './$types';
|
||||||
|
import { fail } from '@sveltejs/kit';
|
||||||
|
import { superValidate } from 'sveltekit-superforms/server';
|
||||||
|
import { formSchema } from './schema';
|
||||||
|
import { db } from '$lib/db';
|
||||||
|
import { user as userSchema } from '$lib/db/schema';
|
||||||
|
import { eq } from 'drizzle-orm';
|
||||||
|
import { nanoid } from 'nanoid';
|
||||||
|
|
||||||
|
export const load = (async () => {
|
||||||
|
return {
|
||||||
|
form: superValidate(formSchema),
|
||||||
|
};
|
||||||
|
}) satisfies PageServerLoad;
|
||||||
|
|
||||||
|
export const actions: Actions = {
|
||||||
|
default: async (event) => {
|
||||||
|
const form = await superValidate(event, formSchema);
|
||||||
|
|
||||||
|
if (!form.valid) {
|
||||||
|
return fail(400, {
|
||||||
|
form,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (form.data.password !== form.data.password_confirm) {
|
||||||
|
return fail(400, {
|
||||||
|
form,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const users = await db
|
||||||
|
.select()
|
||||||
|
.from(userSchema)
|
||||||
|
.where(eq(userSchema.email, form.data.email));
|
||||||
|
|
||||||
|
const user = users[0];
|
||||||
|
|
||||||
|
if (user) {
|
||||||
|
await db
|
||||||
|
.insert(userSchema)
|
||||||
|
.values({ email: form.data.email, password: form.data.password });
|
||||||
|
const token = nanoid(32);
|
||||||
|
event.cookies.set('token', token, {
|
||||||
|
httpOnly: true,
|
||||||
|
sameSite: 'strict',
|
||||||
|
path: '/',
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
form,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return fail(400, {
|
||||||
|
form,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof SyntaxError) {
|
||||||
|
return fail(400, {
|
||||||
|
form,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.log(error);
|
||||||
|
return fail(400, {
|
||||||
|
form,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
export const formSchema = z.object({
|
||||||
|
email: z.string().email(),
|
||||||
|
password: z.string().min(8),
|
||||||
|
password_confirm: z.string().min(8),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type FormSchema = typeof formSchema;
|
||||||
Loading…
Reference in New Issue