This commit is contained in:
Dan Mihailescu 2024-12-15 18:17:02 -05:00
parent f0f32d71fc
commit ce0d940953
35 changed files with 62 additions and 66 deletions

3
src/app.d.ts vendored
View file

@ -2,10 +2,9 @@
// for information about these interfaces
declare global {
namespace App {
// interface Error {}
interface Locals {
user: import("lucia").User | null;
user: import('lucia').User | null;
session: import('lucia').Session | null;
}
// interface PageData {}

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 { PrismaClient } from '@prisma/client';
const client = new PrismaClient();
const adapter = new PrismaAdapter(client.session, client.user)
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"
secure: process.env.NODE_ENV === 'production'
}
},
getUserAttributes: (attributes)=>{
getUserAttributes: (attributes) => {
return {
email: attributes.email
}
};
}
});
declare module "lucia" {
declare module 'lucia' {
interface Register {
Lucia: typeof Lucia;
DatabaseUserAttributes: DatabaseUserAttributes
DatabaseUserAttributes: DatabaseUserAttributes;
}
}
interface DatabaseUserAttributes {
email: string
email: string;
}
export type Auth = typeof auth;

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,12 @@ 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 hashedPassword = await new Argon2id().hash(form.get('password') as string);
const user = await prisma.user.create({
data: {
email: form.get('email') as string,
@ -61,6 +59,5 @@ export const actions = {
maxAge: 120
});
redirect(302, '/');
}
} satisfies Actions;