From 0ca7c01bc4a6c5ab50ac80f9a8e5d5c5db442f45 Mon Sep 17 00:00:00 2001 From: Rory& Date: Sun, 1 Jun 2025 08:04:30 +0200 Subject: Register works, part of login and auth middleware --- src/dto/auth/AuthDto.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/dto/auth/AuthDto.js (limited to 'src/dto/auth/AuthDto.js') diff --git a/src/dto/auth/AuthDto.js b/src/dto/auth/AuthDto.js new file mode 100644 index 0000000..14e09ae --- /dev/null +++ b/src/dto/auth/AuthDto.js @@ -0,0 +1,37 @@ +import { SafeNSoundError } from '#util/error.js'; +import Joi from 'joi'; + +/** + * Generic authentication DTO. + */ +export class AuthDto { + static schema = new Joi.object({ + username: Joi.string().required(), + email: Joi.string().email().required(), + password: Joi.string().required() + }).or('username', 'email'); + + username; + email; + password; + + static async create(data) { + const obj = new AuthDto(); + for (const key of Object.keys(data)) { + if (key in obj) { + obj[key] = data[key]; + } + } + + try { + return await AuthDto.schema.validateAsync(obj); + } catch (e) { + console.log(e); + throw new SafeNSoundError({ + errCode: 'JOI_VALIDATION_ERROR', + message: e.message, + validation_details: e.details + }); + } + } +} -- cgit 1.5.1