fix(services): 🚑 User - multiple corrections on call of services

This commit is contained in:
Mathis H (Avnyr) 2024-04-24 16:56:55 +02:00
parent fa93b24ccc
commit 03d10ca675
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -2,6 +2,9 @@ import {Logger} from "tslog";
import Argon2id from "@node-rs/argon2"; 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 JwtService from "@services/jwt.service";
import MysqlService from "@services/mysql.service";
const logger = new Logger({ name: "UserService" }); const logger = new Logger({ name: "UserService" });
@ -50,7 +53,7 @@ async function RegisterService(sanitizedData) {
logger.info(`REGISTER :> Invalid password (${sanitizedData.username})`) logger.info(`REGISTER :> Invalid password (${sanitizedData.username})`)
return { error: "invalidPassword" }; return { error: "invalidPassword" };
} }
const passwordHash = await getHashFromPassword(sanitizedData.password) const passwordHash = await CredentialService.hash(sanitizedData.password)
// Does the new user has accepted GDPR ? // Does the new user has accepted GDPR ?
if (sanitizedData.gdpr !== true) { if (sanitizedData.gdpr !== true) {
@ -77,7 +80,7 @@ async function RegisterService(sanitizedData) {
// JWT // JWT
const alg = 'HS512' const alg = 'HS512'
const token = await JwtSign({ const token = await JwtService.sign({
sub: NewUser.id sub: NewUser.id
}, alg, }, alg,
'1d', '1d',
@ -112,9 +115,9 @@ async function RegisterService(sanitizedData) {
* @returns {string} result.user.username - The username of the user. * @returns {string} result.user.username - The username of the user.
* @returns {string} result.user.displayName - The display name of the user. * @returns {string} result.user.displayName - The display name of the user.
*/ */
async function LoginService(sanitizedData) { async function LoginService(sanitizedData: { username: string; password: string; }) {
//const passwordHash = await getHashFromPassword(sanitizedData.password); //const passwordHash = await getHashFromPassword(sanitizedData.password);
const dbUser = await getUserFromUsername(sanitizedData.username); const dbUser = await MysqlService.User.getByUsername(DbHandler, sanitizedData.username);
if (!dbUser) { if (!dbUser) {
console.log(`LoginService :> User does not exist (${sanitizedData.username})`); console.log(`LoginService :> User does not exist (${sanitizedData.username})`);
return { error: "userNotFound" }; return { error: "userNotFound" };
@ -124,13 +127,7 @@ async function LoginService(sanitizedData) {
console.log(`LoginService :> Invalid password (${sanitizedData.username})`); console.log(`LoginService :> Invalid password (${sanitizedData.username})`);
return { error: "invalidPassword" }; return { error: "invalidPassword" };
} }
const isPasswordValid = await Argon2id.verify( const isPasswordValid = await CredentialService.compare(sanitizedData.password, dbUser.hash)
Buffer.from(dbUser.passwordHash),
Buffer.from(sanitizedData.password),
{
secret: Buffer.from(`${process.env.HASH_SECRET}`),
algorithm: 2
});
if (!isPasswordValid) { if (!isPasswordValid) {
console.log(isPasswordValid) console.log(isPasswordValid)
console.log(`LoginService :> Invalid password (${sanitizedData.username})`); console.log(`LoginService :> Invalid password (${sanitizedData.username})`);
@ -139,7 +136,7 @@ async function LoginService(sanitizedData) {
// biome-ignore lint/style/useConst: <explanation> // biome-ignore lint/style/useConst: <explanation>
let userData = { let userData = {
error: "none", error: "none",
jwt: null, jwt: '',
user: { user: {
id: dbUser.id, id: dbUser.id,
username: dbUser.username, username: dbUser.username,
@ -147,8 +144,7 @@ async function LoginService(sanitizedData) {
} }
}; };
const alg = 'HS512'; userData.jwt = await JwtService.sign({sub: dbUser.id}, {alg: 'HS512'}, '7d', 'user')
userData.jwt = await JwtSign({sub: dbUser.id}, alg, '1d', 'user')
console.log("USERDATA :>"); console.log("USERDATA :>");