diff --git a/frontend/src/routes/(auth)/signup/(components)/form.svelte b/frontend/src/routes/(auth)/signup/(components)/form.svelte new file mode 100644 index 0000000..ccaab68 --- /dev/null +++ b/frontend/src/routes/(auth)/signup/(components)/form.svelte @@ -0,0 +1,69 @@ + + +
+ + + Email + + + + + + + Password + + + + + + + Password Confirm + + + + + Sign Up +
diff --git a/frontend/src/routes/(auth)/signup/(components)/user-auth-form.svelte b/frontend/src/routes/(auth)/signup/(components)/user-auth-form.svelte deleted file mode 100644 index 7552ae1..0000000 --- a/frontend/src/routes/(auth)/signup/(components)/user-auth-form.svelte +++ /dev/null @@ -1,67 +0,0 @@ - - -
-
- - -
-
- - -
-
- - -
- -
diff --git a/frontend/src/routes/(auth)/signup/+page.server.ts b/frontend/src/routes/(auth)/signup/+page.server.ts new file mode 100644 index 0000000..88f2b85 --- /dev/null +++ b/frontend/src/routes/(auth)/signup/+page.server.ts @@ -0,0 +1,63 @@ +import type { PageServerLoad, Actions } from './$types' +import { fail } from '@sveltejs/kit' +import { setError, superValidate } from 'sveltekit-superforms' +import { formSchema } from './schema' +import { zod } from 'sveltekit-superforms/adapters' +import { db } from '$lib/db' +import { user as userSchema } from '$lib/db/schema' +import { eq } from 'drizzle-orm' +import { lucia } from '$lib/server/auth' + +export const load = (async (event) => { + return { + form: await superValidate(zod(formSchema)), + } +}) satisfies PageServerLoad + +export const actions: Actions = { + default: async (event) => { + const form = await superValidate(event, zod(formSchema)) + if (!form.valid) { + return fail(400, { + form, + }) + } + + if (form.data.password !== form.data.password_confirm) { + return setError(form, 'password_confirm', 'Password Not Match') + } + + const users = await db + .select() + .from(userSchema) + .where(eq(userSchema.email, form.data.email)) + + const user = users[0] + + if (user) { + return setError(form, 'email', 'Email Already Exist') + } + + const hashedPassword = await Bun.password.hash(form.data.password) + const returnUsers = await db + .insert(userSchema) + .values({ + email: form.data.email, + password: hashedPassword, + }) + .returning() + + const session = await lucia.createSession(returnUsers[0].id, {}) + const sessionCookie = lucia.createSessionCookie(session.id) + + event.cookies.set(sessionCookie.name, sessionCookie.value, { + ...sessionCookie.attributes, + path: '/', + secure: Bun.env.APP_ENV === 'prod', + }) + + return { + form, + } + }, +} diff --git a/frontend/src/routes/(auth)/signup/+page.svelte b/frontend/src/routes/(auth)/signup/+page.svelte index eb7e01e..c94894a 100644 --- a/frontend/src/routes/(auth)/signup/+page.svelte +++ b/frontend/src/routes/(auth)/signup/+page.svelte @@ -1,37 +1,40 @@
-
+ class="container relative flex-col justify-center items-center h-screen md:grid lg:grid-cols-2 lg:px-0 lg:max-w-none"> +
+ class="flex flex-col justify-center mx-auto space-y-6 w-full sm:w-[350px]">

Create an account

-

+

Enter your email below to create your account

- -

+

+

Already Have An Account? Login{' '} + class="underline underline-offset-4 hover:text-primary"> Here {' '}

diff --git a/frontend/src/routes/(auth)/signup/schema.ts b/frontend/src/routes/(auth)/signup/schema.ts index 7d2c758..46eb6b0 100644 --- a/frontend/src/routes/(auth)/signup/schema.ts +++ b/frontend/src/routes/(auth)/signup/schema.ts @@ -2,8 +2,16 @@ import { z } from 'zod' export const formSchema = z.object({ email: z.string().email(), - password: z.string().min(8), - password_confirm: z.string().min(8), + password: z + .string() + .min(8, { + message: 'Password must be at least 8 characters long', + }), + password_confirm: z + .string() + .min(8, { + message: 'Password must be at least 8 characters long', + }), }) export type FormSchema = typeof formSchema diff --git a/frontend/src/routes/api/signup/+server.ts b/frontend/src/routes/api/signup/+server.ts deleted file mode 100644 index 066c5cd..0000000 --- a/frontend/src/routes/api/signup/+server.ts +++ /dev/null @@ -1,76 +0,0 @@ -import type { RequestHandler } from './$types' -import { user as userSchema } from '$lib/db/schema' -import { eq } from 'drizzle-orm' -import { userCreateSchema } from '$lib/server/types' -import { db } from '$lib/db' -import { lucia } from '$lib/server/auth' - -export const GET: RequestHandler = async () => { - return new Response() -} - -export const POST: RequestHandler = async (event) => { - const body = await event.request.json() - - const userCreate = userCreateSchema.safeParse(body) - - if (!userCreate.success) { - return new Response( - JSON.stringify({ - success: false, - }), - ) - } - - if (userCreate.data.password !== userCreate.data.password_confirm) { - return new Response( - JSON.stringify({ - success: false, - message: 'password doesnt match', - }), - ) - } - - const users = await db - .select() - .from(userSchema) - .where(eq(userSchema.email, userCreate.data.email)) - - const user = users[0] - - if (!user) { - const hashedPassword = await Bun.password.hash( - userCreate.data.password, - ) - const returnUsers = await db - .insert(userSchema) - .values({ - email: userCreate.data.email, - password: hashedPassword, - }) - .returning() - - const user = returnUsers[0] - - const session = await lucia.createSession(user.id, {}) - const sessionCookie = lucia.createSessionCookie(session.id) - - event.cookies.set(sessionCookie.name, sessionCookie.value, { - ...sessionCookie.attributes, - path: '/', - secure: Bun.env.APP_ENV === 'prod', - }) - - return new Response( - JSON.stringify({ - success: true, - }), - ) - } else { - return new Response( - JSON.stringify({ - success: false, - }), - ) - } -}