Lucia Authentication (#8)
* initial lucia installation * added prismAdapter for lucia * fixed adapter * main lucia set * moved into correct folder * fixed * removed npm lock * removed supabase(i swear) * Lucia register done * lucia login done * removed * fixed issues with uuid * fixed all commented issues * fixed event param * Update +page.server.ts Signed-off-by: DanMihailescu <as42554525@yahoo.ca> * Update lucia.ts Signed-off-by: DanMihailescu <as42554525@yahoo.ca> --------- Signed-off-by: DanMihailescu <as42554525@yahoo.ca> Co-authored-by: Dan Mihailescu <dan.mihailescu5@gmail.com>
This commit is contained in:
parent
242ec113f6
commit
992eb07f5c
11 changed files with 108 additions and 72 deletions
6
src/app.d.ts
vendored
6
src/app.d.ts
vendored
|
|
@ -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 {}
|
||||
|
|
|
|||
31
src/lib/server/lucia.ts
Normal file
31
src/lib/server/lucia.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import { Lucia } from 'lucia';
|
||||
import { PrismaAdapter } from '@lucia-auth/adapter-prisma';
|
||||
import { prisma } from '$lib/server/prisma';
|
||||
|
||||
const adapter = new PrismaAdapter(prisma.session, prisma.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
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
declare module 'lucia' {
|
||||
interface Register {
|
||||
Lucia: typeof Lucia;
|
||||
DatabaseUserAttributes: DatabaseUserAttributes;
|
||||
}
|
||||
}
|
||||
|
||||
interface DatabaseUserAttributes {
|
||||
email: string;
|
||||
}
|
||||
|
||||
export type Auth = typeof auth;
|
||||
|
|
@ -1,18 +1,17 @@
|
|||
import { prisma } from '$lib/server/prisma';
|
||||
|
||||
export async function load(event) {
|
||||
const userId = event.cookies.get('user');
|
||||
if (!userId && isNaN(Number(userId))) {
|
||||
if (!userId) {
|
||||
return {
|
||||
authenticated: false
|
||||
};
|
||||
}
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: Number(userId)
|
||||
id: userId
|
||||
}
|
||||
});
|
||||
return {
|
||||
authenticated: !!user
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
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 { auth } from '$lib/server/lucia.js';
|
||||
|
||||
export const actions = {
|
||||
login: async (event) => {
|
||||
|
|
@ -17,30 +19,46 @@ export const actions = {
|
|||
logger.error('User not found! ${user}');
|
||||
return error(401);
|
||||
}
|
||||
event.cookies.set('user', String(user.id), {
|
||||
const password = form.get('password') as string;
|
||||
if (!password) {
|
||||
return error(401, 'Password is required');
|
||||
}
|
||||
const validPassword = await new Argon2id().verify(user.password, password);
|
||||
if (!validPassword) {
|
||||
return error(400, 'Password is incorrect!');
|
||||
}
|
||||
const session = await auth.createSession(user.id, []);
|
||||
const sessionCookie = auth.createSessionCookie(session.id);
|
||||
event.cookies.set(sessionCookie.name, sessionCookie.value, {
|
||||
path: '/',
|
||||
maxAge: 120
|
||||
});
|
||||
redirect(302, '/');
|
||||
},
|
||||
|
||||
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 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,
|
||||
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;
|
||||
} satisfies Actions;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue