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);
|
||||
|
||||
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})`);
|
||||
return res.status(HttpStatusCode.BadRequest).json({
|
||||
error: RegisterServiceResult.error,
|
||||
@ -165,11 +168,17 @@ async function getAllUsers(req: Request, res: Response) {
|
||||
}
|
||||
const AllUserResponse = await UserService.getAll();
|
||||
if (!AllUserResponse.users) {
|
||||
return res.type("application/json").status(HttpStatusCode.InternalServerError).json({
|
||||
return res
|
||||
.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) {
|
||||
@ -177,14 +186,20 @@ async function getUser(req: Request, res: Response) {
|
||||
const bearerToken = authHeader?.split(" ")[1];
|
||||
if (!bearerToken) {
|
||||
logger.warn(`Bearer token not provided (${req.ip})`);
|
||||
return res.type("application/json").status(HttpStatusCode.Unauthorized).json({
|
||||
return res
|
||||
.type("application/json")
|
||||
.status(HttpStatusCode.Unauthorized)
|
||||
.json({
|
||||
error: "Unauthorized",
|
||||
});
|
||||
}
|
||||
const payload = await JwtService.verify(bearerToken);
|
||||
if (!payload || !payload.sub) {
|
||||
logger.warn(`Unauthorized access attempt (${req.ip})`);
|
||||
return res.type("application/json").status(HttpStatusCode.Unauthorized).json({
|
||||
return res
|
||||
.type("application/json")
|
||||
.status(HttpStatusCode.Unauthorized)
|
||||
.json({
|
||||
error: "Unauthorized",
|
||||
});
|
||||
}
|
||||
@ -195,7 +210,10 @@ async function getUser(req: Request, res: Response) {
|
||||
});
|
||||
}
|
||||
if ("username" in sourceUser && !sourceUser.is_admin) {
|
||||
return res.type("application/json").status(HttpStatusCode.Unauthorized).json({
|
||||
return res
|
||||
.type("application/json")
|
||||
.status(HttpStatusCode.Unauthorized)
|
||||
.json({
|
||||
error: "Unauthorized",
|
||||
});
|
||||
}
|
||||
@ -308,7 +326,7 @@ async function deleteUser(req: Request, res: Response): Promise<Response> {
|
||||
}
|
||||
const payload = await JwtService.verify(bearerToken);
|
||||
|
||||
if (!payload) {
|
||||
if (!payload || !payload.sub) {
|
||||
logger.warn(`Invalid token (${req.ip})`);
|
||||
return res.type("application/json").status(401).json({
|
||||
error: "Invalid token",
|
||||
|
||||
@ -2,13 +2,13 @@ import type { IDbUser } from "@interfaces/database/IDbUser";
|
||||
import type { IReqLogin } from "@interfaces/requests/IReqLogin";
|
||||
import type { IReqRegister } from "@interfaces/requests/IReqRegister";
|
||||
import { ErrorType, type ISError } from "@interfaces/services/ISError";
|
||||
import type { IUserUpdate } from "@interfaces/services/IUserUpdate";
|
||||
import CredentialService from "@services/credential.service";
|
||||
import JwtService from "@services/jwt.service";
|
||||
import MySqlService from "@services/mysql.service";
|
||||
import { isDebugMode } from "@utils/debugState";
|
||||
import { Logger } from "tslog";
|
||||
import { v4 } from "uuid";
|
||||
import {IUserUpdate} from "@interfaces/services/IUserUpdate";
|
||||
|
||||
const logger = new Logger({
|
||||
name: "UserService",
|
||||
@ -99,7 +99,6 @@ DbHandler.factorize({
|
||||
logger.trace(`\n\n> ${result._valuesArray.join(', ')}\n\n> ${result.totalFields}\n\n> ${result._keysTemplate}\n`)
|
||||
})*/
|
||||
|
||||
//ToTest
|
||||
/**
|
||||
* Registers a new user.
|
||||
*
|
||||
@ -164,7 +163,7 @@ async function register(inputData: IReqRegister): Promise<ISError | string> {
|
||||
);
|
||||
|
||||
// JWT
|
||||
const token = await JwtService.sign(
|
||||
return await JwtService.sign(
|
||||
{
|
||||
sub: currentId,
|
||||
},
|
||||
@ -174,7 +173,6 @@ async function register(inputData: IReqRegister): Promise<ISError | string> {
|
||||
"1d",
|
||||
"user",
|
||||
);
|
||||
return token;
|
||||
} catch (err) {
|
||||
logger.error(`\n\n${err}\n`);
|
||||
return {
|
||||
@ -184,7 +182,6 @@ async function register(inputData: IReqRegister): Promise<ISError | string> {
|
||||
}
|
||||
}
|
||||
|
||||
//ToTest
|
||||
/**
|
||||
* Logs in a user with the provided input data.
|
||||
*
|
||||
@ -331,9 +328,8 @@ async function editUserService(
|
||||
async function deleteUserService(targetId: string): Promise<boolean> {
|
||||
logger.info(`Deleting user ${targetId}`);
|
||||
try {
|
||||
const DeleteResult = await MySqlService.User.delete(DbHandler, targetId)
|
||||
if (DeleteResult.affectedRows !== 0) return true;
|
||||
return false;
|
||||
const DeleteResult = await MySqlService.User.delete(DbHandler, targetId);
|
||||
return DeleteResult.affectedRows !== 0;
|
||||
} catch (e) {
|
||||
logger.warn(e);
|
||||
return false;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user