phone auth, image uploading, address broken up
This commit is contained in:
@@ -1,35 +1,34 @@
|
||||
const jwt = require('jsonwebtoken');
|
||||
const { User } = require('../models'); // Import from models/index.js to get models with associations
|
||||
const jwt = require("jsonwebtoken");
|
||||
const { User } = require("../models"); // Import from models/index.js to get models with associations
|
||||
|
||||
const authenticateToken = async (req, res, next) => {
|
||||
const authHeader = req.headers['authorization'];
|
||||
const token = authHeader && authHeader.split(' ')[1];
|
||||
const authHeader = req.headers["authorization"];
|
||||
const token = authHeader && authHeader.split(" ")[1];
|
||||
|
||||
if (!token) {
|
||||
return res.status(401).json({ error: 'Access token required' });
|
||||
return res.status(401).json({ error: "Access token required" });
|
||||
}
|
||||
|
||||
try {
|
||||
const decoded = jwt.verify(token, process.env.JWT_SECRET);
|
||||
// Handle both 'userId' and 'id' for backward compatibility
|
||||
const userId = decoded.userId || decoded.id;
|
||||
|
||||
const userId = decoded.id;
|
||||
|
||||
if (!userId) {
|
||||
return res.status(401).json({ error: 'Invalid token format' });
|
||||
return res.status(401).json({ error: "Invalid token format" });
|
||||
}
|
||||
|
||||
|
||||
const user = await User.findByPk(userId);
|
||||
|
||||
|
||||
if (!user) {
|
||||
return res.status(401).json({ error: 'User not found' });
|
||||
return res.status(401).json({ error: "User not found" });
|
||||
}
|
||||
|
||||
req.user = user;
|
||||
next();
|
||||
} catch (error) {
|
||||
console.error('Auth middleware error:', error);
|
||||
return res.status(403).json({ error: 'Invalid or expired token' });
|
||||
console.error("Auth middleware error:", error);
|
||||
return res.status(403).json({ error: "Invalid or expired token" });
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { authenticateToken };
|
||||
module.exports = { authenticateToken };
|
||||
|
||||
40
backend/middleware/upload.js
Normal file
40
backend/middleware/upload.js
Normal file
@@ -0,0 +1,40 @@
|
||||
const multer = require('multer');
|
||||
const path = require('path');
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
|
||||
// Configure storage for profile images
|
||||
const profileImageStorage = multer.diskStorage({
|
||||
destination: function (req, file, cb) {
|
||||
cb(null, path.join(__dirname, '../uploads/profiles'));
|
||||
},
|
||||
filename: function (req, file, cb) {
|
||||
// Generate unique filename: uuid + original extension
|
||||
const uniqueId = uuidv4();
|
||||
const ext = path.extname(file.originalname);
|
||||
cb(null, `${uniqueId}${ext}`);
|
||||
}
|
||||
});
|
||||
|
||||
// File filter to accept only images
|
||||
const imageFileFilter = (req, file, cb) => {
|
||||
// Accept images only
|
||||
const allowedMimes = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp'];
|
||||
if (allowedMimes.includes(file.mimetype)) {
|
||||
cb(null, true);
|
||||
} else {
|
||||
cb(new Error('Invalid file type. Only JPEG, PNG, GIF and WebP images are allowed.'), false);
|
||||
}
|
||||
};
|
||||
|
||||
// Create multer upload middleware for profile images
|
||||
const uploadProfileImage = multer({
|
||||
storage: profileImageStorage,
|
||||
fileFilter: imageFileFilter,
|
||||
limits: {
|
||||
fileSize: 5 * 1024 * 1024 // 5MB limit
|
||||
}
|
||||
}).single('profileImage');
|
||||
|
||||
module.exports = {
|
||||
uploadProfileImage
|
||||
};
|
||||
Reference in New Issue
Block a user