Compare commits

..

No commits in common. "f3bddc71704c2b19149693b20e599de4bc743f5d" and "6e429f4f27aa70b455c0c9d4b19b08b5b04acbf5" have entirely different histories.

3 changed files with 56 additions and 22 deletions

View File

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

View File

@ -1,3 +1,5 @@
//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,22 +29,41 @@ async function getUserFromIdService(id: string | undefined) {
return dbUser;
}
async function register(ReqData: IReqRegister) {
if (ReqData.password.length < 6) {
logger.info(`REGISTER :> Invalid password (${ReqData.username})`)
/**
* 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})`)
return { error: "invalidPassword" };
}
const passwordHash = await CredentialService.hash(ReqData.password)
const passwordHash = await CredentialService.hash(sanitizedData.password)
// Does the new user has accepted GDPR ?
if (ReqData.gdpr !== true) {
logger.info(`REGISTER :> GDPR not validated (${ReqData.username})`)
if (sanitizedData.gdpr !== true) {
logger.info(`REGISTER :> GDPR not validated (${sanitizedData.username})`)
return { error: "gdprNotApproved" }
}
// Check if exist and return
const dbUserIfExist = await getUserFromUsername(ReqData.username)
const dbUserIfExist = await getUserFromUsername(sanitizedData.username)
if (dbUserIfExist) {
logger.info(`REGISTER :> User exist (${dbUserIfExist.username})\n ID:${dbUserIfExist.id}`)
return { error: "exist" }
@ -54,9 +73,9 @@ async function register(ReqData: IReqRegister) {
// New UserService (class)
const NewUser = new User(ReqData.username, ReqData.displayName, passwordHash, currentDate);
NewUser.setFirstName(ReqData.firstName);
NewUser.setLastName(ReqData.lastName);
const NewUser = new User(sanitizedData.username, sanitizedData.displayName, passwordHash, currentDate);
NewUser.setFirstName(sanitizedData.firstName);
NewUser.setLastName(sanitizedData.lastName);
// JWT
@ -83,22 +102,35 @@ async function register(ReqData: IReqRegister) {
return userData
}
async function login(ReqData: IReqLogin) {
/**
* 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; }) {
//const passwordHash = await getHashFromPassword(sanitizedData.password);
const dbUser = await MysqlService.User.getByUsername(DbHandler, ReqData.username);
const dbUser = await MysqlService.User.getByUsername(DbHandler, sanitizedData.username);
if (!dbUser) {
console.log(`LoginService :> User does not exist (${ReqData.username})`);
console.log(`LoginService :> User does not exist (${sanitizedData.username})`);
return { error: "userNotFound" };
}
if (ReqData.password.length < 6) {
if (sanitizedData.password.length < 6) {
console.log('X')
console.log(`LoginService :> Invalid password (${ReqData.username})`);
console.log(`LoginService :> Invalid password (${sanitizedData.username})`);
return { error: "invalidPassword" };
}
const isPasswordValid = await CredentialService.compare(ReqData.password, dbUser.hash)
const isPasswordValid = await CredentialService.compare(sanitizedData.password, dbUser.hash)
if (!isPasswordValid) {
console.log(isPasswordValid)
console.log(`LoginService :> Invalid password (${ReqData.username})`);
console.log(`LoginService :> Invalid password (${sanitizedData.username})`);
return { error: "invalidPassword" };
}
// biome-ignore lint/style/useConst: <explanation>
@ -187,8 +219,8 @@ async function deleteUserService(targetId) {
}
const UserService = {
register: register,
login: login,
register: RegisterService,
login: LoginService,
getAll: getAllUsersService,
getFromId: getUserFromIdService,
edit: editUserService,