Lucia Authentication #8
BIN
prisma/dev.db
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,32 +1,32 @@
|
|||
|
You dont need to set it to a const... You dont need to set it to a const...
Reuse the Reuse the `$lib/server/prisma` client plz
You dont need to set it to a const... You dont need to set it to a const...
|
||||
import { Lucia } from "lucia";
|
||||
|
Reuse the Reuse the `$lib/server/prisma` client plz
You dont need to set it to a const... You dont need to set it to a const...
|
||||
import { PrismaAdapter } from "@lucia-auth/adapter-prisma";
|
||||
|
Reuse the Reuse the `$lib/server/prisma` client plz
You dont need to set it to a const... You dont need to set it to a const...
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
|
Reuse the Reuse the `$lib/server/prisma` client plz
You dont need to set it to a const... You dont need to set it to a const...
|
||||
import { Lucia } from 'lucia';
|
||||
|
Reuse the Reuse the `$lib/server/prisma` client plz
You dont need to set it to a const... You dont need to set it to a const...
|
||||
import { PrismaAdapter } from '@lucia-auth/adapter-prisma';
|
||||
|
Reuse the Reuse the `$lib/server/prisma` client plz
You dont need to set it to a const... You dont need to set it to a const...
|
||||
import { prisma } from '$lib/server/prisma';
|
||||
|
Reuse the Reuse the `$lib/server/prisma` client plz
You dont need to set it to a const... You dont need to set it to a const...
|
||||
|
||||
const client = new PrismaClient();
|
||||
|
Reuse the Reuse the `$lib/server/prisma` client plz
You dont need to set it to a const... You dont need to set it to a const...
|
||||
const adapter = new PrismaAdapter(client.session, client.user)
|
||||
|
Reuse the Reuse the `$lib/server/prisma` client plz
You dont need to set it to a const... You dont need to set it to a const...
|
||||
const client = prisma;
|
||||
|
Reuse the Reuse the `$lib/server/prisma` client plz
You dont need to set it to a const... You dont need to set it to a const...
|
||||
const adapter = new PrismaAdapter(client.session, client.user);
|
||||
|
Reuse the Reuse the `$lib/server/prisma` client plz
You dont need to set it to a const... You dont need to set it to a const...
|
||||
// expect error (see next section)
|
||||
export const auth = new Lucia(adapter, {
|
||||
sessionCookie: {
|
||||
attributes: {
|
||||
secure: process.env.NODE_ENV === "production"
|
||||
|
Reuse the Reuse the `$lib/server/prisma` client plz
You dont need to set it to a const... You dont need to set it to a const...
|
||||
secure: process.env.NODE_ENV === 'production'
|
||||
|
Reuse the Reuse the `$lib/server/prisma` client plz
You dont need to set it to a const... You dont need to set it to a const...
|
||||
}
|
||||
},
|
||||
getUserAttributes: (attributes) => {
|
||||
return {
|
||||
email: attributes.email
|
||||
}
|
||||
|
Reuse the Reuse the `$lib/server/prisma` client plz
You dont need to set it to a const... You dont need to set it to a const...
|
||||
};
|
||||
|
Reuse the Reuse the `$lib/server/prisma` client plz
You dont need to set it to a const... You dont need to set it to a const...
|
||||
}
|
||||
});
|
||||
|
||||
declare module "lucia" {
|
||||
|
Reuse the Reuse the `$lib/server/prisma` client plz
You dont need to set it to a const... You dont need to set it to a const...
|
||||
declare module 'lucia' {
|
||||
|
Reuse the Reuse the `$lib/server/prisma` client plz
You dont need to set it to a const... You dont need to set it to a const...
|
||||
interface Register {
|
||||
Lucia: typeof Lucia;
|
||||
DatabaseUserAttributes: DatabaseUserAttributes
|
||||
|
Reuse the Reuse the `$lib/server/prisma` client plz
You dont need to set it to a const... You dont need to set it to a const...
|
||||
DatabaseUserAttributes: DatabaseUserAttributes;
|
||||
|
Reuse the Reuse the `$lib/server/prisma` client plz
You dont need to set it to a const... You dont need to set it to a const...
|
||||
}
|
||||
}
|
||||
|
||||
interface DatabaseUserAttributes {
|
||||
email: string
|
||||
|
Reuse the Reuse the `$lib/server/prisma` client plz
You dont need to set it to a const... You dont need to set it to a const...
|
||||
email: string;
|
||||
|
Reuse the Reuse the `$lib/server/prisma` client plz
You dont need to set it to a const... You dont need to set it to a const...
|
||||
}
|
||||
|
||||
export type Auth = typeof auth;
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
|
I dont think you can type event as a a Session, from the app typedefs you made it should be event.locals.[session or user idk] I dont think you can type event as a a Session, from the app typedefs you made it should be _event.locals.[session or user idk]_
https://github.com/Machinata/hestia/pull/8/files#diff-b07092d78bfaa239d6ad0d01b0897c3ef49533b08512622d83c6f4e1dd3b363e
switched back to any type switched back to any type
Use the logger pls Use the logger pls
I dont think you can type event as a a Session, from the app typedefs you made it should be event.locals.[session or user idk] I dont think you can type event as a a Session, from the app typedefs you made it should be _event.locals.[session or user idk]_
https://github.com/Machinata/hestia/pull/8/files#diff-b07092d78bfaa239d6ad0d01b0897c3ef49533b08512622d83c6f4e1dd3b363e
switched back to any type switched back to any type
Use the logger pls Use the logger pls
|
||||
import { prisma } from '$lib/server/prisma';
|
||||
import type { Session } from 'lucia';
|
||||
|
I dont think you can type event as a a Session, from the app typedefs you made it should be event.locals.[session or user idk] I dont think you can type event as a a Session, from the app typedefs you made it should be _event.locals.[session or user idk]_
https://github.com/Machinata/hestia/pull/8/files#diff-b07092d78bfaa239d6ad0d01b0897c3ef49533b08512622d83c6f4e1dd3b363e
switched back to any type switched back to any type
Use the logger pls Use the logger pls
|
||||
export async function load(event: Session) {
|
||||
|
I dont think you can type event as a a Session, from the app typedefs you made it should be event.locals.[session or user idk] I dont think you can type event as a a Session, from the app typedefs you made it should be _event.locals.[session or user idk]_
https://github.com/Machinata/hestia/pull/8/files#diff-b07092d78bfaa239d6ad0d01b0897c3ef49533b08512622d83c6f4e1dd3b363e
switched back to any type switched back to any type
Use the logger pls Use the logger pls
|
||||
export async function load(event) {
|
||||
|
I dont think you can type event as a a Session, from the app typedefs you made it should be event.locals.[session or user idk] I dont think you can type event as a a Session, from the app typedefs you made it should be _event.locals.[session or user idk]_
https://github.com/Machinata/hestia/pull/8/files#diff-b07092d78bfaa239d6ad0d01b0897c3ef49533b08512622d83c6f4e1dd3b363e
switched back to any type switched back to any type
Use the logger pls Use the logger pls
|
||||
console.log(event.userId);
|
||||
|
I dont think you can type event as a a Session, from the app typedefs you made it should be event.locals.[session or user idk] I dont think you can type event as a a Session, from the app typedefs you made it should be _event.locals.[session or user idk]_
https://github.com/Machinata/hestia/pull/8/files#diff-b07092d78bfaa239d6ad0d01b0897c3ef49533b08512622d83c6f4e1dd3b363e
switched back to any type switched back to any type
Use the logger pls Use the logger pls
|
||||
const userId = event.userId;
|
||||
if (!userId) {
|
||||
return {
|
||||
|
|
|
|||
|
I dont think you can type event as a a Session, from the app typedefs you made it should be event.locals.[session or user idk] I dont think you can type event as a a Session, from the app typedefs you made it should be _event.locals.[session or user idk]_
https://github.com/Machinata/hestia/pull/8/files#diff-b07092d78bfaa239d6ad0d01b0897c3ef49533b08512622d83c6f4e1dd3b363e
switched back to any type switched back to any type
Use the logger pls Use the logger pls
I dont think you can type event as a a Session, from the app typedefs you made it should be event.locals.[session or user idk] I dont think you can type event as a a Session, from the app typedefs you made it should be _event.locals.[session or user idk]_
https://github.com/Machinata/hestia/pull/8/files#diff-b07092d78bfaa239d6ad0d01b0897c3ef49533b08512622d83c6f4e1dd3b363e
switched back to any type switched back to any type
Use the logger pls Use the logger pls
|
||||
|
|
@ -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,11 +19,11 @@ 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);
|
||||
const validPassword = await new Argon2id().verify(user.password, password);
|
||||
if (!validPassword) {
|
||||
return error(400, 'Password is incorrect!');
|
||||
}
|
||||
|
|
@ -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;
|
||||
Reuse the
$lib/server/prismaclient plz