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 MysqlService from "@services/mysql.service";
import {Logger} from "tslog"; import {Logger} from "tslog";
import { v4 as uuidv4 } from 'uuid'; 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 type { IDbCategory } from "@interfaces/database/IDbCategory";
import MysqlService from "@services/mysql.service"; import MysqlService from "@services/mysql.service";
import {Logger} from "tslog"; import {Logger} from "tslog";

View File

@ -1,10 +1,10 @@
import {Logger} from "tslog"; import {Logger} from "tslog";
import Argon2id from "@node-rs/argon2";
import MySqlService from "@services/mysql.service"; import MySqlService from "@services/mysql.service";
import CredentialService from "@services/credential.service"; import CredentialService from "@services/credential.service";
import JwtService from "@services/jwt.service"; import JwtService from "@services/jwt.service";
import MysqlService from "@services/mysql.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" }); const logger = new Logger({ name: "UserService" });
@ -29,22 +29,41 @@ async function getUserFromIdService(id: string | undefined) {
return dbUser; return dbUser;
} }
async function register(ReqData: IReqRegister) { /**
if (ReqData.password.length < 6) { * Registers a new user by creating a UserService object, generating a JWT token, and inserting the user into the database.
logger.info(`REGISTER :> Invalid password (${ReqData.username})`) *
* @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" }; return { error: "invalidPassword" };
} }
const passwordHash = await CredentialService.hash(ReqData.password) const passwordHash = await CredentialService.hash(sanitizedData.password)
// Does the new user has accepted GDPR ? // Does the new user has accepted GDPR ?
if (ReqData.gdpr !== true) { if (sanitizedData.gdpr !== true) {
logger.info(`REGISTER :> GDPR not validated (${ReqData.username})`) logger.info(`REGISTER :> GDPR not validated (${sanitizedData.username})`)
return { error: "gdprNotApproved" } return { error: "gdprNotApproved" }
} }
// Check if exist and return // Check if exist and return
const dbUserIfExist = await getUserFromUsername(ReqData.username) const dbUserIfExist = await getUserFromUsername(sanitizedData.username)
if (dbUserIfExist) { if (dbUserIfExist) {
logger.info(`REGISTER :> User exist (${dbUserIfExist.username})\n ID:${dbUserIfExist.id}`) logger.info(`REGISTER :> User exist (${dbUserIfExist.username})\n ID:${dbUserIfExist.id}`)
return { error: "exist" } return { error: "exist" }
@ -54,9 +73,9 @@ async function register(ReqData: IReqRegister) {
// New UserService (class) // New UserService (class)
const NewUser = new User(ReqData.username, ReqData.displayName, passwordHash, currentDate); const NewUser = new User(sanitizedData.username, sanitizedData.displayName, passwordHash, currentDate);
NewUser.setFirstName(ReqData.firstName); NewUser.setFirstName(sanitizedData.firstName);
NewUser.setLastName(ReqData.lastName); NewUser.setLastName(sanitizedData.lastName);
// JWT // JWT
@ -83,22 +102,35 @@ async function register(ReqData: IReqRegister) {
return userData 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 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) { if (!dbUser) {
console.log(`LoginService :> User does not exist (${ReqData.username})`); console.log(`LoginService :> User does not exist (${sanitizedData.username})`);
return { error: "userNotFound" }; return { error: "userNotFound" };
} }
if (ReqData.password.length < 6) { if (sanitizedData.password.length < 6) {
console.log('X') console.log('X')
console.log(`LoginService :> Invalid password (${ReqData.username})`); console.log(`LoginService :> Invalid password (${sanitizedData.username})`);
return { error: "invalidPassword" }; return { error: "invalidPassword" };
} }
const isPasswordValid = await CredentialService.compare(ReqData.password, dbUser.hash) const isPasswordValid = await CredentialService.compare(sanitizedData.password, dbUser.hash)
if (!isPasswordValid) { if (!isPasswordValid) {
console.log(isPasswordValid) console.log(isPasswordValid)
console.log(`LoginService :> Invalid password (${ReqData.username})`); console.log(`LoginService :> Invalid password (${sanitizedData.username})`);
return { error: "invalidPassword" }; return { error: "invalidPassword" };
} }
// biome-ignore lint/style/useConst: <explanation> // biome-ignore lint/style/useConst: <explanation>
@ -187,8 +219,8 @@ async function deleteUserService(targetId) {
} }
const UserService = { const UserService = {
register: register, register: RegisterService,
login: login, login: LoginService,
getAll: getAllUsersService, getAll: getAllUsersService,
getFromId: getUserFromIdService, getFromId: getUserFromIdService,
edit: editUserService, edit: editUserService,