Compare commits

...

3 Commits

Author SHA1 Message Date
f3bddc7170
feat(services): change IDbBrand import to explicit type import
Change the import of `IDbBrand` in `brand.service.ts` to explicitly denote it as a type. This clarifies that `IDbBrand` is only used for its type information, improving readability and understanding of code usage.

Signed-off-by: Mathis <yidhra@tuta.io>
2024-04-25 16:57:39 +02:00
789d62ea62
feat(services): refactor register and login functions in UserService
- Removed extraneous comments and updated function parameters to use interface types in `register` and `login` methods.
- Renamed `RegisterService` and `LoginService` to `register` and `login` respectively.
- This update enhances readability and maintains the consistency in code by leveraging TypeScript's type-checking feature.

Signed-off-by: Mathis <yidhra@tuta.io>
2024-04-25 16:57:21 +02:00
debb30b896
refactor(services): remove duplicate comments in category.service.ts
Duplicate comments were found and removed from `category.service.ts`. This refactoring step will make the code cleaner and less confusing.

Signed-off-by: Mathis <yidhra@tuta.io>
2024-04-25 16:57:10 +02:00
3 changed files with 22 additions and 56 deletions

View File

@ -1,4 +1,4 @@
import IDbBrand from "@interfaces/database/IDbBrand";
import type IDbBrand from "@interfaces/database/IDbBrand";
import MysqlService from "@services/mysql.service";
import {Logger} from "tslog";
import { v4 as uuidv4 } from 'uuid';

View File

@ -1,5 +1,3 @@
//FEAT Create new category
//FEAT Create new category
import type { IDbCategory } from "@interfaces/database/IDbCategory";
import MysqlService from "@services/mysql.service";
import {Logger} from "tslog";

View File

@ -1,10 +1,10 @@
import {Logger} from "tslog";
import Argon2id from "@node-rs/argon2";
import MySqlService from "@services/mysql.service";
import CredentialService from "@services/credential.service";
import JwtService from "@services/jwt.service";
import MysqlService from "@services/mysql.service";
import type {IReqRegister} from "@interfaces/requests/IReqRegister";
import {IReqLogin} from "@interfaces/requests/IReqLogin";
const logger = new Logger({ name: "UserService" });
@ -29,41 +29,22 @@ async function getUserFromIdService(id: string | undefined) {
return dbUser;
}
/**
* Registers a new user by creating a UserService object, generating a JWT token, and inserting the user into the database.
*
* @param {Object} sanitizedData - The sanitized user data.
* @param {string} sanitizedData.username - The username of the new user.
* @param {string} sanitizedData.displayName - The display namcoe of the new user.
* @param {string} sanitizedData.firstName
* @param {string} sanitizedData.lastName
* @param {string} sanitizedData.password - The password of the new user.
* @param {boolean} sanitizedData.gdpr - Indicates whether the new user has accepted GDPR.
*
* @returns {Object} - An object containing the registered user's data and JWT token.
* @returns {string} error - The error name, if any. "none" if registration was successful.
* @returns {string|null} jwt - The JWT token for the registered user. Null if registration was not successful.
* @returns {Object|null} user - The registered user's data. Null if registration was not successful.
* @returns {string|null} user.id - The ID of the registered user. Null if registration was not successful.
* @returns {string|null} user.username - The username of the registered user. Null if registration was not successful.
* @returns {string|null} user.displayName - The display name of the registered user. Null if registration was not successful.
*/
async function RegisterService(sanitizedData) {
if (sanitizedData.password.length < 6) {
logger.info(`REGISTER :> Invalid password (${sanitizedData.username})`)
async function register(ReqData: IReqRegister) {
if (ReqData.password.length < 6) {
logger.info(`REGISTER :> Invalid password (${ReqData.username})`)
return { error: "invalidPassword" };
}
const passwordHash = await CredentialService.hash(sanitizedData.password)
const passwordHash = await CredentialService.hash(ReqData.password)
// Does the new user has accepted GDPR ?
if (sanitizedData.gdpr !== true) {
logger.info(`REGISTER :> GDPR not validated (${sanitizedData.username})`)
if (ReqData.gdpr !== true) {
logger.info(`REGISTER :> GDPR not validated (${ReqData.username})`)
return { error: "gdprNotApproved" }
}
// Check if exist and return
const dbUserIfExist = await getUserFromUsername(sanitizedData.username)
const dbUserIfExist = await getUserFromUsername(ReqData.username)
if (dbUserIfExist) {
logger.info(`REGISTER :> User exist (${dbUserIfExist.username})\n ID:${dbUserIfExist.id}`)
return { error: "exist" }
@ -73,9 +54,9 @@ async function RegisterService(sanitizedData) {
// New UserService (class)
const NewUser = new User(sanitizedData.username, sanitizedData.displayName, passwordHash, currentDate);
NewUser.setFirstName(sanitizedData.firstName);
NewUser.setLastName(sanitizedData.lastName);
const NewUser = new User(ReqData.username, ReqData.displayName, passwordHash, currentDate);
NewUser.setFirstName(ReqData.firstName);
NewUser.setLastName(ReqData.lastName);
// JWT
@ -102,35 +83,22 @@ async function RegisterService(sanitizedData) {
return userData
}
/**
* Performs the login process by verifying the provided credentials.
* @param {Object} sanitizedData - The sanitized user login data.
* @param {string} sanitizedData.username - The username provided by the user.
* @param {string} sanitizedData.password - The password provided by the user.
* @returns {Object} - The login result object.
* @returns {string} result.error - The error code if there is an error during the login process.
* @returns {string} result.jwt - The JSON Web Token (JWT) generated upon successful login.
* @returns {Object} result.user - The user information.
* @returns {number} result.user.id - The ID of the user.
* @returns {string} result.user.username - The username of the user.
* @returns {string} result.user.displayName - The display name of the user.
*/
async function LoginService(sanitizedData: { username: string; password: string; }) {
async function login(ReqData: IReqLogin) {
//const passwordHash = await getHashFromPassword(sanitizedData.password);
const dbUser = await MysqlService.User.getByUsername(DbHandler, sanitizedData.username);
const dbUser = await MysqlService.User.getByUsername(DbHandler, ReqData.username);
if (!dbUser) {
console.log(`LoginService :> User does not exist (${sanitizedData.username})`);
console.log(`LoginService :> User does not exist (${ReqData.username})`);
return { error: "userNotFound" };
}
if (sanitizedData.password.length < 6) {
if (ReqData.password.length < 6) {
console.log('X')
console.log(`LoginService :> Invalid password (${sanitizedData.username})`);
console.log(`LoginService :> Invalid password (${ReqData.username})`);
return { error: "invalidPassword" };
}
const isPasswordValid = await CredentialService.compare(sanitizedData.password, dbUser.hash)
const isPasswordValid = await CredentialService.compare(ReqData.password, dbUser.hash)
if (!isPasswordValid) {
console.log(isPasswordValid)
console.log(`LoginService :> Invalid password (${sanitizedData.username})`);
console.log(`LoginService :> Invalid password (${ReqData.username})`);
return { error: "invalidPassword" };
}
// biome-ignore lint/style/useConst: <explanation>
@ -219,8 +187,8 @@ async function deleteUserService(targetId) {
}
const UserService = {
register: RegisterService,
login: LoginService,
register: register,
login: login,
getAll: getAllUsersService,
getFromId: getUserFromIdService,
edit: editUserService,