-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
179 lines (162 loc) · 6.07 KB
/
app.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require('bcryptjs');
const crypto = require('crypto');
const { usersDB, recordsDB } = require('./init-db.js');
const json2csv = require('json2csv').parse;
const path = require('path');
const cors = require('cors');
const session = require('express-session');
// Secret key for session, randomly generated
let SECRET_KEY = crypto.randomBytes(32).toString('hex');
// Create the Express app
const app = express();
const port = 3001;
const corsOptions = {
credentials: true,
optionsSuccessStatus: 204
};
app.use(cors(corsOptions));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'dist')));
// Use session middleware
app.use(session({
secret: SECRET_KEY,
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: true, // Ensures the cookie is sent as HttpOnly
secure: true, // Set to true if you are using HTTPS
}
}));
let requireAdmin = (req, res, next) => {
if (!req.session || req.session.admin !== true)
return res.status(403).json({ error: 'Admin privileges required' });
next();
};
// Main route
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
// Route to get records by PIN
app.post('/get-records', (req, res) => {
recordsDB.find({}, (err, rows) => {
if (err) return res.status(500).json({ error: err.message });
res.json(rows);
});
});
// Route to get all users
app.post('/get-users', (req, res) => {
usersDB.find({}, (err, rows) => {
if (err) return res.status(500).json({ error: err.message });
res.json(rows);
});
});
// Route to download records as CSV
app.post('/download-records', (req, res) => {
recordsDB.find({}, (err, rows) => {
if (err) return res.status(500).json({ error: err.message });
// Convert records to CSV
const fields = ['name', 'pin', 'action', 'time', 'ip'];
const opts = { fields };
const csv = json2csv(rows, opts);
// Set the response header
res.setHeader('Content-disposition', 'attachment; filename=records.csv');
res.set('Content-Type', 'text/csv');
res.status(200).send(csv);
});
});
// Route to add record
app.post('/add-record', (req, res) => {
const { pin, action, time, ip } = req.body;
// Check if a user with the provided PIN exists
usersDB.findOne({ pin }, (err, user) => {
if (err) return res.status(500).json({ error: err.message });
if (!user) return res.status(400).json({ error: 'No user with this PIN' });
// Find the name associated with the PIN
const name = user.name;
// Insert the new record along with the IP
recordsDB.insert({ name, pin, action, time, ip }, (err, newRecord) => {
if (err) return res.status(500).json({ error: err.message });
// Return the new record ID and name
res.status(201).json({ id: newRecord._id, name });
});
});
});
// Route to login
app.post('/login', (req, res) => {
const { username, password } = req.body;
// Find the user in the database
usersDB.findOne({ username }, (err, user) => {
if (err) return res.status(500).json({ error: err.message });
// Verify password
if (!user) return res.status(401).json({ error: 'No user with those credentials' });
const passwordIsValid = bcrypt.compareSync(password, user.password);
if (!passwordIsValid) return res.status(401).json({ error: 'Invalid credentials' });
// Set admin flag in session
req.session.admin = true;
req.session.save(err => {
if (err) return res.status(500).json({ error: err.message });
res.json({ success: true });
});
});
});
// Route to logout
app.post('/logout', (req, res) => {
// Destroy the session
req.session.destroy((err) => {
if (err) return res.status(500).json({ error: err.message });
res.json({ success: true });
});
});
// Route to check if user is logged in
app.get('/is-logged-in', (req, res) => {
res.json({ isLoggedIn: !!req.session.admin });
});
// Route to add user
app.post('/add-admin', (req, res) => {
const { username, password } = req.body;
// Check if username already exists
usersDB.findOne({ username }, (err, existingUser) => {
if (err) return res.status(500).json({ error: err.message });
if (existingUser) return res.status(400).json({ error: 'Username already exists' });
// Hash the password
const hashedPassword = bcrypt.hashSync(password, 8);
// Generate a login token
const loginToken = crypto.randomBytes(16).toString('hex');
// Insert the new user
usersDB.insert({ username, password: hashedPassword, loginToken: loginToken }, (err, newUser) => {
if (err) return res.status(500).json({ error: err.message });
// Return the new user ID and login token
res.status(201).json({ id: newUser._id, token: loginToken });
});
});
});
// Route to add employee
app.post('/add-employee', (req, res) => {
const { name, pin } = req.body;
// Check if PIN already exists
usersDB.findOne({ pin }, (err, existingEmployee) => {
if (err) return res.status(500).json({ error: err.message });
if (existingEmployee) return res.status(400).json({ error: 'PIN already exists' });
// Logic to add employee to the database
usersDB.insert({ name, pin }, (err, newEmployee) => {
if (err) return res.status(500).json({ error: err.message });
// Return the new employee ID
res.status(201).json({ id: newEmployee._id });
});
});
});
if (process.env.NODE_ENV === 'production') {
// Export the app for production (e.g., when using Phusion Passenger)
module.exports = app;
module.exports.db = {
usersDB,
recordsDB
};
} else {
// Start the server for local development and testing
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
}