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 // for information about these interfaces
declare global { declare global {
namespace App { namespace App {
// interface Error {} // interface Error {}
interface Locals { interface Locals {
user: import("lucia").User | null; user: import('lucia').User | null;
session: import('lucia').Session | null; session: import('lucia').Session | null;
} }
// interface PageData {} // interface PageData {}

View file

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

View file

@ -1,7 +1,7 @@
import { logger } from '$lib/server/logger'; import { logger } from '$lib/server/logger';
import { prisma } from '$lib/server/prisma'; import { prisma } from '$lib/server/prisma';
import { error, redirect, type Actions } from '@sveltejs/kit'; 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'; import { auth } from '$lib/server/lucia.js';
export const actions = { export const actions = {
@ -19,12 +19,12 @@ export const actions = {
logger.error('User not found! ${user}'); logger.error('User not found! ${user}');
return error(401); return error(401);
} }
const pw = form.get('password') as string; const password = form.get('password') as string;
if(!pw) { if (!password) {
return error(401, 'Password is required') return error(401, 'Password is required');
} }
const validPassword = await new Argon2id().verify(user.password,pw); const validPassword = await new Argon2id().verify(user.password, password);
if(!validPassword) { if (!validPassword) {
return error(400, 'Password is incorrect!'); return error(400, 'Password is incorrect!');
} }
const session = await auth.createSession(user.id, []); const session = await auth.createSession(user.id, []);
@ -36,14 +36,12 @@ export const actions = {
redirect(302, '/'); redirect(302, '/');
}, },
register: async (event) => { register: async (event) => {
const form = await event.request.formData(); const form = await event.request.formData();
if (!form.has('email') || !form.has('name') || !form.has('password')) { if (!form.has('email') || !form.has('name') || !form.has('password')) {
return error(400); 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({ const user = await prisma.user.create({
data: { data: {
email: form.get('email') as string, email: form.get('email') as string,
@ -61,6 +59,5 @@ export const actions = {
maxAge: 120 maxAge: 120
}); });
redirect(302, '/'); redirect(302, '/');
} }
} satisfies Actions; } satisfies Actions;