From a22c00fcefa10a99505c05393106fb3a655de243 Mon Sep 17 00:00:00 2001 From: Rory& Date: Sun, 1 Jun 2025 04:40:09 +0200 Subject: Add register with validation --- src/db/dbAccess/user.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'src/db/dbAccess/user.js') diff --git a/src/db/dbAccess/user.js b/src/db/dbAccess/user.js index 413b1cf..6301cb5 100644 --- a/src/db/dbAccess/user.js +++ b/src/db/dbAccess/user.js @@ -1,23 +1,25 @@ import { hash, compare } from 'bcrypt'; import { DbUser } from '#db/schemas/index.js'; +import { RegisterDto } from '#dto/auth/index.js'; -export async function registerUser(username, password, email, type = 'user') { - if (!username || !password || !email) { - throw new Error( - 'Username, password, and email are required to register a user.' - ); - } +/** + * @param data {RegisterDto} + * @returns {Promise<(Error | HydratedDocument, ObtainSchemaGeneric & ObtainSchemaGeneric, ObtainSchemaGeneric, ObtainSchemaGeneric>)[]>} + */ +export async function registerUser(data) { + if (!(data instanceof RegisterDto)) + throw new Error('Invalid data type. Expected RegisterDto.'); - const passwordHash = await hash(password, 10); + const passwordHash = await hash(data.password, 10); if (!passwordHash) { throw new Error('Failed to hash password.'); } return DbUser.create({ - username, + username: data.username, passwordHash, - email, - type + email: data.email, + type: data.type }); } -- cgit 1.5.1