From 3d08574401c1f44adc3b8c3a7a82bbcbc33bf31d Mon Sep 17 00:00:00 2001 From: Insub Kim Date: Wed, 11 Jun 2025 10:08:31 +0000 Subject: [PATCH] hello world --- src/routes/(public)/login/+page.server.ts | 138 ++++++++++++++++++++ src/routes/(public)/login/+page.svelte | 19 +-- src/routes/(public)/login/login-form.svelte | 55 +++++--- src/routes/(public)/login/schema.ts | 8 ++ 4 files changed, 186 insertions(+), 34 deletions(-) create mode 100644 src/routes/(public)/login/+page.server.ts create mode 100644 src/routes/(public)/login/schema.ts diff --git a/src/routes/(public)/login/+page.server.ts b/src/routes/(public)/login/+page.server.ts new file mode 100644 index 0000000..8dcf6ce --- /dev/null +++ b/src/routes/(public)/login/+page.server.ts @@ -0,0 +1,138 @@ +import type { PageServerLoad, Actions } from "./$types.js"; +import { fail } from "@sveltejs/kit"; +import { superValidate } from "sveltekit-superforms"; +import { zod } from "sveltekit-superforms/adapters"; +import { formSchema } from "./schema"; + +export const load: PageServerLoad = async () => { + return { + form: await superValidate(zod(formSchema)), + }; +}; + +export const actions: Actions = { + default: async (event) => { + const form = await superValidate(event, zod(formSchema)); + if (!form.valid) { + return fail(400, { + form, + }); + } + return { + form, + }; + }, +}; + +// import { hash, verify } from '@node-rs/argon2'; +// import { encodeBase32LowerCase } from '@oslojs/encoding'; +// import { fail, redirect } from '@sveltejs/kit'; +// import { eq } from 'drizzle-orm'; +// import * as auth from '$lib/server/auth'; +// import { db } from '$lib/server/db'; +// import * as table from '$lib/server/db/schema'; +// import type { Actions, PageServerLoad } from './$types'; + +// export const load: PageServerLoad = async (event) => { +// if (event.locals.user) { +// return redirect(302, '/demo/lucia'); +// } +// return {}; +// }; + +// export const actions: Actions = { +// login: async (event) => { +// const formData = await event.request.formData(); +// const username = formData.get('username'); +// const password = formData.get('password'); + +// if (!validateUsername(username)) { +// return fail(400, { message: 'Invalid username (min 3, max 31 characters, alphanumeric only)' }); +// } +// if (!validatePassword(password)) { +// return fail(400, { message: 'Invalid password (min 6, max 255 characters)' }); +// } + +// const results = await db +// .select() +// .from(table.user) +// .where(eq(table.user.username, username)); + +// const existingUser = results.at(0); +// if (!existingUser) { +// return fail(400, { message: 'Incorrect username or password' }); +// } + +// const validPassword = await verify(existingUser.passwordHash, password, { +// memoryCost: 19456, +// timeCost: 2, +// outputLen: 32, +// parallelism: 1, +// }); +// if (!validPassword) { +// return fail(400, { message: 'Incorrect username or password' }); +// } + +// const sessionToken = auth.generateSessionToken(); +// const session = await auth.createSession(sessionToken, existingUser.id); +// auth.setSessionTokenCookie(event, sessionToken, session.expiresAt); + +// return redirect(302, '/demo/lucia'); +// }, +// register: async (event) => { +// const formData = await event.request.formData(); +// const username = formData.get('username'); +// const password = formData.get('password'); + +// if (!validateUsername(username)) { +// return fail(400, { message: 'Invalid username' }); +// } +// if (!validatePassword(password)) { +// return fail(400, { message: 'Invalid password' }); +// } + +// const userId = generateUserId(); +// const passwordHash = await hash(password, { +// // recommended minimum parameters +// memoryCost: 19456, +// timeCost: 2, +// outputLen: 32, +// parallelism: 1, +// }); + +// try { +// await db.insert(table.user).values({ id: userId, username, passwordHash }); + +// const sessionToken = auth.generateSessionToken(); +// const session = await auth.createSession(sessionToken, userId); +// auth.setSessionTokenCookie(event, sessionToken, session.expiresAt); +// } catch (e) { +// return fail(500, { message: 'An error has occurred' }); +// } +// return redirect(302, '/demo/lucia'); +// }, +// }; + +// function generateUserId() { +// // ID with 120 bits of entropy, or about the same as UUID v4. +// const bytes = crypto.getRandomValues(new Uint8Array(15)); +// const id = encodeBase32LowerCase(bytes); +// return id; +// } + +// function validateUsername(username: unknown): username is string { +// return ( +// typeof username === 'string' && +// username.length >= 3 && +// username.length <= 31 && +// /^[a-z0-9_-]+$/.test(username) +// ); +// } + +// function validatePassword(password: unknown): password is string { +// return ( +// typeof password === 'string' && +// password.length >= 6 && +// password.length <= 255 +// ); +// } diff --git a/src/routes/(public)/login/+page.svelte b/src/routes/(public)/login/+page.svelte index c26ffe9..63ad961 100644 --- a/src/routes/(public)/login/+page.svelte +++ b/src/routes/(public)/login/+page.svelte @@ -1,17 +1,10 @@ -
+ \ No newline at end of file +
diff --git a/src/routes/(public)/login/login-form.svelte b/src/routes/(public)/login/login-form.svelte index 14dc451..778a699 100644 --- a/src/routes/(public)/login/login-form.svelte +++ b/src/routes/(public)/login/login-form.svelte @@ -1,44 +1,56 @@ +
+ + + {#snippet children({ props })} + Email + + {/snippet} + + This is your public display name. + + + Submit +
+
:: LOGIN :: - 외부인증 서비스를 통해 소셜로그인.
-
- -
-
- - 등록한 계정으로 직접 로그인 - -
@@ -82,3 +94,4 @@ 과 개인정보 보호정책 에 동의합니다.
+--> diff --git a/src/routes/(public)/login/schema.ts b/src/routes/(public)/login/schema.ts new file mode 100644 index 0000000..aa1e074 --- /dev/null +++ b/src/routes/(public)/login/schema.ts @@ -0,0 +1,8 @@ +import { z } from "zod"; + +export const formSchema = z.object({ + email: z.string().email(), + password: z.string().min(8), +}); + +export type FormSchema = typeof formSchema; \ No newline at end of file