summary refs log tree commit diff
path: root/src/dto
diff options
context:
space:
mode:
Diffstat (limited to 'src/dto')
-rw-r--r--src/dto/auth/AuthDto.js (renamed from src/dto/auth/LoginDto.js)20
-rw-r--r--src/dto/auth/DeviceDto.js36
-rw-r--r--src/dto/auth/WhoAmIDto.js26
-rw-r--r--src/dto/auth/index.js2
4 files changed, 76 insertions, 8 deletions
diff --git a/src/dto/auth/LoginDto.js b/src/dto/auth/AuthDto.js

index b675b4d..14e09ae 100644 --- a/src/dto/auth/LoginDto.js +++ b/src/dto/auth/AuthDto.js
@@ -1,7 +1,10 @@ import { SafeNSoundError } from '#util/error.js'; import Joi from 'joi'; -export class LoginDto { +/** + * Generic authentication DTO. + */ +export class AuthDto { static schema = new Joi.object({ username: Joi.string().required(), email: Joi.string().email().required(), @@ -9,16 +12,19 @@ export class LoginDto { }).or('username', 'email'); username; + email; password; - async Create({ username, email, password }) { - this.username = username ?? email; - this.password = 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 LoginDto.schema.validateAsync(this, { - abortEarly: true - }); + return await AuthDto.schema.validateAsync(obj); } catch (e) { console.log(e); throw new SafeNSoundError({ 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/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<WhoAmIDto>} + */ + 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';