-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.js
More file actions
521 lines (459 loc) · 13.5 KB
/
Copy pathweb.js
File metadata and controls
521 lines (459 loc) · 13.5 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
import express from 'express';
import path from 'path';
import session from 'express-session';
import admin from 'firebase-admin';
import { InferenceClient } from "@huggingface/inference";
import config from './config.json' with { type: "json" };
import fbAdminConfig from './fbadmin.json' with { type: "json" };
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express();
const port = config.port;
admin.initializeApp({
credential: admin.credential.cert(fbAdminConfig)
});
const db = admin.firestore();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(session({
secret: 'mindmirror-secret',
resave: false,
saveUninitialized: false
}));
function isAdmin(req) {
return req.session.user && config.admin_emails.includes(req.session.user.email);
}
function isAuthenticated(req) {
return req.session.user;
}
// serve static files
app.use(express.static(__dirname));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('/login', (req, res) => {
res.sendFile(path.join(__dirname, 'login.html'));
});
app.post('/login', async (req, res) => {
const { email, password, token } = req.body;
if (!email || !password || !token) {
return res.status(400).send('Missing fields');
}
try {
const fetch = await import('node-fetch').then(m => m.default);
const verifyResponse = await fetch('https://www.google.com/recaptcha/api/siteverify', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: `secret=${config.recaptcha_secret_key}&response=${token}`
});
const verifyData = await verifyResponse.json();
if (!verifyData.success || verifyData.score < 0.5) {
return res.status(400).send('reCAPTCHA failed');
}
// simple session auth for demo
req.session.user = { email };
if (config.admin_emails.includes(email)) {
return res.redirect('/admin');
} else {
return res.redirect('/test');
}
} catch (err) {
console.error(err);
res.status(500).send('Server error');
}
});
app.post('/signup', async (req, res) => {
const { name, email, password, confirm, token } = req.body;
if (!name || !email || !password || !confirm || !token) {
return res.status(400).send('Missing fields');
}
if (password !== confirm) {
return res.status(400).send('Passwords do not match');
}
try {
const fetch = await import('node-fetch').then(m => m.default);
const verifyResponse = await fetch('https://www.google.com/recaptcha/api/siteverify', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: `secret=${config.recaptcha_secret_key}&response=${token}`
});
const verifyData = await verifyResponse.json();
if (!verifyData.success || verifyData.score < 0.5) {
return res.status(400).send('reCAPTCHA failed');
}
const userRecord = await admin.auth().createUser({
email: email,
password: password,
displayName: name
});
console.log(`Firebase user created: ${userRecord.uid}`);
res.redirect('/test');
} catch (err) {
console.error(err);
res.status(500).send(`Error: ${err.message}`);
}
});
// admin panel protected
app.get('/admin', (req, res) => {
if (req.session.user && config.admin_emails.includes(req.session.user.email)) {
res.sendFile(path.join(__dirname, 'admin.html'));
} else {
res.redirect('/login');
}
});
// API to get questions
app.get('/api/questions', async (req, res) => {
if (!isAdmin(req)) {
res.redirect('/login');
}
try {
const snapshot = await db.collection('questions').get();
const questions = [];
snapshot.forEach(doc => {
questions.push({ id: doc.id, ...doc.data() });
});
res.json(questions);
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Server error' });
}
});
// add question
app.post('/api/questions/add', async (req, res) => {
if (!isAdmin(req)) {
return res.status(403).send('Forbidden');
}
const { text, options } = req.body;
try {
// check for duplicate
const duplicate = await db.collection('questions').where('text', '==', text).get();
if (!duplicate.empty) {
return res.status(400).json({ error: 'Question already exists' });
}
const ref = await db.collection('questions').add({ text, options });
res.json({ id: ref.id });
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Add failed' });
}
});
// update question
app.post('/api/questions/update', async (req, res) => {
if (!isAdmin(req)) {
return res.status(403).send('Forbidden');
}
const { id, text, options } = req.body;
try {
await db.collection('questions').doc(id).update({ text, options });
res.json({ success: true });
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Update failed' });
}
});
// bulk delete
app.post('/api/questions/delete', async (req, res) => {
if (!isAdmin(req)) {
return res.status(403).send('Forbidden');
}
const { ids } = req.body;
try {
for (const id of ids) {
await db.collection('questions').doc(id).delete();
}
res.json({ success: true });
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Delete failed' });
}
});
// get users list
app.get('/api/users', async (req, res) => {
if (!isAdmin(req)) {
return res.redirect('/login');
}
try {
const listUsers = await admin.auth().listUsers(1000); // max 1000
const users = listUsers.users.map(u => ({
uid: u.uid,
email: u.email,
displayName: u.displayName || '',
admin: u.customClaims && u.customClaims.admin === true
}));
res.json(users);
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Failed to list users' });
}
});
// delete user
app.post('/api/users/delete', async (req, res) => {
if (!isAdmin(req)) {
return res.status(403).send('Forbidden');
}
const { uid } = req.body;
try {
await admin.auth().deleteUser(uid);
res.json({ success: true });
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Delete failed' });
}
});
app.post('/api/questions/import', async (req, res) => {
if (!isAdmin(req)) {
return res.status(403).send('Forbidden');
}
try {
const questions = req.body.questions;
for(const q of questions){
// check duplicates
const exists = await db.collection('questions').where('text','==',q.text).get();
if(exists.empty){
await db.collection('questions').add({
text: q.text,
options: q.options
});
}
}
res.send("Import done");
} catch(err){
console.error(err);
res.status(500).send("Import failed");
}
});
app.get('/api/users', async (req, res) => {
if (!isAdmin(req)) {
return res.redirect('/login');
}
try {
const listUsers = await admin.auth().listUsers(1000);
const users = listUsers.users.map(u => ({
uid: u.uid,
name: u.displayName || '',
email: u.email || '',
createdAt: u.metadata.creationTime,
disabled: u.disabled || false
}));
res.json(users);
} catch(err){
console.error(err);
res.status(500).send('Error fetching users');
}
});
app.post('/api/users/update', async (req, res) => {
const { uid, name, email } = req.body;
try {
await admin.auth().updateUser(uid, { displayName: name, email });
res.send('Updated');
} catch(err){
console.error(err);
res.status(500).send('Update failed');
}
});
app.post('/api/users/delete', async (req, res) => {
const { uid } = req.body;
try {
await admin.auth().deleteUser(uid);
res.send('Deleted');
} catch(err){
console.error(err);
res.status(500).send('Delete failed');
}
});
app.post('/api/users/disable', async (req, res) => {
const { uid, disable } = req.body;
try {
await admin.auth().updateUser(uid, { disabled: disable });
res.send('Status updated');
} catch(err){
console.error(err);
res.status(500).send('Failed to update status');
}
});
app.post('/api/users/add', async (req, res) => {
if (!isAdmin(req)) {
return res.status(403).send('Forbidden');
}
const { name, email, password, role } = req.body;
if (!name || !email || !password || !role) {
return res.status(400).send("Missing fields");
}
try {
const userRecord = await admin.auth().createUser({
displayName: name,
email,
password,
});
if(role==="admin"){
await admin.auth().setCustomUserClaims(userRecord.uid, {admin:true});
}
res.send("Created");
} catch(err){
console.error(err);
res.status(500).send(err.message);
}
});
app.get('/api/questions', async (req,res)=>{
try {
const snapshot = await db.collection("questions").get();
const questions = [];
snapshot.forEach(doc=>{
questions.push({id:doc.id, ...doc.data()});
});
res.json(questions);
} catch(err){
console.error(err);
res.status(500).send("Error loading questions");
}
});
app.get('/test', (req, res) => {
if (!isAuthenticated(req)) {
return res.redirect('/login');
}
res.sendFile(path.join(__dirname, 'test.html'));
});
app.post('/test', async (req, res) => {
if (!isAuthenticated(req)) {
return res.status(403).send('Not logged in');
}
const rawAnswers = req.body.answers;
const questionsSnap = await db.collection('questions').get();
const questionList = [];
questionsSnap.forEach(doc => {
questionList.push(doc.data().text);
});
const answers = {};
Object.keys(rawAnswers).forEach((key, idx) => {
const qIndex = parseInt(key.replace('q', ''));
const questionText = questionList[qIndex];
if (questionText) {
answers[questionText] = rawAnswers[key];
}
});
if (!answers || Object.keys(answers).length < 1) {
return res.status(400).send('At least one question must be answered.');
}
try {
const userEmail = req.session.user.email;
// Save or replace by using email as document ID
const docRef = db.collection('results').doc(userEmail);
await docRef.set({
user: userEmail,
answers,
created: new Date()
});
const summarizedAnswers = Object.entries(answers).slice(0, 30).map(([k, v]) => `${k}: ${v}`).join(", ");
const prompt = `
I just finished a mental health assessment. Top answers:
${summarizedAnswers}
Write me a paragraph on my mental and psychological health evaluation discussing the problems if any and it's solutions in around 300 words.
`;
const hfClient = new InferenceClient(config.hugging_face_key);
const aiResponse = await hfClient.chatCompletion({
model: "mistralai/Mistral-7B-Instruct-v0.3",
provider: "novita",
messages: [
{ role: "system", content: "Mental and psychological health evaluation" },
{ role: "user", content: prompt }
]
});
const evaluationText = aiResponse.choices[0].message.content;
// Update the same document with evaluation
await docRef.update({ evaluation: evaluationText });
res.json({ evaluation: evaluationText });
} catch (err) {
console.error(err);
res.status(500).send('Error evaluating');
}
});
app.get('/api/questions-user', async (req,res)=>{
try{
const snap = await db.collection('questions').get();
const list = [];
snap.forEach(doc=>{
list.push({ id: doc.id, ...doc.data() });
});
res.json(list);
}catch(e){
console.error(e);
res.status(500).send('Failed to load questions');
}
});
app.get('/test', (req,res)=>{
res.sendFile(path.join(__dirname,'test.html'));
});
app.get('/logout', (req, res) => {
req.session.destroy(() => {
res.redirect('/login');
});
});
app.get('/api/stats', async (req, res) => {
if (!isAdmin(req)) {
return res.status(403).send('Forbidden');
}
try {
const snapshot = await db.collection('results').get();
const totalAttempts = snapshot.size;
res.json({ totalAttempts });
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Server error' });
}
});
app.post('/api/feedback', async (req, res) => {
try {
const { rating, feedback } = req.body;
if (!isAuthenticated(req)) return res.status(403).send("Not logged in");
await db.collection("feedback").add({
user: req.session.user.email,
rating: rating ? Number(rating) : null,
feedback,
created: new Date()
});
res.json({ success: true });
} catch(err){
console.error(err);
res.status(500).send("Feedback failed");
}
});
app.get('/api/feedback', async (req, res) => {
try {
const snapshot = await db.collection('feedbacks').get();
const feedbacks = snapshot.docs.map(doc => ({
id: doc.id,
...doc.data()
}));
res.json(feedbacks);
} catch (err) {
console.error(err);
res.status(500).send("Could not fetch feedback");
}
});
app.get('/api/reports/:email', async (req, res) => {
const { email } = req.params;
try {
const docRef = db.collection('results').doc(email);
const snapshot = await docRef.get();
if (!snapshot.exists) {
return res.status(404).send("Report not found");
}
const data = snapshot.data();
res.json({
email,
name: data.name || '',
rating: data.rating || '',
feedback: data.feedback || '',
evaluation: data.evaluation || '',
answers: data.answers || []
});
} catch (err) {
console.error(err);
res.status(500).send("Server error");
}
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});