diff --git a/src/adapters/database/inmemory.ts b/src/adapters/database/inmemory.ts index 5dcdbe3c..032e6f2d 100644 --- a/src/adapters/database/inmemory.ts +++ b/src/adapters/database/inmemory.ts @@ -3,18 +3,14 @@ import packageJson from '@/package.json'; import DatabaseProvider, { DatabaseError } from '~/ports/database'; import { ChartType, Lang } from '~/ports/types'; -import { ROLE } from '~/ports/user/model'; import type { SessionConfig } from 'h3'; +import type { ID } from '~/ports/types'; import type { System } from '~/ports/system/model'; import type { User } from '~/ports/user/model'; -import type { Identity, String } from '~/ports/types'; -import { - hashPasswordWithBcrypt, - isPasswordStrong, -} from '~/server/utils/password'; +import { hashPassword, isPasswordStrong } from '~/server/utils/password'; -const INMDP_DEBUG = debug('InMemoryDP'); +const DEBUG = debug('InMemoryDB'); // Represent in-memory data structure type InMemoryData = { @@ -27,7 +23,7 @@ export default class InMemory extends DatabaseProvider { protected data: InMemoryData = { users: [] }; async connect() { - INMDP_DEBUG('Connection...'); + DEBUG('Connection...'); const system: System = { release: packageJson.release.version, interface: { @@ -74,7 +70,7 @@ export default class InMemory extends DatabaseProvider { }; this.data.system = system; - INMDP_DEBUG('Connection done'); + DEBUG('Connection done'); } async disconnect() { @@ -82,15 +78,10 @@ export default class InMemory extends DatabaseProvider { } async getSystem() { - INMDP_DEBUG('Get System'); + DEBUG('Get System'); return this.data.system; } - async saveSystem(system: System) { - INMDP_DEBUG('Save System'); - this.data.system = system; - } - async getLang() { return this.data.system?.lang || Lang.EN; } @@ -99,18 +90,15 @@ export default class InMemory extends DatabaseProvider { return this.data.users; } - async getUser(id: Identity) { - INMDP_DEBUG('Get User'); - if (typeof id === 'string' || typeof id === 'number') { - return this.data.users.find((user) => user.id === id); - } - return this.data.users.find((user) => user.id === id.id); + async getUser(id: ID) { + DEBUG('Get User'); + return this.data.users.find((user) => user.id === id); } - async newUserWithPassword(username: String, password: String) { - INMDP_DEBUG('New User'); + async newUserWithPassword(username: string, password: string) { + DEBUG('New User'); if (username.length < 8) { - throw new DatabaseError(DatabaseError.ERROR_USERNAME_LEN); + throw new DatabaseError(DatabaseError.ERROR_USERNAME_REQ); } if (!isPasswordStrong(password)) { @@ -125,13 +113,13 @@ export default class InMemory extends DatabaseProvider { } const now = new Date(); - const isUserEmpty = this.data.users.length == 0; + const isUserEmpty = this.data.users.length === 0; const newUser: User = { id: `${this.data.users.length + 1}`, - password: hashPasswordWithBcrypt(password), + password: hashPassword(password), username, - role: isUserEmpty ? ROLE.ADMIN : ROLE.CLIENT, + role: isUserEmpty ? 'ADMIN' : 'CLIENT', enabled: true, createdAt: now, updatedAt: now, @@ -140,18 +128,17 @@ export default class InMemory extends DatabaseProvider { this.data.users.push(newUser); } - async saveUser(user: User) { - let _user = await this.getUser(user); + async updateUser(user: User) { + let _user = await this.getUser(user.id); if (_user) { - INMDP_DEBUG('Update User'); + DEBUG('Update User'); _user = user; } } - async deleteUser(id: Identity) { - INMDP_DEBUG('Delete User'); - const _id = typeof id === 'string' || typeof id === 'number' ? id : id.id; - const idx = this.data.users.findIndex((user) => user.id == _id); + async deleteUser(id: ID) { + DEBUG('Delete User'); + const idx = this.data.users.findIndex((user) => user.id === id); if (idx !== -1) { this.data.users.splice(idx, 1); } diff --git a/src/i18n.config.ts b/src/i18n.config.ts index aa9cad63..4b247f89 100644 --- a/src/i18n.config.ts +++ b/src/i18n.config.ts @@ -46,6 +46,11 @@ export default defineI18nConfig(() => ({ ExpireDate: 'Expire Date', Permanent: 'Permanent', OneTimeLink: 'Generate short one time link', + errorDatabaseConn: 'Failed to connect to the database.', + errorPasswordReq: + 'Password does not meet the strength requirements. It must be at least 12 characters long, with at least one uppercase letter, one lowercase letter, one number, and one special character.', + errorUsernameReq: 'Username must be longer than 8 characters.', + errorUserExist: 'User already exists.', }, ua: { name: 'Ім`я', diff --git a/src/package.json b/src/package.json index 85b2482d..353e40a8 100644 --- a/src/package.json +++ b/src/package.json @@ -35,6 +35,7 @@ "qrcode": "^1.5.4", "tailwindcss": "^3.4.10", "timeago.js": "^4.0.2", + "uuid": "^10.0.0", "vue": "latest", "vue3-apexcharts": "^1.5.3", "zod": "^3.23.8" @@ -44,6 +45,7 @@ "@types/bcryptjs": "^2.4.6", "@types/debug": "^4.1.12", "@types/qrcode": "^1.5.5", + "@types/uuid": "^10.0.0", "eslint": "^9.8.0", "eslint-config-prettier": "^9.1.0", "prettier": "^3.3.3", diff --git a/src/pages/login.vue b/src/pages/login.vue index b8e854c0..d034ee3a 100644 --- a/src/pages/login.vue +++ b/src/pages/login.vue @@ -86,7 +86,7 @@