Compare commits
No commits in common. "3232e5fac16df3fb5219ee0a2b184392cc23fcbc" and "355cb0ec908e15a99cea5ed8f2409ca0290a1948" have entirely different histories.
3232e5fac1
...
355cb0ec90
@ -1,9 +1,8 @@
|
|||||||
export interface IReqRegister {
|
export interface IReqRegister {
|
||||||
username: string;
|
username: string;
|
||||||
|
displayName: string;
|
||||||
firstName: string;
|
firstName: string;
|
||||||
lastName: string;
|
lastName: string;
|
||||||
dob: Date;
|
|
||||||
email: string;
|
|
||||||
gdpr?: boolean;
|
|
||||||
password: string;
|
password: string;
|
||||||
|
gdpr?: boolean;
|
||||||
}
|
}
|
||||||
|
@ -1,25 +0,0 @@
|
|||||||
/**
|
|
||||||
* Represents an error object.
|
|
||||||
*
|
|
||||||
* @interface ISError
|
|
||||||
*/
|
|
||||||
export interface ISError {
|
|
||||||
error: ErrorType;
|
|
||||||
message: string;
|
|
||||||
result?: unknown;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents the types of errors that can occur in the application.
|
|
||||||
*
|
|
||||||
* @enum {number}
|
|
||||||
*/
|
|
||||||
export enum ErrorType {
|
|
||||||
InvalidData = 0,
|
|
||||||
DatabaseError = 1,
|
|
||||||
ServiceError = 2,
|
|
||||||
NotFound = 3,
|
|
||||||
PasswordInvalid = 4,
|
|
||||||
UnAuthorized = 5,
|
|
||||||
UnexpectedError = 6,
|
|
||||||
}
|
|
@ -1,14 +1,10 @@
|
|||||||
import type {IDbUser} from "@interfaces/database/IDbUser";
|
import type { IReqLogin } from "@interfaces/requests/IReqLogin";
|
||||||
import type {IReqLogin} from "@interfaces/requests/IReqLogin";
|
import type { IReqRegister } from "@interfaces/requests/IReqRegister";
|
||||||
import type {IReqRegister} from "@interfaces/requests/IReqRegister";
|
|
||||||
import {ErrorType, type ISError} from "@interfaces/services/ISError";
|
|
||||||
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 MysqlService from "@services/mysql.service";
|
import MysqlService from "@services/mysql.service";
|
||||||
import {Logger} from "tslog";
|
import { Logger } from "tslog";
|
||||||
import {v4} from "uuid";
|
|
||||||
|
|
||||||
|
|
||||||
const logger = new Logger({
|
const logger = new Logger({
|
||||||
name: "UserService",
|
name: "UserService",
|
||||||
@ -17,124 +13,92 @@ const logger = new Logger({
|
|||||||
const DbHandler = new MySqlService.Handler("UserService");
|
const DbHandler = new MySqlService.Handler("UserService");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves a user from the database by the given email address.
|
* Retrieves a user object from the database based on the given username.
|
||||||
*
|
*
|
||||||
* @param {string} targetEmail - The email address of the user to retrieve.
|
* @param {string} username - The username of the user to retrieve.
|
||||||
* @returns {Promise<IDbUser | ISError>}
|
* @returns {Promise<Object | null>} - The user object if found, or null if not found.
|
||||||
* - A promise that resolves with the user.
|
|
||||||
* - If the user is not found, an error object is returned.
|
|
||||||
* - If an error occurs during the database operation, an error object is returned.
|
|
||||||
*/
|
*/
|
||||||
async function getUserByEmail(targetEmail: string): Promise<IDbUser | ISError> {
|
async function getUserFromUsername(username: string): Promise<object | null> {
|
||||||
try {
|
const dbUser = await MySqlService.User.getByUsername(DbHandler, username);
|
||||||
const dbUser = await MySqlService.User.getByEmail(DbHandler, targetEmail);
|
if (dbUser === undefined) return null;
|
||||||
if (dbUser === undefined) {
|
return dbUser;
|
||||||
logger.info(`User not found (${targetEmail})`);
|
|
||||||
return {
|
|
||||||
error: ErrorType.NotFound,
|
|
||||||
message: "The user was not fund.",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return dbUser;
|
|
||||||
} catch (err) {
|
|
||||||
logger.error(err);
|
|
||||||
return {
|
|
||||||
error: ErrorType.DatabaseError,
|
|
||||||
message: "An unknown error occurred.",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
async function getUserFromIdService(id: string | undefined) {
|
||||||
* Retrieves a user from the database based on the provided ID.
|
const dbUser = await MySqlService.User.getById(DbHandler, id);
|
||||||
*
|
if (dbUser === undefined) return null;
|
||||||
* @param {string} id - The ID of the user to retrieve.
|
return dbUser;
|
||||||
* @returns {Promise<IDbUser | ISError>} - A promise that resolves with the user object if found, or an error object if not.
|
|
||||||
*/
|
|
||||||
async function getUserFromIdService(id: string): Promise<IDbUser | ISError> {
|
|
||||||
try {
|
|
||||||
if (!id || id.length !== 36) {
|
|
||||||
logger.info(`Invalid ID (${id})`);
|
|
||||||
return {
|
|
||||||
error: ErrorType.InvalidData,
|
|
||||||
message: "Invalid ID length.",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
const dbUser = await MySqlService.User.getById(DbHandler, id);
|
|
||||||
if (dbUser === undefined) {
|
|
||||||
logger.info(`User not found (${id})`);
|
|
||||||
return {
|
|
||||||
error: ErrorType.NotFound,
|
|
||||||
message: "The user was not found.",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return dbUser;
|
|
||||||
} catch (err) {
|
|
||||||
return {
|
|
||||||
error: ErrorType.DatabaseError,
|
|
||||||
message: "An unknown error occurred.",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function register(ReqData: IReqRegister): Promise<ISError | string> {
|
async function register(ReqData: IReqRegister) {
|
||||||
if (ReqData.password.length < 6) {
|
if (ReqData.password.length < 6) {
|
||||||
|
logger.info(`REGISTER :> Invalid password (${ReqData.username})`);
|
||||||
return {
|
return {
|
||||||
error: ErrorType.InvalidData,
|
error: "invalidPassword",
|
||||||
message: "Password must be at least 6 characters long.",
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const passwordHash = await CredentialService.hash(`${ReqData.password}`);
|
const passwordHash = await CredentialService.hash(ReqData.password);
|
||||||
|
|
||||||
// Does the new user has accepted GDPR ?
|
// Does the new user has accepted GDPR ?
|
||||||
if (ReqData.gdpr !== true) {
|
if (ReqData.gdpr !== true) {
|
||||||
|
logger.info(`REGISTER :> GDPR not validated (${ReqData.username})`);
|
||||||
return {
|
return {
|
||||||
error: ErrorType.InvalidData,
|
error: "gdprNotApproved",
|
||||||
message: "GDPR acceptance is required.",
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const currentDate = new Date();
|
|
||||||
|
|
||||||
// Check if exist and return
|
// Check if exist and return
|
||||||
const dbUserIfExist: IDbUser | ISError = await getUserByEmail(ReqData.email);
|
|
||||||
if ("error" in dbUserIfExist) {
|
const dbUserIfExist = await getUserFromUsername(ReqData.username);
|
||||||
|
if (dbUserIfExist) {
|
||||||
|
logger.info(
|
||||||
|
`REGISTER :> User exist (${dbUserIfExist.username})\n ID:${dbUserIfExist.id}`,
|
||||||
|
);
|
||||||
return {
|
return {
|
||||||
error: dbUserIfExist.error,
|
error: "exist",
|
||||||
message: dbUserIfExist.message,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const NewUser = await MySqlService.User.insert(DbHandler, {
|
const currentDate = new Date();
|
||||||
id: v4(),
|
|
||||||
email: ReqData.email,
|
// New UserService (class)
|
||||||
username: ReqData.username,
|
|
||||||
firstname: ReqData.firstName,
|
const NewUser = new User(
|
||||||
lastname: ReqData.lastName,
|
ReqData.username,
|
||||||
dob: ReqData.dob,
|
ReqData.displayName,
|
||||||
hash: passwordHash,
|
passwordHash,
|
||||||
gdpr: currentDate,
|
currentDate,
|
||||||
is_admin: false,
|
);
|
||||||
is_mail_verified: false,
|
NewUser.setFirstName(ReqData.firstName);
|
||||||
});
|
NewUser.setLastName(ReqData.lastName);
|
||||||
if ("error" in NewUser || !NewUser.id) {
|
|
||||||
return {
|
|
||||||
error: ErrorType.DatabaseError,
|
|
||||||
message: 'Error when inserting user in database.'
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// JWT
|
// JWT
|
||||||
|
|
||||||
|
const alg = "HS512";
|
||||||
const token = await JwtService.sign(
|
const token = await JwtService.sign(
|
||||||
{
|
{
|
||||||
sub: NewUser.id,
|
sub: NewUser.id,
|
||||||
},
|
},
|
||||||
{
|
alg,
|
||||||
alg: "HS512",
|
|
||||||
},
|
|
||||||
"1d",
|
"1d",
|
||||||
"user",
|
"user",
|
||||||
);
|
);
|
||||||
return token;
|
|
||||||
|
const userData = {
|
||||||
|
error: "none",
|
||||||
|
jwt: token,
|
||||||
|
user: {
|
||||||
|
id: NewUser.id,
|
||||||
|
username: NewUser.username,
|
||||||
|
displayName: NewUser.displayName,
|
||||||
|
firstName: NewUser.firstName,
|
||||||
|
lastName: NewUser.lastName,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
logger.info(userData);
|
||||||
|
await Db.collection("users").insertOne(NewUser);
|
||||||
|
logger.info(`REGISTER :> Inserted new user (${NewUser.username})`);
|
||||||
|
return userData;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function login(ReqData: IReqLogin) {
|
async function login(ReqData: IReqLogin) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user