Initial marketplace implementation
Full-stack marketplace for buying/selling second-hand items. React 19 + TypeScript + Tailwind CSS v4 frontend with 17 screens, Express + Prisma + Socket.io backend, Stripe payments, JWT auth. Deployed at https://marketplace.173.212.212.157.sslip.io/ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
68
server/src/routes/user.ts
Normal file
68
server/src/routes/user.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { Router } from 'express';
|
||||
import { prisma } from '../config/database.js';
|
||||
import { authenticate } from '../middleware/auth.js';
|
||||
import { hashPassword, comparePassword } from '../utils/password.js';
|
||||
import { AppError } from '../middleware/errorHandler.js';
|
||||
|
||||
const router = Router();
|
||||
|
||||
const userSelect = {
|
||||
id: true, email: true, fullName: true, nickname: true, avatar: true,
|
||||
phone: true, location: true, bio: true, rating: true,
|
||||
showEmail: true, showPhone: true, showLocation: true,
|
||||
createdAt: true,
|
||||
};
|
||||
|
||||
router.get('/profile', authenticate, async (req, res, next) => {
|
||||
try {
|
||||
const user = await prisma.user.findUnique({ where: { id: req.userId }, select: userSelect });
|
||||
if (!user) throw new AppError(404, 'User not found');
|
||||
res.json(user);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
router.put('/profile', authenticate, async (req, res, next) => {
|
||||
try {
|
||||
const { fullName, nickname, phone, location, bio, showEmail, showPhone, showLocation } = req.body;
|
||||
const user = await prisma.user.update({
|
||||
where: { id: req.userId },
|
||||
data: { fullName, nickname, phone, location, bio, showEmail, showPhone, showLocation },
|
||||
select: userSelect,
|
||||
});
|
||||
res.json(user);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
router.put('/password', authenticate, async (req, res, next) => {
|
||||
try {
|
||||
const { currentPassword, newPassword } = req.body;
|
||||
const user = await prisma.user.findUnique({ where: { id: req.userId } });
|
||||
if (!user) throw new AppError(404, 'User not found');
|
||||
|
||||
const valid = await comparePassword(currentPassword, user.passwordHash);
|
||||
if (!valid) throw new AppError(400, 'Current password is incorrect');
|
||||
|
||||
const passwordHash = await hashPassword(newPassword);
|
||||
await prisma.user.update({ where: { id: req.userId }, data: { passwordHash } });
|
||||
|
||||
res.json({ message: 'Password updated' });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/:id', async (req, res, next) => {
|
||||
try {
|
||||
const user = await prisma.user.findUnique({ where: { id: req.params.id }, select: userSelect });
|
||||
if (!user) throw new AppError(404, 'User not found');
|
||||
res.json(user);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user