fix(services): 🚑 User - multiple corrections on call of services
This commit is contained in:
parent
fa93b24ccc
commit
03d10ca675
@ -2,6 +2,9 @@ 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";
|
||||
|
||||
|
||||
const logger = new Logger({ name: "UserService" });
|
||||
@ -50,7 +53,7 @@ async function RegisterService(sanitizedData) {
|
||||
logger.info(`REGISTER :> Invalid password (${sanitizedData.username})`)
|
||||
return { error: "invalidPassword" };
|
||||
}
|
||||
const passwordHash = await getHashFromPassword(sanitizedData.password)
|
||||
const passwordHash = await CredentialService.hash(sanitizedData.password)
|
||||
|
||||
// Does the new user has accepted GDPR ?
|
||||
if (sanitizedData.gdpr !== true) {
|
||||
@ -77,7 +80,7 @@ async function RegisterService(sanitizedData) {
|
||||
// JWT
|
||||
|
||||
const alg = 'HS512'
|
||||
const token = await JwtSign({
|
||||
const token = await JwtService.sign({
|
||||
sub: NewUser.id
|
||||
}, alg,
|
||||
'1d',
|
||||
@ -112,9 +115,9 @@ async function RegisterService(sanitizedData) {
|
||||
* @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) {
|
||||
async function LoginService(sanitizedData: { username: string; password: string; }) {
|
||||
//const passwordHash = await getHashFromPassword(sanitizedData.password);
|
||||
const dbUser = await getUserFromUsername(sanitizedData.username);
|
||||
const dbUser = await MysqlService.User.getByUsername(DbHandler, sanitizedData.username);
|
||||
if (!dbUser) {
|
||||
console.log(`LoginService :> User does not exist (${sanitizedData.username})`);
|
||||
return { error: "userNotFound" };
|
||||
@ -124,13 +127,7 @@ async function LoginService(sanitizedData) {
|
||||
console.log(`LoginService :> Invalid password (${sanitizedData.username})`);
|
||||
return { error: "invalidPassword" };
|
||||
}
|
||||
const isPasswordValid = await Argon2id.verify(
|
||||
Buffer.from(dbUser.passwordHash),
|
||||
Buffer.from(sanitizedData.password),
|
||||
{
|
||||
secret: Buffer.from(`${process.env.HASH_SECRET}`),
|
||||
algorithm: 2
|
||||
});
|
||||
const isPasswordValid = await CredentialService.compare(sanitizedData.password, dbUser.hash)
|
||||
if (!isPasswordValid) {
|
||||
console.log(isPasswordValid)
|
||||
console.log(`LoginService :> Invalid password (${sanitizedData.username})`);
|
||||
@ -139,7 +136,7 @@ async function LoginService(sanitizedData) {
|
||||
// biome-ignore lint/style/useConst: <explanation>
|
||||
let userData = {
|
||||
error: "none",
|
||||
jwt: null,
|
||||
jwt: '',
|
||||
user: {
|
||||
id: dbUser.id,
|
||||
username: dbUser.username,
|
||||
@ -147,8 +144,7 @@ async function LoginService(sanitizedData) {
|
||||
}
|
||||
};
|
||||
|
||||
const alg = 'HS512';
|
||||
userData.jwt = await JwtSign({sub: dbUser.id}, alg, '1d', 'user')
|
||||
userData.jwt = await JwtService.sign({sub: dbUser.id}, {alg: 'HS512'}, '7d', 'user')
|
||||
|
||||
|
||||
console.log("USERDATA :>");
|
||||
|
Loading…
x
Reference in New Issue
Block a user