translaite/app/models/user.server.ts

95 lines
1.9 KiB
TypeScript

import type { Password, User } from "@prisma/client";
import bcrypt from "bcryptjs";
import { prisma } from "~/db.server";
export type { User } from "@prisma/client";
export async function countUsers() {
return prisma.user.count();
}
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,
},
},
},
});
}
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,
});
}
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;
}