diff --git a/prisma/dev.db b/prisma/dev.db index 4d6eb2e..86ecbef 100644 Binary files a/prisma/dev.db and b/prisma/dev.db differ diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 0192fdb..bdf9ac4 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -30,7 +30,7 @@ model Session { id String @id @default(uuid()) expiresAt DateTime createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt + updatedAt DateTime @default(now()) @updatedAt userId String user User @relation(references: [id], fields: [userId]) } @@ -41,7 +41,7 @@ model Post { title String content String published Boolean? @default(false) - author User @relation(fields: [authorId], references: [id]) + author User @relation(references: [id], fields: [authorId]) authorId String createdAt DateTime @default(now()) updatedAt DateTime @default(now()) @updatedAt diff --git a/src/lib/server/lucia.ts b/src/lib/server/lucia.ts index f93f932..79cf54f 100644 --- a/src/lib/server/lucia.ts +++ b/src/lib/server/lucia.ts @@ -1,32 +1,32 @@ -import { Lucia } from "lucia"; -import { PrismaAdapter } from "@lucia-auth/adapter-prisma"; -import { PrismaClient } from "@prisma/client"; +import { Lucia } from 'lucia'; +import { PrismaAdapter } from '@lucia-auth/adapter-prisma'; +import { prisma } from '$lib/server/prisma'; -const client = new PrismaClient(); -const adapter = new PrismaAdapter(client.session, client.user) +const client = prisma; +const adapter = new PrismaAdapter(client.session, client.user); // expect error (see next section) export const auth = new Lucia(adapter, { - sessionCookie: { - attributes: { - secure: process.env.NODE_ENV === "production" - } - }, - getUserAttributes: (attributes)=>{ - return { - email: attributes.email - } - } + sessionCookie: { + attributes: { + secure: process.env.NODE_ENV === 'production' + } + }, + getUserAttributes: (attributes) => { + return { + email: attributes.email + }; + } }); -declare module "lucia" { - interface Register { - Lucia: typeof Lucia; - DatabaseUserAttributes: DatabaseUserAttributes - } +declare module 'lucia' { + interface Register { + Lucia: typeof Lucia; + DatabaseUserAttributes: DatabaseUserAttributes; + } } interface DatabaseUserAttributes { - email: string + email: string; } -export type Auth = typeof auth; \ No newline at end of file +export type Auth = typeof auth; diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index 145d816..7c3b840 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -1,6 +1,6 @@ import { prisma } from '$lib/server/prisma'; -import type { Session } from 'lucia'; -export async function load(event: Session) { +export async function load(event) { + console.log(event.userId); const userId = event.userId; if (!userId) { return { diff --git a/src/routes/login/+page.server.ts b/src/routes/login/+page.server.ts index 6c6e9a1..dc69fae 100644 --- a/src/routes/login/+page.server.ts +++ b/src/routes/login/+page.server.ts @@ -1,7 +1,7 @@ import { logger } from '$lib/server/logger'; import { prisma } from '$lib/server/prisma'; import { error, redirect, type Actions } from '@sveltejs/kit'; -import { Argon2id } from "oslo/password" +import { Argon2id } from 'oslo/password'; import { auth } from '$lib/server/lucia.js'; export const actions = { @@ -19,12 +19,12 @@ export const actions = { logger.error('User not found! ${user}'); return error(401); } - const pw = form.get('password') as string; - if(!pw) { - return error(401, 'Password is required') + const password = form.get('password') as string; + if (!password) { + return error(401, 'Password is required'); } - const validPassword = await new Argon2id().verify(user.password,pw); - if(!validPassword) { + const validPassword = await new Argon2id().verify(user.password, password); + if (!validPassword) { return error(400, 'Password is incorrect!'); } const session = await auth.createSession(user.id, []); @@ -36,14 +36,13 @@ export const actions = { redirect(302, '/'); }, - - register: async (event) => { const form = await event.request.formData(); if (!form.has('email') || !form.has('name') || !form.has('password')) { return error(400); } - const hashedPassword = await new Argon2id().hash(form.get('password') as string) + const password = form.get('password') as string; + const hashedPassword = await new Argon2id().hash(password); const user = await prisma.user.create({ data: { email: form.get('email') as string, @@ -61,6 +60,5 @@ export const actions = { maxAge: 120 }); redirect(302, '/'); - } -} satisfies Actions; \ No newline at end of file +} satisfies Actions;