-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
221 lines (182 loc) · 6.2 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
216
217
218
219
220
221
const express = require('express');
const app = express();
const session = require('express-session');
const MongoDBSession = require('connect-mongodb-session')(session);
const bcrypt = require('bcryptjs');
const mongoose = require('mongoose');
const path = require('path');
const username = 'jamesthorley291';
const password = encodeURIComponent('iMiiCas150Penena');
// Models
const Meeting = require('./models/Meeting');
const PersonalSupervisor = require('./models/Personalsupervisor');
const SelfReport = require('./models/Selfreport');
const SeniorTutor = require('./models/Seniortutor');
const Relationship = require('./models/Relationship');
const Student = require('./models/Student');
const mongoUri = `mongodb+srv://${username}:${password}@cluster0.jxgtgxt.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0`;
const PORT = process.env.PORT || 3000;
// Connect to MongoDB
mongoose.connect(mongoUri)
.then(() => {
console.log('Connected to MongoDB');
})
.catch((err) => {
console.error('Error connecting to MongoDB:', err);
});
const store = new MongoDBSession({
uri: mongoUri,
collection: 'mySessions',
});
app.use(session({
secret: 'key that will sign cookie',
resave: false,
saveUninitialized: false,
store: store,
}));
const isAuth = (req, res, next) => {
if (req.session.isAuth) {
next();
} else {
res.redirect('/login');
}
};
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));
// Login route
app.post("/login", async (req, res) => {
const { username, password, userType } = req.body;
let model;
switch (userType) {
case 'student':
model = Student;
break;
case 'personalSupervisor':
model = PersonalSupervisor;
break;
case 'seniorTutor':
model = SeniorTutor;
break;
default:
console.error(`Login attempt with invalid user type: ${userType}`);
res.status(400).send("Invalid user type");
return;
}
try {
const user = await model.findOne({ username: username.trim() });
if (!user) {
console.error(`Login failed for non-existent username: ${username}`);
return res.redirect('/login'); // Consider using flash messages for user feedback
}
const isMatch = await bcrypt.compare(password, user.passwordHash);
if (!isMatch) {
console.error(`Password mismatch for user: ${username}`);
return res.redirect("/login"); // Consider using flash messages for user feedback
}
req.session.isAuth = true;
req.session.userId = user._id;
req.session.userType = userType;
console.log(`User ${username} logged in as ${userType}`);
// Redirect based on user type
switch (userType) {
case 'student':
res.redirect('/studentDashboard');
break;
case 'personalSupervisor':
res.redirect('/supervisorDashboard');
break;
case 'seniorTutor':
res.redirect('/seniorTutorDashboard');
break;
}
} catch (err) {
console.error(`Error during login for ${username}: ${err.message}`, err);
res.status(500).send("Login error");
}
});
// User logout
app.post('/logout', (req, res) => {
req.session.destroy(err => {
if (err) {
console.error(err);
res.status(500).send('Could not log out, please try again.');
} else {
res.clearCookie('connect.sid');
res.redirect('/login');
}
});
});
app.post('/reports/create', isAuth , async (req, res) => {
// Check for authentication first
if (!req.session.isAuth) {
return res.status(401).send('Unauthorized: No access to create reports.');
}
// Extract data from the request body
const { reportContent, reportDate = new Date() } = req.body;
// Retrieve the studentId from session (assuming you store this upon login)
const studentId = req.session.userId; // Adjust based on your session setup
if (!reportContent) {
return res.status(400).send('Report content is required.');
}
try {
// Create a new self report using the SelfReport model
const newSelfReport = new SelfReport({
studentId: studentId,
reportContent: reportContent,
reportDate: reportDate
});
// Save the new self report to the database
await newSelfReport.save();
// Send a success response back to the client
res.status(201).send({ message: 'Self report created successfully', reportId: newSelfReport._id });
} catch (error) {
console.error('Failed to create self report:', error);
res.status(500).send('Error creating self report');
}
});
app.get('/reports/self', async (req, res) => {
try {
const studentId = req.session.userId; // Make sure this is correctly set
console.log("Fetching reports for student ID:", studentId); // Debugging output
const reports = await SelfReport.find({ studentId: studentId }).exec(); // Ensure proper execution
console.log("Reports found:", reports); // Debugging output
res.json({ reports }); // Send the reports back to the client
} catch (error) {
console.error('Error fetching self reports:', error);
res.status(500).send('Failed to retrieve self reports.');
}
});
// Serve HTML files
app.get('/studentDashboard', isAuth, (req, res) => {
if (req.session.userType === 'student') {
res.sendFile(path.join(__dirname, 'public' ,'studentDashboard.html'));
} else {
res.status(403).send("Access Denied");
}
});
app.get('/supervisorDashboard', isAuth, (req, res) => {
if (req.session.userType === 'personalSupervisor') {
res.sendFile(path.join(__dirname, 'views', 'supervisorDashboard.html'));
} else {
res.status(403).send("Access Denied");
}
});
app.get('/seniorTutorDashboard', isAuth, (req, res) => {
if (req.session.userType === 'seniorTutor') {
res.sendFile(path.join(__dirname, 'views', 'seniorTutorDashboard.html'));
} else {
res.status(403).send("Access Denied");
}
});
app.get('/login', (req, res) => {
res.sendFile(path.join(__dirname, 'public' ,'login.html'));
});
app.get('/', (req, res) => {
res.redirect('/login.html');
});
// Start the server
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});