mirror of https://github.com/TZGyn/shortener
added nanoid and signup form action
parent
c49faab6ae
commit
6fae7de4a8
@ -1,62 +1,32 @@
|
||||
<script lang="ts">
|
||||
import { Button } from '$lib/components/ui/button';
|
||||
import { Input } from '$lib/components/ui/input';
|
||||
import { Label } from '$lib/components/ui/label';
|
||||
import { Loader2 } from 'lucide-svelte';
|
||||
import { cn } from '$lib/utils';
|
||||
import * as Form from '$lib/components/ui/form';
|
||||
import { formSchema, type FormSchema } from '../schema';
|
||||
import type { SuperValidated } from 'sveltekit-superforms';
|
||||
|
||||
let className: string | undefined | null = undefined;
|
||||
export { className as class };
|
||||
|
||||
let isLoading = false;
|
||||
async function onSubmit(event: SubmitEvent) {
|
||||
isLoading = true;
|
||||
|
||||
setTimeout(() => {
|
||||
isLoading = false;
|
||||
}, 3000);
|
||||
}
|
||||
export let form: SuperValidated<FormSchema>;
|
||||
</script>
|
||||
|
||||
<div class={cn('grid gap-6', className)} {...$$restProps}>
|
||||
<form on:submit|preventDefault={onSubmit}>
|
||||
<div class="grid gap-4">
|
||||
<div class="grid gap-1">
|
||||
<Label for="email">Email</Label>
|
||||
<Input
|
||||
id="email"
|
||||
placeholder="name@example.com"
|
||||
type="email"
|
||||
autocapitalize="none"
|
||||
autocomplete="email"
|
||||
autocorrect="off"
|
||||
disabled={isLoading}
|
||||
/>
|
||||
</div>
|
||||
<div class="grid gap-1">
|
||||
<Label for="password">Password</Label>
|
||||
<Input
|
||||
id="password"
|
||||
placeholder="••••••••"
|
||||
type="password"
|
||||
disabled={isLoading}
|
||||
/>
|
||||
</div>
|
||||
<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>
|
||||
<Form.Root method="POST" {form} schema={formSchema} let:config>
|
||||
<Form.Field {config} name="email">
|
||||
<Form.Item>
|
||||
<Form.Label>Email</Form.Label>
|
||||
<Form.Input placeholder="name@example.com" />
|
||||
<Form.Validation />
|
||||
</Form.Item>
|
||||
</Form.Field>
|
||||
<Form.Field {config} name="password">
|
||||
<Form.Item>
|
||||
<Form.Label>Password</Form.Label>
|
||||
<Form.Input type="password" placeholder="••••••••" />
|
||||
<Form.Validation />
|
||||
</Form.Item>
|
||||
</Form.Field>
|
||||
<Form.Field {config} name="password_confirm">
|
||||
<Form.Item>
|
||||
<Form.Label>Password Confirm</Form.Label>
|
||||
<Form.Input type="password" placeholder="••••••••" />
|
||||
<Form.Validation />
|
||||
</Form.Item>
|
||||
</Form.Field>
|
||||
<Form.Button class="w-full">Sign Up</Form.Button>
|
||||
</Form.Root>
|
||||
|
||||
@ -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