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 +++++++++++++++++++++++++++++++++++++ src/dto/auth/DeviceDto.js | 36 ++++++++++++++++++++++++++++++++++++ src/dto/auth/LoginDto.js | 31 ------------------------------- src/dto/auth/WhoAmIDto.js | 26 ++++++++++++++++++++++++++ src/dto/auth/index.js | 2 +- 5 files changed, 100 insertions(+), 32 deletions(-) create mode 100644 src/dto/auth/AuthDto.js create mode 100644 src/dto/auth/DeviceDto.js delete mode 100644 src/dto/auth/LoginDto.js create mode 100644 src/dto/auth/WhoAmIDto.js (limited to 'src/dto') 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 + }); + } + } +} diff --git a/src/dto/auth/DeviceDto.js b/src/dto/auth/DeviceDto.js new file mode 100644 index 0000000..40f1959 --- /dev/null +++ b/src/dto/auth/DeviceDto.js @@ -0,0 +1,36 @@ +import { SafeNSoundError } from '#util/error.js'; +import Joi from 'joi'; + +export class RegisterDto { + static schema = new Joi.object({ + username: Joi.string().required(), + email: Joi.string().email().required(), + password: Joi.string().required(), + type: Joi.string().valid('user', 'monitor', 'admin').required() + }); + + username; + email; + password; + type = 'user'; + + static async create(data) { + const obj = new RegisterDto(); + for (const key of Object.keys(data)) { + if (key in obj) { + obj[key] = data[key]; + } + } + + try { + return await RegisterDto.schema.validateAsync(obj); + } catch (e) { + console.log(e); + throw new SafeNSoundError({ + errCode: 'JOI_VALIDATION_ERROR', + message: e.message, + validation_details: e.details + }); + } + } +} diff --git a/src/dto/auth/LoginDto.js b/src/dto/auth/LoginDto.js deleted file mode 100644 index b675b4d..0000000 --- a/src/dto/auth/LoginDto.js +++ /dev/null @@ -1,31 +0,0 @@ -import { SafeNSoundError } from '#util/error.js'; -import Joi from 'joi'; - -export class LoginDto { - static schema = new Joi.object({ - username: Joi.string().required(), - email: Joi.string().email().required(), - password: Joi.string().required() - }).or('username', 'email'); - - username; - password; - - async Create({ username, email, password }) { - this.username = username ?? email; - this.password = password; - - try { - return await LoginDto.schema.validateAsync(this, { - abortEarly: true - }); - } catch (e) { - console.log(e); - throw new SafeNSoundError({ - errCode: 'JOI_VALIDATION_ERROR', - message: e.message, - validation_details: e.details - }); - } - } -} diff --git a/src/dto/auth/WhoAmIDto.js b/src/dto/auth/WhoAmIDto.js new file mode 100644 index 0000000..ae1795a --- /dev/null +++ b/src/dto/auth/WhoAmIDto.js @@ -0,0 +1,26 @@ +import { SafeNSoundError } from '#util/error.js'; +import Joi from 'joi'; + +/** + * Generic authentication DTO. + */ +export class WhoAmIDto { + userId; + username; + deviceId; + + /** + * @param data {WhoAmIDto} + * @returns {Promise} + */ + static async create(data) { + const obj = new WhoAmIDto(); + for (const key of Object.keys(data)) { + if (key in obj) { + obj[key] = data[key]; + } + } + + return obj; + } +} diff --git a/src/dto/auth/index.js b/src/dto/auth/index.js index 6d57d5e..aa1d435 100644 --- a/src/dto/auth/index.js +++ b/src/dto/auth/index.js @@ -1,2 +1,2 @@ -export * from './LoginDto.js'; +export * from './AuthDto.js'; export * from './RegisterDto.js'; -- cgit 1.5.1