added hashed password when user signing up

pull/3/head
TZGyn 2 years ago
parent 486d9128ff
commit fc62cde6f6
Signed by: TZGyn
GPG Key ID: 122EAF77AE81FD4A

@ -1,71 +1,72 @@
import type { PageServerLoad, Actions } from './$types'; import type { PageServerLoad, Actions } from './$types'
import { fail } from '@sveltejs/kit'; import { fail } from '@sveltejs/kit'
import { superValidate } from 'sveltekit-superforms/server'; import { superValidate } from 'sveltekit-superforms/server'
import { formSchema } from './schema'; 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'; import { nanoid } from 'nanoid'
export const load = (async () => { export const load = (async () => {
return { return {
form: superValidate(formSchema), form: superValidate(formSchema),
}; }
}) satisfies PageServerLoad; }) satisfies PageServerLoad
export const actions: Actions = { export const actions: Actions = {
default: async (event) => { default: async (event) => {
const form = await superValidate(event, formSchema); const form = await superValidate(event, formSchema)
if (!form.valid) { if (!form.valid) {
return fail(400, { return fail(400, {
form, form,
}); })
} }
if (form.data.password !== form.data.password_confirm) { if (form.data.password !== form.data.password_confirm) {
return fail(400, { return fail(400, {
form, form,
}); })
} }
try { try {
const users = await db const users = await db
.select() .select()
.from(userSchema) .from(userSchema)
.where(eq(userSchema.email, form.data.email)); .where(eq(userSchema.email, form.data.email))
const user = users[0]; const user = users[0]
if (user) { if (!user) {
const hashedPassword = await Bun.password.hash(form.data.password)
await db await db
.insert(userSchema) .insert(userSchema)
.values({ email: form.data.email, password: form.data.password }); .values({ email: form.data.email, password: hashedPassword })
const token = nanoid(32); const token = nanoid(32)
event.cookies.set('token', token, { event.cookies.set('token', token, {
httpOnly: true, httpOnly: true,
sameSite: 'strict', sameSite: 'strict',
path: '/', path: '/',
}); })
return { return {
form, form,
}; }
} else { } else {
return fail(400, { return fail(400, {
form, form,
}); })
} }
} catch (error) { } catch (error) {
if (error instanceof SyntaxError) { if (error instanceof SyntaxError) {
return fail(400, { return fail(400, {
form, form,
}); })
} else { } else {
console.log(error); console.log(error)
return fail(400, { return fail(400, {
form, form,
}); })
} }
} }
}, },
}; }

Loading…
Cancel
Save