-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
95 lines (80 loc) · 2.61 KB
/
Copy pathserver.js
File metadata and controls
95 lines (80 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import express from 'express';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import jwt from 'jsonwebtoken';
import bcrypt from 'bcryptjs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express();
const PORT = 3000;
// Mock database
const users = [];
const products = [];
const messages = [];
app.use(express.json());
app.use(express.static('public'));
// Authentication middleware
const authenticateToken = (req, res, next) => {
const token = req.headers['authorization']?.split(' ')[1];
if (!token) return res.sendStatus(401);
jwt.verify(token, 'secret_key', (err, user) => {
if (err) return res.sendStatus(403);
req.user = user;
next();
});
};
// Auth routes
app.post('/api/register', async (req, res) => {
try {
const { username, password, role } = req.body;
const hashedPassword = await bcrypt.hash(password, 10);
users.push({ username, password: hashedPassword, role });
res.status(201).json({ message: 'User registered successfully' });
} catch (error) {
res.status(500).json({ error: 'Error registering user' });
}
});
app.post('/api/login', async (req, res) => {
try {
const { username, password } = req.body;
const user = users.find(u => u.username === username);
if (!user) return res.status(400).json({ error: 'User not found' });
const validPassword = await bcrypt.compare(password, user.password);
if (!validPassword) return res.status(400).json({ error: 'Invalid password' });
const token = jwt.sign({ username: user.username, role: user.role }, 'secret_key');
res.json({ token });
} catch (error) {
res.status(500).json({ error: 'Error logging in' });
}
});
// Product routes
app.get('/api/products', (req, res) => {
res.json(products);
});
app.post('/api/products', authenticateToken, (req, res) => {
if (req.user.role !== 'farmer') {
return res.status(403).json({ error: 'Only farmers can create listings' });
}
const product = { ...req.body, farmer: req.user.username };
products.push(product);
res.status(201).json(product);
});
// Message routes
app.post('/api/messages', authenticateToken, (req, res) => {
const message = {
...req.body,
from: req.user.username,
timestamp: new Date()
};
messages.push(message);
res.status(201).json(message);
});
app.get('/api/messages', authenticateToken, (req, res) => {
const userMessages = messages.filter(m =>
m.from === req.user.username || m.to === req.user.username
);
res.json(userMessages);
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});