From b0f631cf45ee59f59840a703f28576dca15fd3e9 Mon Sep 17 00:00:00 2001 From: Kai Ritthaler Date: Fri, 16 May 2025 10:09:41 +0200 Subject: [PATCH] changed statuscodes in authenticateToken --- code/backend/src/middleware/authenticateToken.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/code/backend/src/middleware/authenticateToken.ts b/code/backend/src/middleware/authenticateToken.ts index ba5c856..0724031 100644 --- a/code/backend/src/middleware/authenticateToken.ts +++ b/code/backend/src/middleware/authenticateToken.ts @@ -1,6 +1,7 @@ import express, { NextFunction, Request, Response } from "express"; import jwt, { TokenExpiredError } from "jsonwebtoken"; import dotenv from "dotenv"; +import { StatusCodes } from "http-status-codes"; dotenv.config(); // imports the JWT secret const JWT_SECRET: string = process.env.TOKEN_SECRET!; @@ -28,7 +29,7 @@ export function authenticateToken() { const token = authHeader && authHeader.split(" ")[1]; // split the header to get the token if (token == null) - res.sendStatus(401); // if there is no token, return 401 Unauthorized + res.sendStatus(StatusCodes.UNAUTHORIZED); // if there is no token, return 401 Unauthorized else { jwt.verify(token, JWT_SECRET, (err: any, user: any) => { // verify the token with the secret @@ -36,7 +37,7 @@ export function authenticateToken() { if (err) { if (err instanceof TokenExpiredError) { // check if the error is expired and return 401 - res.status(401).json({ + res.status(StatusCodes.UNAUTHORIZED).json({ error: "Token expired", details: [{ message: "Token expired" }], }); @@ -45,7 +46,7 @@ export function authenticateToken() { // if the token is invalid, return 403 Forbidden else { - res.status(403).json({ + res.status(StatusCodes.FORBIDDEN).json({ error: "Invalid token", details: [{ message: "Invalid token" }], });