Replace mysql2 with prisma

also I updated packages,
and properly typed api input
a lot of time was spent, I don't remeber what really I did x3
but everything was related to replacing mysql2 with prisma
This commit is contained in:
Wroclaw 2023-11-08 05:35:48 +01:00
parent be1e3909b6
commit eebf25198d
39 changed files with 1081 additions and 1292 deletions

View file

@ -1,7 +1,7 @@
import crypto from "crypto";
import { defineEventHandler, getCookie, setCookie, readBody } from "h3";
import { database, type data } from "../utils/database";
import { database } from "../utils/database";
import { isString } from "../utils/isString";
import { cookieSettings } from "../utils/rootUtils";
import Snowflake from "~/utils/snowflake";
@ -28,19 +28,26 @@ export default defineEventHandler(async (e) => {
const hashedPassword = getPasswordHash(password);
const [account] = await database.query(
"SELECT CONVERT(`id`, CHAR(32)) AS `id` from `users` WHERE `username` = ? AND `password` = ? LIMIT 1",
[login, hashedPassword],
)as unknown as data<{id: string}>;
const account = await database.user.findUnique({
where: {
username: login,
password: hashedPassword,
},
select: {
id: true,
},
});
if (account.length === 0) throw createError({ statusCode: 400, message: "Invalid username or password." });
if (account === null) throw createError({ statusCode: 400, message: "Invalid username or password." });
const sessionId = new Snowflake().toString();
const sessionId = new Snowflake();
await database.query(
"INSERT INTO `sessions` (`id`, `user`) VALUES ( ? , ? )",
[sessionId, account[0].id],
);
setCookie(e, "token", sessionId, cookieSettings);
return { message: "Login successful", token: sessionId };
await database.session.create({
data: {
id: sessionId.state,
userId: account.id,
},
});
setCookie(e, "token", sessionId.toString(), cookieSettings);
return { message: "Login successful", token: sessionId.toString() };
});