phone auth, image uploading, address broken up

This commit is contained in:
jackiettran
2025-07-30 19:12:56 -04:00
parent 72d79596ce
commit 7c6c120969
17 changed files with 759 additions and 182 deletions

View File

@@ -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 };