Lucia Authentication #8
BIN
prisma/dev.db
|
|
@ -30,7 +30,7 @@ model Session {
|
||||||
id String @id @default(uuid())
|
id String @id @default(uuid())
|
||||||
expiresAt DateTime
|
expiresAt DateTime
|
||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
updatedAt DateTime @updatedAt
|
updatedAt DateTime @default(now()) @updatedAt
|
||||||
userId String
|
userId String
|
||||||
user User @relation(references: [id], fields: [userId])
|
user User @relation(references: [id], fields: [userId])
|
||||||
}
|
}
|
||||||
|
|
@ -41,7 +41,7 @@ model Post {
|
||||||
title String
|
title String
|
||||||
content String
|
content String
|
||||||
published Boolean? @default(false)
|
published Boolean? @default(false)
|
||||||
author User @relation(fields: [authorId], references: [id])
|
author User @relation(references: [id], fields: [authorId])
|
||||||
authorId String
|
authorId String
|
||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
updatedAt DateTime @default(now()) @updatedAt
|
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";
|
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...
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";
|
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...
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";
|
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...
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();
|
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...
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)
|
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...
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)
|
// 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'
|
||||||
|
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...
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
getUserAttributes: (attributes)=>{
|
getUserAttributes: (attributes) => {
|
||||||
|
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...
|
|||||||
return {
|
return {
|
||||||
email: attributes.email
|
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" {
|
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...
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 {
|
interface Register {
|
||||||
Lucia: typeof Lucia;
|
Lucia: typeof Lucia;
|
||||||
DatabaseUserAttributes: DatabaseUserAttributes
|
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...
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 {
|
interface DatabaseUserAttributes {
|
||||||
email: string
|
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...
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;
|
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 { prisma } from '$lib/server/prisma';
|
||||||
import type { Session } from 'lucia';
|
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
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) {
|
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
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;
|
const userId = event.userId;
|
||||||
if (!userId) {
|
if (!userId) {
|
||||||
return {
|
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 { 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,13 @@ 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 password = form.get('password') as string;
|
||||||
|
const hashedPassword = await new Argon2id().hash(password);
|
||||||
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 +60,5 @@ export const actions = {
|
||||||
maxAge: 120
|
maxAge: 120
|
||||||
});
|
});
|
||||||
redirect(302, '/');
|
redirect(302, '/');
|
||||||
|
|
||||||
}
|
}
|
||||||
} satisfies Actions;
|
} satisfies Actions;
|
||||||
Reuse the
$lib/server/prismaclient plz