feat(controllers): improve response error handling in auth.controller

- Improve error handling in methods of the `auth.controller`
- Refactor conditional checks for errors to handle non-string types and missing payload data.
- Update response error messages to be more consistent and informative.

Issue: #19
Signed-off-by: Mathis <yidhra@tuta.io>
This commit is contained in:
Mathis H (Avnyr) 2024-05-02 15:41:38 +02:00
parent 62742e6afe
commit 7f52a9d75e
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -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
.type("application/json")
.status(HttpStatusCode.InternalServerError)
.json({
error: "Internal server error", 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,14 +186,20 @@ 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
.type("application/json")
.status(HttpStatusCode.Unauthorized)
.json({
error: "Unauthorized", 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
.type("application/json")
.status(HttpStatusCode.Unauthorized)
.json({
error: "Unauthorized", error: "Unauthorized",
}); });
} }
@ -195,7 +210,10 @@ 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
.type("application/json")
.status(HttpStatusCode.Unauthorized)
.json({
error: "Unauthorized", error: "Unauthorized",
}); });
} }
@ -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",