added nanoid and signup form action

pull/3/head
TZGyn 2 years ago
parent c49faab6ae
commit 6fae7de4a8
Signed by: TZGyn
GPG Key ID: 122EAF77AE81FD4A

Binary file not shown.

@ -36,6 +36,7 @@
"formsnap": "^0.4.1", "formsnap": "^0.4.1",
"lucide-svelte": "^0.292.0", "lucide-svelte": "^0.292.0",
"mode-watcher": "^0.0.7", "mode-watcher": "^0.0.7",
"nanoid": "^5.0.3",
"postgres": "^3.4.3", "postgres": "^3.4.3",
"sveltekit-superforms": "^1.10.1", "sveltekit-superforms": "^1.10.1",
"tailwind-merge": "^2.0.0", "tailwind-merge": "^2.0.0",

@ -5,6 +5,7 @@ import { formSchema } from './schema';
import { db } from '$lib/db'; import { db } from '$lib/db';
import { user as userSchema } from '$lib/db/schema'; import { user as userSchema } from '$lib/db/schema';
import { eq } from 'drizzle-orm'; import { eq } from 'drizzle-orm';
import { nanoid } from 'nanoid';
export const load = (async () => { export const load = (async () => {
return { return {
@ -16,7 +17,6 @@ export const actions: Actions = {
default: async (event) => { default: async (event) => {
const form = await superValidate(event, formSchema); const form = await superValidate(event, formSchema);
console.log(form);
if (!form.valid) { if (!form.valid) {
return fail(400, { return fail(400, {
form, form,
@ -34,6 +34,12 @@ export const actions: Actions = {
user && (await Bun.password.verify(form.data.password, user.password)); user && (await Bun.password.verify(form.data.password, user.password));
if (user && matchPassword) { if (user && matchPassword) {
const token = nanoid(32);
event.cookies.set('token', token, {
httpOnly: true,
sameSite: 'strict',
path: '/',
});
return { return {
form, form,
}; };

@ -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,
});
}
}
},
};

@ -1,6 +1,9 @@
<script lang="ts"> <script lang="ts">
import type { PageData } from './$types';
import ThemeToggle from '$lib/components/theme-toggle.svelte'; import ThemeToggle from '$lib/components/theme-toggle.svelte';
import UserAuthForm from './(components)/user-auth-form.svelte'; import UserAuthForm from './(components)/user-auth-form.svelte';
export let data: PageData;
</script> </script>
<div <div
@ -28,7 +31,7 @@
Enter your email below to create your account Enter your email below to create your account
</p> </p>
</div> </div>
<UserAuthForm /> <UserAuthForm form={data.form} />
<p class="px-8 text-center text-sm text-muted-foreground"> <p class="px-8 text-center text-sm text-muted-foreground">
Already Have An Account? Login{' '} Already Have An Account? Login{' '}
<a <a

@ -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…
Cancel
Save