-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.js
215 lines (178 loc) · 5.76 KB
/
Server.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const express = require('express');
const sqlite3 = require('sqlite3').verbose();
const app = express();
const port = 3000;
const bcrypt = require('bcrypt');
const saltRounds = 10; // Adjust based on your security needs
app.use(express.static('public'));
app.use(express.json());
// Connect to SQLite database
// Connect to SQLite database
const db = new sqlite3.Database('./database.db', (err) => {
if (err) {
console.error('Error connecting to database:', err.message);
} else {
console.log('Connected to the database');
}
});
// Define the insert query for user signup
const insertQuery = 'INSERT INTO users (username, password) VALUES (?, ?)';
// Create a table for user accounts
db.run(`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
password TEXT NOT NULL
)
`);
// Create a table for quiz questions
db.run(`
CREATE TABLE IF NOT EXISTS quiz_questions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
question TEXT NOT NULL,
answer INTEGER NOT NULL
)
`);
// Create a table for quiz attempts
db.run(`
CREATE TABLE IF NOT EXISTS quiz_attempts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
question_id INTEGER,
user_answer INTEGER,
is_correct INTEGER,
start_time DATETIME,
end_time DATETIME,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (question_id) REFERENCES quiz_questions(id)
)
`);
// Add this after your existing routes
// Create a table for user sessions
db.run(`
CREATE TABLE IF NOT EXISTS user_sessions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
session_id TEXT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id)
)
`);
// Generate a random session ID
function generateSessionId() {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const sessionIdLength = 32;
let sessionId = '';
for (let i = 0; i < sessionIdLength; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
sessionId += characters.charAt(randomIndex);
}
return sessionId;
}
// Create a user session
app.post('/create_session', (req, res) => {
const { user_id } = req.body;
const sessionId = generateSessionId();
db.run(
'INSERT INTO user_sessions (user_id, session_id) VALUES (?, ?)',
[user_id, sessionId],
(err) => {
if (err) {
console.error(err.message);
return res.status(500).send('Internal Server Error');
}
res.status(200).json({ session_id: sessionId });
}
);
});
// Handle user signup
app.post('/signup', async (req, res) => {
console.log('Received signup request:', req.body);
const { username, password } = req.body;
console.log('Received signup request:', { username, password });
try {
const hashedPassword = await bcrypt.hash(password, saltRounds);
await new Promise((resolve, reject) => {
db.run(insertQuery, [username, hashedPassword], (err) => {
if (err) {
console.error('Error during INSERT operation:', err.message);
reject(err);
} else {
console.log('User account created successfully!');
resolve();
}
});
});
res.status(200).send('User account created successfully!');
} catch (error) {
res.status(500).send('Internal Server Error');
}
});
app.post('/login', async (req, res) => {
const { username, password } = req.body;
console.log('Received login request:', { username, password });
try {
const row = await new Promise((resolve, reject) => {
// Check if the username exists in the database
db.get('SELECT id, password FROM users WHERE username = ?', [username], (err, row) => {
if (err) {
reject(err);
} else {
resolve(row);
}
});
});
console.log('User found in the database:', row);
if (row) {
const match = await bcrypt.compare(password, row.password);
if (match) {
res.status(200).json({ userId: row.id });
} else {
res.status(401).json({ error: 'Invalid username or password' });
}
} else {
res.status(401).json({ error: 'Invalid username or password' });
}
} catch (error) {
console.error(error.message);
res.status(500).json({ error: 'Internal Server Error' });
}
});
// Get a random quiz question for the user
app.get('/get-quiz-question', (req, res) => {
db.get('SELECT * FROM quiz_questions ORDER BY RANDOM() LIMIT 1', (err, row) => {
if (err) {
console.error(err.message);
return res.status(500).send('Internal Server Error');
}
res.status(200).json(row);
});
});
// Record a quiz attempt
app.post('/record-quiz-attempt', (req, res) => {
const { user_id, question_id, user_answer, is_correct, start_time, end_time } = req.body;
db.run(
'INSERT INTO quiz_attempts (user_id, question_id, user_answer, is_correct, start_time, end_time) VALUES (?, ?, ?, ?, ?, ?)',
[user_id, question_id, user_answer, is_correct, start_time, end_time],
(err) => {
if (err) {
console.error(err.message);
return res.status(500).send('Internal Server Error');
}
res.status(200).json({ message: 'Quiz attempt recorded successfully!' });
}
);
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
// Close the SQLite database connection on application shutdown
process.on('SIGINT', () => {
db.close((err) => {
if (err) {
console.error(err.message);
} else {
console.log('Disconnected from the database');
process.exit(0);
}
});
});