-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend_database.js
201 lines (177 loc) · 6.12 KB
/
backend_database.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
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const path = require('path');
const bcrypt = require('bcrypt');
const crypto = require('crypto');
const nodemailer = require('nodemailer');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
// Database connection
mongoose.connect('mongodb+srv://dbEsteemJK:[email protected]/?retryWrites=true&w=majority&appName=Esteem-JK', { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("Database connection successful"))
.catch(err => console.error("Database connection error:", err));
// Configure Nodemailer
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'vkff fnsy cfea qhun'
}
});
// Dynamic schema and model function
const getUserModel = (organization) => {
const userSchema = new mongoose.Schema({
name: String,
email: { type: String, unique: true },
password: String,
age: Number,
organization: String,
otp: String,
otpExpires: Date
});
userSchema.pre('save', function (next) {
if (this.isModified('password')) {
bcrypt.hash(this.password, 10, (err, hash) => {
if (err) return next(err);
this.password = hash;
next();
});
} else {
next();
}
});
return mongoose.model(organization.toLowerCase().replace(/\s+/g, '_'), userSchema, organization.toLowerCase().replace(/\s+/g, '_'));
};
// Send OTP route
app.get('/send-otp', async (req, res) => {
const { email, organization } = req.query;
const otp = crypto.randomInt(100000, 999999).toString();
const otpExpires = new Date(Date.now() + 15 * 60 * 1000);
const User = getUserModel(organization);
try {
const user = await User.findOneAndUpdate(
{ email },
{ otp, otpExpires },
{ new: true, upsert: true }
);
if (user) {
const mailOptions = {
from: '[email protected]',
to: email,
subject: 'Your OTP Code',
text: `Your OTP code is ${otp}. It is valid for 15 minutes.`
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error('Error sending OTP:', error);
res.status(500).json({ success: false, message: 'Failed to send OTP' });
} else {
res.json({ success: true });
}
});
} else {
res.status(400).json({ success: false, message: 'Failed to generate OTP' });
}
} catch (error) {
res.status(500).json({ success: false, message: 'Server error' });
}
});
// Verify OTP route
app.post('/verify-otp', async (req, res) => {
const { email, otp, organization } = req.body;
const User = getUserModel(organization);
try {
const user = await User.findOne({ email });
if (user && user.otp === otp && new Date() <= user.otpExpires) {
res.json({ success: true });
} else {
res.status(400).json({ success: false, message: 'Invalid or expired OTP' });
}
} catch (error) {
console.error("Error verifying OTP:", error);
res.status(500).json({ success: false, message: 'Server error' });
}
});
// User signup route
app.post('/signup', async (req, res) => {
const { name, email, password, age, otp, organization } = req.body;
const collectionName = organization.toLowerCase().replace(/\s+/g, '_');
const User = getUserModel(organization);
try {
const collections = await mongoose.connection.db.listCollections({ name: collectionName }).toArray();
if (collections.length > 0) {
console.log(`Organization '${organization}' exists, adding user to the collection.`);
} else {
console.log(`Organization '${organization}' does not exist, creating new collection.`);
}
const existingUser = await User.findOne({ email });
// If the user does not exist
if (!existingUser) {
if (otp) {
const newUser = new User({
name,
email,
password,
age,
organization,
otp, // Store OTP in case you need it
otpExpires: new Date(Date.now() + 15 * 60 * 1000) // New expiry time for OTP
});
await newUser.save();
console.log("User created successfully");
return res.redirect('/front-end.html');
} else {
return res.status(400).send("OTP is required");
}
} else {
// If the user exists, verify the OTP
if (existingUser.otp !== otp || new Date() > existingUser.otpExpires) {
return res.status(400).send("Invalid or expired OTP");
}
// Update user details if OTP is valid
existingUser.name = name;
existingUser.password = password; // This will trigger the password hash process in the pre-save hook
existingUser.age = age;
existingUser.organization = organization;
await existingUser.save();
console.log("User updated successfully");
return res.redirect('/front-end.html');
}
} catch (error) {
console.error("Error during signup:", error);
res.status(500).send("Error during signup");
}
});
// User login route
app.post('/login', (req, res) => {
const { email, password, organization } = req.body;
const User = getUserModel(organization);
User.findOne({ email })
.then(user => {
if (user) {
bcrypt.compare(password, user.password, (err, result) => {
if (err) {
console.error("Error comparing passwords:", err);
res.status(500).send("Error logging in");
} else if (result) {
res.redirect('/front-end.html');
} else {
res.status(401).send("Login Failed: Incorrect Email or Password");
}
});
} else {
res.status(401).send("Login Failed: Incorrect Email or Password");
}
})
.catch(error => {
console.error("Error logging in:", error);
res.status(500).send("Error logging in");
});
});
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});