fixed all commented issues

This commit is contained in:
Dan Mihailescu 2024-12-18 10:11:29 -05:00
parent 87921c24a0
commit ea0ce134fe
5 changed files with 35 additions and 37 deletions

Binary file not shown.

View file

@ -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

View file

@ -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;
export type Auth = typeof auth;

View file

@ -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 {

View file

@ -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;
} satisfies Actions;