Lucia register done

This commit is contained in:
Dan Mihailescu 2024-12-15 17:29:28 -05:00
parent ec963ef4cb
commit a6c6b4a7a7
10 changed files with 181 additions and 12 deletions

6
src/app.d.ts vendored
View file

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

View file

@ -8,7 +8,7 @@ const adapter = new PrismaAdapter(client.session, client.user)
export const auth = new Lucia(adapter, {
sessionCookie: {
attributes: {
// secure: process.env.NODE_ENV === "production"
secure: process.env.NODE_ENV === "production"
}
},
getUserAttributes: (attributes)=>{

View file

@ -1,6 +1,10 @@
import { logger } from '$lib/server/logger';
import { prisma } from '$lib/server/prisma';
import { error, redirect, type Actions } from '@sveltejs/kit';
//import { password } from 'bun';
import { Argon2id } from "oslo/password"
import { generateId } from 'lucia';
import { auth } from '$lib/server/lucia.js';
export const actions = {
login: async (event) => {
@ -25,22 +29,27 @@ export const actions = {
},
register: async (event) => {
const form = await event.request.formData();
if (!form.has('email') || !form.has('name')) {
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 user = await prisma.user.create({
data: {
email: form.get('email') as string,
name: form.get('name') as string
name: form.get('name') as string,
password: hashedPassword
}
});
const session = await auth.createSession(user.id.toString(), {});
const sessionCookie = auth.createSessionCookie(session.id);
if (!user) {
return error(500);
}
event.cookies.set('user', String(user.id), {
event.cookies.set(sessionCookie.name, sessionCookie.value, {
path: '/',
maxAge: 120
});
redirect(302, '/');
}
} satisfies Actions;