Files
2025-09-21 17:11:02 +01:00

49 lines
1.2 KiB
TypeScript

import bcrypt from 'bcryptjs';
import { db } from '@/lib/db';
import { users } from '@/lib/db/schema';
import { eq } from 'drizzle-orm';
import { generateId } from '@/lib/utils';
export async function hashPassword(password: string): Promise<string> {
return bcrypt.hash(password, 12);
}
export async function verifyPassword(password: string, hashedPassword: string): Promise<boolean> {
return bcrypt.compare(password, hashedPassword);
}
export async function createUser(data: {
email: string;
name: string;
surname: string;
password: string;
role?: 'user' | 'admin';
}) {
const hashedPassword = await hashPassword(data.password);
const now = new Date();
const newUser = {
id: generateId(),
email: data.email.toLowerCase(),
name: data.name,
surname: data.surname,
password: hashedPassword,
role: data.role || 'user',
createdAt: now,
updatedAt: now,
};
const [user] = await db.insert(users).values(newUser).returning();
return user;
}
export async function getUserByEmail(email: string) {
const [user] = await db.select().from(users).where(eq(users.email, email.toLowerCase()));
return user;
}
export async function getUserById(id: string) {
const [user] = await db.select().from(users).where(eq(users.id, id));
return user;
}