2023-08-07 10:52:44 +02:00
|
|
|
import type { Password, User } from "@prisma/client";
|
|
|
|
|
import bcrypt from "bcryptjs";
|
|
|
|
|
|
|
|
|
|
import { prisma } from "~/db.server";
|
|
|
|
|
|
|
|
|
|
export type { User } from "@prisma/client";
|
|
|
|
|
|
2023-08-07 13:35:07 +02:00
|
|
|
export async function countUsers() {
|
|
|
|
|
return prisma.user.count();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 10:52:44 +02:00
|
|
|
export async function getUserById(id: User["id"]) {
|
|
|
|
|
return prisma.user.findUnique({ where: { id } });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getUserByEmail(email: User["email"]) {
|
|
|
|
|
return prisma.user.findUnique({ where: { email } });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function createUser(email: User["email"], password: string) {
|
|
|
|
|
const hashedPassword = await bcrypt.hash(password, 10);
|
|
|
|
|
|
|
|
|
|
return prisma.user.create({
|
|
|
|
|
data: {
|
|
|
|
|
email,
|
|
|
|
|
password: {
|
|
|
|
|
create: {
|
|
|
|
|
hash: hashedPassword,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 11:21:43 +02:00
|
|
|
export async function updateUser(
|
|
|
|
|
email: User["email"],
|
|
|
|
|
data: {
|
|
|
|
|
openAIKey?: User["openAIKey"];
|
|
|
|
|
password?: string;
|
|
|
|
|
}
|
|
|
|
|
) {
|
|
|
|
|
let userData: { [key: string]: any } = {};
|
|
|
|
|
if (data.openAIKey) {
|
|
|
|
|
userData.openAIKey = data.openAIKey;
|
|
|
|
|
}
|
|
|
|
|
if (data.password) {
|
|
|
|
|
const hashedPassword = await bcrypt.hash(data.password, 10);
|
|
|
|
|
userData.password = {
|
|
|
|
|
create: {
|
|
|
|
|
hash: hashedPassword,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Object.values(userData).length === 0) return;
|
|
|
|
|
|
|
|
|
|
return prisma.user.update({
|
|
|
|
|
where: { email },
|
|
|
|
|
data: userData,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 10:52:44 +02:00
|
|
|
export async function deleteUserByEmail(email: User["email"]) {
|
|
|
|
|
return prisma.user.delete({ where: { email } });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function verifyLogin(
|
|
|
|
|
email: User["email"],
|
|
|
|
|
password: Password["hash"]
|
|
|
|
|
) {
|
|
|
|
|
const userWithPassword = await prisma.user.findUnique({
|
|
|
|
|
where: { email },
|
|
|
|
|
include: {
|
|
|
|
|
password: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!userWithPassword || !userWithPassword.password) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const isValid = await bcrypt.compare(
|
|
|
|
|
password,
|
|
|
|
|
userWithPassword.password.hash
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!isValid) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { password: _password, ...userWithoutPassword } = userWithPassword;
|
|
|
|
|
|
|
|
|
|
return userWithoutPassword;
|
|
|
|
|
}
|