Compare commits
2 Commits
bdfc598218
...
7f52a9d75e
| Author | SHA1 | Date | |
|---|---|---|---|
| 7f52a9d75e | |||
| 62742e6afe |
@ -64,7 +64,10 @@ async function registerUser(req: Request, res: Response): Promise<Response> {
|
|||||||
|
|
||||||
const RegisterServiceResult = await UserService.register(sanitizeData);
|
const RegisterServiceResult = await UserService.register(sanitizeData);
|
||||||
|
|
||||||
if (typeof RegisterServiceResult !== 'string' && RegisterServiceResult.message === "GDPR acceptance is required.") {
|
if (
|
||||||
|
typeof RegisterServiceResult !== "string" &&
|
||||||
|
RegisterServiceResult.message === "GDPR acceptance is required."
|
||||||
|
) {
|
||||||
logger.warn(`GDPR not approved (${req.ip})`);
|
logger.warn(`GDPR not approved (${req.ip})`);
|
||||||
return res.status(HttpStatusCode.BadRequest).json({
|
return res.status(HttpStatusCode.BadRequest).json({
|
||||||
error: RegisterServiceResult.error,
|
error: RegisterServiceResult.error,
|
||||||
@ -165,11 +168,17 @@ async function getAllUsers(req: Request, res: Response) {
|
|||||||
}
|
}
|
||||||
const AllUserResponse = await UserService.getAll();
|
const AllUserResponse = await UserService.getAll();
|
||||||
if (!AllUserResponse.users) {
|
if (!AllUserResponse.users) {
|
||||||
return res.type("application/json").status(HttpStatusCode.InternalServerError).json({
|
return res
|
||||||
error: "Internal server error",
|
.type("application/json")
|
||||||
});
|
.status(HttpStatusCode.InternalServerError)
|
||||||
|
.json({
|
||||||
|
error: "Internal server error",
|
||||||
|
});
|
||||||
}
|
}
|
||||||
return res.type("application/json").status(HttpStatusCode.Found).json(AllUserResponse);
|
return res
|
||||||
|
.type("application/json")
|
||||||
|
.status(HttpStatusCode.Found)
|
||||||
|
.json(AllUserResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getUser(req: Request, res: Response) {
|
async function getUser(req: Request, res: Response) {
|
||||||
@ -177,16 +186,22 @@ async function getUser(req: Request, res: Response) {
|
|||||||
const bearerToken = authHeader?.split(" ")[1];
|
const bearerToken = authHeader?.split(" ")[1];
|
||||||
if (!bearerToken) {
|
if (!bearerToken) {
|
||||||
logger.warn(`Bearer token not provided (${req.ip})`);
|
logger.warn(`Bearer token not provided (${req.ip})`);
|
||||||
return res.type("application/json").status(HttpStatusCode.Unauthorized).json({
|
return res
|
||||||
error: "Unauthorized",
|
.type("application/json")
|
||||||
});
|
.status(HttpStatusCode.Unauthorized)
|
||||||
|
.json({
|
||||||
|
error: "Unauthorized",
|
||||||
|
});
|
||||||
}
|
}
|
||||||
const payload = await JwtService.verify(bearerToken);
|
const payload = await JwtService.verify(bearerToken);
|
||||||
if (!payload || !payload.sub) {
|
if (!payload || !payload.sub) {
|
||||||
logger.warn(`Unauthorized access attempt (${req.ip})`);
|
logger.warn(`Unauthorized access attempt (${req.ip})`);
|
||||||
return res.type("application/json").status(HttpStatusCode.Unauthorized).json({
|
return res
|
||||||
error: "Unauthorized",
|
.type("application/json")
|
||||||
});
|
.status(HttpStatusCode.Unauthorized)
|
||||||
|
.json({
|
||||||
|
error: "Unauthorized",
|
||||||
|
});
|
||||||
}
|
}
|
||||||
const sourceUser = await UserService.getFromId(payload.sub);
|
const sourceUser = await UserService.getFromId(payload.sub);
|
||||||
if (!sourceUser) {
|
if (!sourceUser) {
|
||||||
@ -195,9 +210,12 @@ async function getUser(req: Request, res: Response) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
if ("username" in sourceUser && !sourceUser.is_admin) {
|
if ("username" in sourceUser && !sourceUser.is_admin) {
|
||||||
return res.type("application/json").status(HttpStatusCode.Unauthorized).json({
|
return res
|
||||||
error: "Unauthorized",
|
.type("application/json")
|
||||||
});
|
.status(HttpStatusCode.Unauthorized)
|
||||||
|
.json({
|
||||||
|
error: "Unauthorized",
|
||||||
|
});
|
||||||
}
|
}
|
||||||
const userId = req.params["id"];
|
const userId = req.params["id"];
|
||||||
const dbUser = await UserService.getFromId(userId);
|
const dbUser = await UserService.getFromId(userId);
|
||||||
@ -308,7 +326,7 @@ async function deleteUser(req: Request, res: Response): Promise<Response> {
|
|||||||
}
|
}
|
||||||
const payload = await JwtService.verify(bearerToken);
|
const payload = await JwtService.verify(bearerToken);
|
||||||
|
|
||||||
if (!payload) {
|
if (!payload || !payload.sub) {
|
||||||
logger.warn(`Invalid token (${req.ip})`);
|
logger.warn(`Invalid token (${req.ip})`);
|
||||||
return res.type("application/json").status(401).json({
|
return res.type("application/json").status(401).json({
|
||||||
error: "Invalid token",
|
error: "Invalid token",
|
||||||
|
|||||||
@ -2,13 +2,13 @@ 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 { ErrorType, type ISError } from "@interfaces/services/ISError";
|
||||||
|
import type { IUserUpdate } from "@interfaces/services/IUserUpdate";
|
||||||
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 { isDebugMode } from "@utils/debugState";
|
import { isDebugMode } from "@utils/debugState";
|
||||||
import { Logger } from "tslog";
|
import { Logger } from "tslog";
|
||||||
import { v4 } from "uuid";
|
import { v4 } from "uuid";
|
||||||
import {IUserUpdate} from "@interfaces/services/IUserUpdate";
|
|
||||||
|
|
||||||
const logger = new Logger({
|
const logger = new Logger({
|
||||||
name: "UserService",
|
name: "UserService",
|
||||||
@ -99,7 +99,6 @@ DbHandler.factorize({
|
|||||||
logger.trace(`\n\n> ${result._valuesArray.join(', ')}\n\n> ${result.totalFields}\n\n> ${result._keysTemplate}\n`)
|
logger.trace(`\n\n> ${result._valuesArray.join(', ')}\n\n> ${result.totalFields}\n\n> ${result._keysTemplate}\n`)
|
||||||
})*/
|
})*/
|
||||||
|
|
||||||
//ToTest
|
|
||||||
/**
|
/**
|
||||||
* Registers a new user.
|
* Registers a new user.
|
||||||
*
|
*
|
||||||
@ -164,7 +163,7 @@ async function register(inputData: IReqRegister): Promise<ISError | string> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// JWT
|
// JWT
|
||||||
const token = await JwtService.sign(
|
return await JwtService.sign(
|
||||||
{
|
{
|
||||||
sub: currentId,
|
sub: currentId,
|
||||||
},
|
},
|
||||||
@ -174,7 +173,6 @@ async function register(inputData: IReqRegister): Promise<ISError | string> {
|
|||||||
"1d",
|
"1d",
|
||||||
"user",
|
"user",
|
||||||
);
|
);
|
||||||
return token;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error(`\n\n${err}\n`);
|
logger.error(`\n\n${err}\n`);
|
||||||
return {
|
return {
|
||||||
@ -184,7 +182,6 @@ async function register(inputData: IReqRegister): Promise<ISError | string> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//ToTest
|
|
||||||
/**
|
/**
|
||||||
* Logs in a user with the provided input data.
|
* Logs in a user with the provided input data.
|
||||||
*
|
*
|
||||||
@ -314,12 +311,12 @@ async function editUserService(
|
|||||||
}
|
}
|
||||||
const result = await MySqlService.User.update(DbHandler, inputData);
|
const result = await MySqlService.User.update(DbHandler, inputData);
|
||||||
if (result.affectedRows === 0) {
|
if (result.affectedRows === 0) {
|
||||||
return {
|
return {
|
||||||
error: ErrorType.DatabaseError,
|
error: ErrorType.DatabaseError,
|
||||||
message: "An unknown error occurred.",
|
message: "An unknown error occurred.",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -331,9 +328,8 @@ async function editUserService(
|
|||||||
async function deleteUserService(targetId: string): Promise<boolean> {
|
async function deleteUserService(targetId: string): Promise<boolean> {
|
||||||
logger.info(`Deleting user ${targetId}`);
|
logger.info(`Deleting user ${targetId}`);
|
||||||
try {
|
try {
|
||||||
const DeleteResult = await MySqlService.User.delete(DbHandler, targetId)
|
const DeleteResult = await MySqlService.User.delete(DbHandler, targetId);
|
||||||
if (DeleteResult.affectedRows !== 0) return true;
|
return DeleteResult.affectedRows !== 0;
|
||||||
return false;
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.warn(e);
|
logger.warn(e);
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user