mirror of
https://github.com/bubblecup-12/VogelSocialMedia.git
synced 2025-07-09 12:08:47 +00:00
added endpoints for profile and feed
This commit is contained in:
parent
9e6eeb27fd
commit
1800056918
22 changed files with 952 additions and 110 deletions
|
@ -9,17 +9,6 @@ dotenv.config();
|
|||
const JWT_SECRET: string = process.env.TOKEN_SECRET!;
|
||||
if (!JWT_SECRET) console.error("No JWT secret provided");
|
||||
|
||||
|
||||
|
||||
// Extend the Express Request interface to include the user property
|
||||
declare global {
|
||||
namespace Express {
|
||||
interface Request {
|
||||
user?: JwtPayload;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Middleware function to authenticate the JWT token
|
||||
export function authenticateToken() {
|
||||
return (req: Request, res: Response, next: NextFunction) => {
|
||||
|
|
25
code/backend/src/middleware/optionalAuthenticateToken.ts
Normal file
25
code/backend/src/middleware/optionalAuthenticateToken.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
import { Request, Response, NextFunction } from "express";
|
||||
import jwt from "jsonwebtoken";
|
||||
import { JwtPayload } from "../types/tokens";
|
||||
const JWT_SECRET: string = process.env.TOKEN_SECRET!;
|
||||
|
||||
export const optionalAuthenticateToken = (
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) => {
|
||||
const authHeader = req.headers["authorization"]; // Get the authorization header
|
||||
const token = authHeader && authHeader.split(" ")[1]; // Extract the token from the "Bearer <token>" format
|
||||
|
||||
if (!token) {
|
||||
return next();
|
||||
}
|
||||
|
||||
jwt.verify(token!, JWT_SECRET, (err, user) => {
|
||||
if (err) {
|
||||
return next();
|
||||
}
|
||||
req.user = user as JwtPayload;
|
||||
next();
|
||||
});
|
||||
};
|
|
@ -1,5 +1,5 @@
|
|||
import { StatusCodes } from "http-status-codes";
|
||||
import multer, { MulterError } from "multer";
|
||||
import multer from "multer";
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
|
||||
// Configure multer to store files in memory
|
||||
|
@ -35,11 +35,7 @@ export const upload = (req: Request, res: Response, next: NextFunction) => {
|
|||
}
|
||||
for (const file of files) {
|
||||
//check for correct filetypes
|
||||
if (
|
||||
file.mimetype !== "image/jpeg" &&
|
||||
file.mimetype !== "image/png" &&
|
||||
file.mimetype !== "image/webp"
|
||||
) {
|
||||
if (["jpeg", "png", "webp"].includes(file.mimetype)) {
|
||||
return res.status(StatusCodes.BAD_REQUEST).json({
|
||||
error: "Invalid file type",
|
||||
details: [
|
42
code/backend/src/middleware/uploadSingle.ts
Normal file
42
code/backend/src/middleware/uploadSingle.ts
Normal file
|
@ -0,0 +1,42 @@
|
|||
import { Request, Response, NextFunction } from "express";
|
||||
import multer from "multer";
|
||||
import { StatusCodes } from "http-status-codes";
|
||||
|
||||
const multerInstance = multer({
|
||||
storage: multer.memoryStorage(),
|
||||
limits: { fileSize: 5 * 1024 * 1024 }, // 5 MB
|
||||
});
|
||||
|
||||
export const upload = (req: Request, res: Response, next: NextFunction) => {
|
||||
multerInstance.single("image")(req, res, (err: any) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res
|
||||
.status(StatusCodes.BAD_REQUEST)
|
||||
.json({ error: err.name, details: [err.message] });
|
||||
}
|
||||
|
||||
if (!req.file) {
|
||||
return res.status(StatusCodes.BAD_REQUEST).json({
|
||||
error: "No file uploaded",
|
||||
details: [{ message: "Please upload a file" }],
|
||||
});
|
||||
}
|
||||
|
||||
const allowedTypes = ["image/jpeg", "image/png", "image/webp"];
|
||||
if (!allowedTypes.includes(req.file.mimetype)) {
|
||||
return res.status(StatusCodes.BAD_REQUEST).json({
|
||||
error: "Invalid file type",
|
||||
details: [
|
||||
{
|
||||
message:
|
||||
"Only .jpeg, .png, or .webp files are allowed. Invalid: " +
|
||||
req.file.originalname,
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue