2025-12-30 13:38:01 +01:00
|
|
|
import {SignUpUser} from "@/types/auth";
|
|
|
|
|
import {hashPassword} from "better-auth/crypto";
|
|
|
|
|
import {db} from "@/db";
|
|
|
|
|
import * as drizzleDb from "@/db";
|
|
|
|
|
import {User, UserThemeEnum} from "@/db/schema/02_user";
|
2026-05-21 07:28:05 +02:00
|
|
|
import {assertValidPassword} from "@/utils/password";
|
2025-12-30 13:38:01 +01:00
|
|
|
|
|
|
|
|
|
2026-06-22 10:52:42 +02:00
|
|
|
export async function hasUsers(): Promise<boolean> {
|
|
|
|
|
const result = await db.select().from(drizzleDb.schemas.user).limit(1);
|
|
|
|
|
return result.length > 0;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-30 13:38:01 +01:00
|
|
|
export async function createUserDb(data: SignUpUser): Promise<User> {
|
2026-05-21 07:28:05 +02:00
|
|
|
assertValidPassword(data.password);
|
|
|
|
|
|
2025-12-30 13:38:01 +01:00
|
|
|
const now = new Date();
|
|
|
|
|
const userId = crypto.randomUUID();
|
|
|
|
|
|
|
|
|
|
const [newUser] = await db.insert(drizzleDb.schemas.user).values({
|
2026-05-21 07:28:05 +02:00
|
|
|
...data,
|
2025-12-30 13:38:01 +01:00
|
|
|
id: userId,
|
|
|
|
|
name: data.name,
|
|
|
|
|
email: data.email,
|
|
|
|
|
emailVerified: true,
|
|
|
|
|
role: data.role,
|
|
|
|
|
createdAt: now,
|
|
|
|
|
updatedAt: now,
|
|
|
|
|
theme: data.theme as UserThemeEnum,
|
|
|
|
|
}).returning();
|
|
|
|
|
|
2026-02-24 16:49:16 +01:00
|
|
|
if (data.password) {
|
|
|
|
|
const hashedPassword = await hashPassword(data.password);
|
|
|
|
|
await db.insert(drizzleDb.schemas.account).values({
|
|
|
|
|
providerId: "credential",
|
|
|
|
|
accountId: userId,
|
|
|
|
|
userId: userId,
|
|
|
|
|
password: hashedPassword,
|
|
|
|
|
createdAt: now,
|
|
|
|
|
updatedAt: now,
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-12-30 13:38:01 +01:00
|
|
|
|
|
|
|
|
return newUser
|
|
|
|
|
}
|