mirror of
https://github.com/bubblecup-12/VogelSocialMedia.git
synced 2025-07-06 15:18:48 +00:00
standardised error messages
This commit is contained in:
parent
dee6a19e51
commit
4042b135c4
3 changed files with 30 additions and 16 deletions
|
@ -37,6 +37,7 @@ export const registerUser = async (req: Request, res: Response) => {
|
||||||
error: "Invalid data",
|
error: "Invalid data",
|
||||||
details: [{ message: `User "${username}" already exists` }],
|
details: [{ message: `User "${username}" already exists` }],
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
const hashedPassword = await bcrypt.hash(password, 10); // hash the password with bcrypt
|
const hashedPassword = await bcrypt.hash(password, 10); // hash the password with bcrypt
|
||||||
if (!hashedPassword) {
|
if (!hashedPassword) {
|
||||||
|
@ -45,6 +46,7 @@ export const registerUser = async (req: Request, res: Response) => {
|
||||||
error: "Invalid data",
|
error: "Invalid data",
|
||||||
details: [{ message: "Server Error" }],
|
details: [{ message: "Server Error" }],
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
const userData = {
|
const userData = {
|
||||||
// create a new user object with the data from the request body and the hashed password
|
// create a new user object with the data from the request body and the hashed password
|
||||||
|
@ -59,6 +61,7 @@ export const registerUser = async (req: Request, res: Response) => {
|
||||||
error: "Server error",
|
error: "Server error",
|
||||||
details: [{ message: "Server Error while creating user" }],
|
details: [{ message: "Server Error while creating user" }],
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
const token: string = generateAccessToken(user.username, user.id); // generate a JWT token with the username and userId as payload
|
const token: string = generateAccessToken(user.username, user.id); // generate a JWT token with the username and userId as payload
|
||||||
res.set("Authorization", `Bearer ${token}`); // set the token in the response header
|
res.set("Authorization", `Bearer ${token}`); // set the token in the response header
|
||||||
|
@ -77,6 +80,7 @@ export const loginUser = async (req: Request, res: Response) => {
|
||||||
error: "Invalid data",
|
error: "Invalid data",
|
||||||
details: [{ message: "Username and password are required" }],
|
details: [{ message: "Username and password are required" }],
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
const user = await prisma.user.findUnique({
|
const user = await prisma.user.findUnique({
|
||||||
// check if the user exists
|
// check if the user exists
|
||||||
|
@ -86,7 +90,12 @@ export const loginUser = async (req: Request, res: Response) => {
|
||||||
});
|
});
|
||||||
if (!user) {
|
if (!user) {
|
||||||
// if the user does not exist, return an error message
|
// if the user does not exist, return an error message
|
||||||
res.status(400).json({ message: `User "${username}" not found` });
|
res
|
||||||
|
.status(400)
|
||||||
|
.json({
|
||||||
|
error: "user not found",
|
||||||
|
details: [{ message: `User "${username}" not found` }],
|
||||||
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const isPasswordValid = await bcrypt.compare(password, user.password); // compare the password with the hashed password in the database
|
const isPasswordValid = await bcrypt.compare(password, user.password); // compare the password with the hashed password in the database
|
||||||
|
@ -96,6 +105,7 @@ export const loginUser = async (req: Request, res: Response) => {
|
||||||
error: "invalid credentials",
|
error: "invalid credentials",
|
||||||
details: [{ message: "Invalid password" }],
|
details: [{ message: "Invalid password" }],
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
const token: string = generateAccessToken(user.username, user.id); // generate a JWT token with the username and userId as payload
|
const token: string = generateAccessToken(user.username, user.id); // generate a JWT token with the username and userId as payload
|
||||||
res.set("Authorization", `Bearer ${token}`); // set the token in the response header
|
res.set("Authorization", `Bearer ${token}`); // set the token in the response header
|
||||||
|
@ -110,6 +120,7 @@ export const getUser = async (req: Request, res: Response) => {
|
||||||
error: "no username",
|
error: "no username",
|
||||||
details: [{ message: "Username is required" }],
|
details: [{ message: "Username is required" }],
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
const user = await prisma.user.findUnique({
|
const user = await prisma.user.findUnique({
|
||||||
where: {
|
where: {
|
||||||
|
|
|
@ -32,24 +32,25 @@ export function authenticateToken() {
|
||||||
else {
|
else {
|
||||||
jwt.verify(token, JWT_SECRET, (err: any, user: any) => {
|
jwt.verify(token, JWT_SECRET, (err: any, user: any) => {
|
||||||
// verify the token with the secret
|
// verify the token with the secret
|
||||||
console.log(err);
|
|
||||||
if (err) {
|
if (err) {
|
||||||
if (err instanceof TokenExpiredError)
|
if (err instanceof TokenExpiredError) {
|
||||||
// check if the error is expired and return 401
|
// check if the error is expired and return 401
|
||||||
res
|
res.status(401).json({
|
||||||
.status(401)
|
|
||||||
.json({
|
|
||||||
error: "Token expired",
|
error: "Token expired",
|
||||||
details: [{ message: "Token expired" }],
|
details: [{ message: "Token expired" }],
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// if the token is invalid, return 403 Forbidden
|
// if the token is invalid, return 403 Forbidden
|
||||||
else
|
else {
|
||||||
res
|
res.status(403).json({
|
||||||
.status(403)
|
|
||||||
.json({
|
|
||||||
error: "Invalid token",
|
error: "Invalid token",
|
||||||
details: [{ message: "Invalid token" }],
|
details: [{ message: "Invalid token" }],
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
|
@ -16,10 +16,12 @@ export function validateData(schema: z.ZodObject<any, any>) {
|
||||||
res
|
res
|
||||||
.status(StatusCodes.BAD_REQUEST)
|
.status(StatusCodes.BAD_REQUEST)
|
||||||
.json({ error: "Invalid data", details: errorMessages });
|
.json({ error: "Invalid data", details: errorMessages });
|
||||||
|
return;
|
||||||
} else {
|
} else {
|
||||||
res
|
res
|
||||||
.status(StatusCodes.INTERNAL_SERVER_ERROR)
|
.status(StatusCodes.INTERNAL_SERVER_ERROR)
|
||||||
.json({ error: "Internal Server Error" });
|
.json({ error: "Internal Server Error" });
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue