-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
177 lines (155 loc) · 5.28 KB
/
Copy pathserver.js
File metadata and controls
177 lines (155 loc) · 5.28 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
const express = require('express');
const mysql = require('mysql2');
const bcrypt = require('bcrypt');
const nodemailer = require('nodemailer');
const crypto = require('crypto');
const app = express();
const port = 3000;
app.use(express.json());
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '981021@Vuyo',
database: 'LOGBOOK',
});
// Check the MySQL connection
db.connect((err) => {
if (err) {
console.error('Error connecting to MySQL:', err);
} else {
console.log('Connected to MySQL database');
}
});
// Function to send verification email
const transporter = nodemailer.createTransport({
// Configure your email service here
service: 'gmail',
auth: {
user: 'YOUR_EMAIL@gmail.com',
pass: 'YOUR_EMAIL_PASSWORD',
},
});
function sendVerificationEmail(email, verificationCode) {
const mailOptions = {
from: 'YOUR_EMAIL@gmail.com',
to: email,
subject: 'Account Verification Code',
text: `Your verification code is: ${verificationCode}`,
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error(error);
} else {
console.log('Verification code email sent: ' + info.response);
}
});
}
// Student Registration Endpoint
app.post('/register/student', async (req, res) => {
const { studentNumber, email, password } = req.body;
const hashedPassword = await bcrypt.hash(password, 10);
const verificationCode = crypto.randomBytes(6).toString('hex');
// Save the verification code in the database
db.query(
'INSERT INTO students (studentNumber, email, password, verificationCode) VALUES (?, ?, ?, ?)',
[studentNumber, email, hashedPassword, verificationCode],
(err, _result) => {
if (err) {
console.error(err);
res.status(500).send('Error registering student');
} else {
// Send the verification code via email
sendVerificationEmail(email, verificationCode);
res.status(201).send('Student registered successfully');
}
}
);
});
// Supervisor Registration Endpoint
app.post('/register/supervisor', async (req, res) => {
const { supervisorName, email, password } = req.body;
const hashedPassword = await bcrypt.hash(password, 10);
const verificationCode = crypto.randomBytes(6).toString('hex');
// Save the verification code in the database
db.query(
'INSERT INTO supervisors (supervisorName, email, password, verificationCode) VALUES (?, ?, ?, ?)',
[supervisorName, email, hashedPassword, verificationCode],
(err, result) => {
if (err) {
console.error(err);
res.status(500).send('Error registering supervisor');
} else {
// Send the verification code via email
sendVerificationEmail(email, verificationCode);
res.status(201).send('Supervisor registered successfully');
}
}
);
});
// Update the password in the database using the verification code
app.post('/reset/student/confirm', async (req, res) => {
const { email, verificationCode, newPassword } = req.body;
const hashedNewPassword = await bcrypt.hash(newPassword, 10);
// Verify the verification code from the database
db.query(
'SELECT * FROM students WHERE email = ? AND verificationCode = ?',
[email, verificationCode],
async (err, result) => {
if (err) {
console.error(err);
res.status(500).send('Error verifying verification code');
} else if (result.length === 0) {
res.status(401).send('Invalid verification code or email');
} else {
// Update the password in the database
db.query(
'UPDATE students SET password = ? WHERE email = ? AND verificationCode = ?',
[hashedNewPassword, email, verificationCode],
(updateErr, updateResult) => {
if (updateErr) {
console.error(updateErr);
res.status(500).send('Error updating password');
} else {
res.status(200).send('Password reset successful');
}
}
);
}
}
);
});
// Update the password in the database using the verification code
app.post('/reset/supervisor/confirm', async (req, res) => {
const { email, verificationCode, newPassword } = req.body;
const hashedNewPassword = await bcrypt.hash(newPassword, 10);
// Verify the verification code from the database
db.query(
'SELECT * FROM supervisors WHERE email = ? AND verificationCode = ?',
[email, verificationCode],
async (err, result) => {
if (err) {
console.error(err);
res.status(500).send('Error verifying verification code');
} else if (result.length === 0) {
res.status(401).send('Invalid verification code or email');
} else {
// Update the password in the database
db.query(
'UPDATE supervisors SET password = ? WHERE email = ? AND verificationCode = ?',
[hashedNewPassword, email, verificationCode],
(updateErr, updateResult) => {
if (updateErr) {
console.error(updateErr);
res.status(500).send('Error updating password');
} else {
res.status(200).send('Password reset successful');
}
}
);
}
}
);
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});