Files
rentall-app/backend/routes/users.js
2025-07-17 00:16:01 -04:00

55 lines
1.4 KiB
JavaScript

const express = require('express');
const { User } = require('../models'); // Import from models/index.js to get models with associations
const { authenticateToken } = require('../middleware/auth');
const router = express.Router();
router.get('/profile', authenticateToken, async (req, res) => {
try {
const user = await User.findByPk(req.user.id, {
attributes: { exclude: ['password'] }
});
res.json(user);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
router.get('/:id', async (req, res) => {
try {
const user = await User.findByPk(req.params.id, {
attributes: { exclude: ['password', 'email', 'phone', 'address'] }
});
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
res.json(user);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
router.put('/profile', authenticateToken, async (req, res) => {
try {
const { firstName, lastName, phone, address, profileImage } = req.body;
await req.user.update({
firstName,
lastName,
phone,
address,
profileImage
});
const updatedUser = await User.findByPk(req.user.id, {
attributes: { exclude: ['password'] }
});
res.json(updatedUser);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
module.exports = router;