-
Notifications
You must be signed in to change notification settings - Fork 1
/
Server.js
290 lines (261 loc) · 8.54 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
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
// Copyright ©2024 SachinAkash01, All rights reserved.
const express = require("express"); // Importing Express.js framework
const axios = require("axios"); // Importing Axios for making HTTP requests
const cors = require("cors"); // Importing CORS middleware for enabling Cross-Origin Resource Sharing
const mongoose = require("mongoose"); // Importing Mongoose for MongoDB interactions
const bcrypt = require("bcryptjs"); // Importing bcrypt for password hashing
const jwt = require("jsonwebtoken"); // Importing JSON Web Token for user authentication
require("dotenv").config();
const app = express(); // Creating an Express application
const nodemailer = require("nodemailer");
const bodyParser = require("body-parser");
const PORT = process.env.PORT || 3001; // Defining the port number
app.use(cors()); // Using CORS middleware to enable Cross-Origin Resource Sharing
app.use(express.json()); // Parsing incoming JSON requests
// MongoDB connection
mongoose
.connect(
"mongodb+srv://SachinAkash01:<password>@iservesl-db.7oh0h24.mongodb.net/iServeSL-DB?retryWrites=true&w=majority",
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
)
.then(() => console.log("Connected to MongoDB"))
.catch((err) => console.error("MongoDB connection error:", err));
// Define User schema
const userSchema = new mongoose.Schema(
{
username: String,
email: String,
password: String,
profession: String,
contact: String,
},
{ versionKey: false }
); // Set versionKey option to false to exclude __v field in the MongoDB cluster
// Define User model
const User = mongoose.model("users", userSchema);
// Define API endpoint for adding a new user
/**
* Add new user.
* @name POST/api/user
* @function
* @memberof module:Server
* @inner
* @param {string} req.body.username - Username of the new user.
* @param {string} req.body.email - Email of the new user.
* @param {string} req.body.password - Password of the new user.
* @param {string} req.body.profession - Profession of the new user.
* @param {string} req.body.contact - Contact number of the new user.
* @returns {Object} Response object indicating success or failure of user addition.
*/
app.post("/api/user", async (req, res) => {
try {
const { username, email, password, profession, contact } = req.body;
// Check if the user already exists
const existingUser = await User.findOne({ email });
if (existingUser) {
return res
.status(400)
.json({ error: "This email has already been used!" });
}
// Hash the password
const hashedPassword = await bcrypt.hash(password, 10);
// Add +94 for the contact number
const finalContactNumber = "+94" + contact;
// Create new user
const newUser = new User({
username,
email,
password: hashedPassword,
profession,
contact: finalContactNumber,
});
// Save the user to the database
await newUser.save();
res.status(201).json({ message: "User added successfully" });
} catch (error) {
console.error("Error adding user:", error);
res.status(500).json({ error: "Failed to add user" });
}
});
// Define API endpoint for retrieving all users
/**
* Retrieve all users.
* @name GET/api/users
* @function
* @memberof module:Server
* @inner
* @returns {Object} Response object containing array of user details.
*/
app.get("/api/users", async (req, res) => {
try {
const users = await User.find();
res.json({ users });
} catch (error) {
console.error("Error retrieving users:", error);
res.status(500).json({ error: "Failed to retrieve users" });
}
});
// Define API endpoint for retrieving a specific user's phone number by email
/**
* Retrieve a specific user's phone number by email.
* @name GET/api/users/:email/phone
* @function
* @memberof module:Server
* @inner
* @param {string} email - The email of the user.
* @returns {Object} Response object containing user's phone number.
*/
app.get("/api/users/:email/phone", async (req, res) => {
const { email } = req.params;
try {
const user = await User.findOne({ email });
if (user) {
const { contact } = user;
res.json({ phone: contact });
} else {
res.status(404).json({ error: "User not found" });
}
} catch (error) {
console.error("Error retrieving user:", error);
res.status(500).json({ error: "Failed to retrieve user" });
}
});
// Define API endpoint for retrieving a specific user's personal details by email
/**
*
* Retrieve a specific user's personal details by email.
* @name GET/api/users/:email
* @function
* @memberof module:Server
* @inner
* @param {string} email - The email of the user.
* @returns {Object} Response object containing user's personal details.
*/
app.get("/api/users/:email", async (req, res) => {
const { email } = req.params;
try {
const user = await User.findOne({ email });
if (user) {
const { contact } = user;
const { username } = user;
const { profession } = user;
const { password } = user;
res.json({
contact: contact,
username: username,
profession: profession,
password: password,
});
} else {
res.status(404).json({ error: "User not found" });
}
} catch (error) {
console.error("Error retrieving user:", error);
res.status(500).json({ error: "Failed to retrieve user" });
}
});
// Define API endpoint for updating user details
app.put("/api/users/:email", async (req, res) => {
const { email } = req.params;
const { contact, profession } = req.body;
try {
const user = await User.findOneAndUpdate(
{ email },
{ $set: { contact, profession } },
{ new: true }
);
if (user) {
res.json({ message: "User details updated successfully" });
} else {
res.status(404).json({ error: "User not found" });
}
} catch (error) {
console.error("Error updating user details:", error);
res.status(500).json({ error: "Failed to update user details" });
}
});
// Define API endpoint for changing user password
app.put("/api/user/:email/password", async (req, res) => {
const { email } = req.params;
const { newPassword } = req.body;
try {
// Find user by email
const user = await User.findOne({ email });
if (!user) {
return res.status(404).json({ error: "User not found" });
}
// Hash the new password
const hashedPassword = await bcrypt.hash(newPassword, 10);
// Update user's password
user.password = hashedPassword;
await user.save();
res.json({ message: "Password changed successfully" });
} catch (error) {
console.error("Error changing password:", error);
res.status(500).json({ error: "Failed to change password" });
}
});
const transporter = nodemailer.createTransport({
service: "Gmail",
auth: {
user: "[email protected]",
pass: "your app password",
},
});
app.post("/api/send-email", async (req, res) => {
const { subject, feedback } = req.body;
const mailOptions = {
from: "[email protected]",
to: "[email protected]",
subject: `Feedback: ${subject}`,
text: feedback,
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error(error);
res.status(500).send("Feedback submission unsuccessful! Try again!");
} else {
console.log("Email sent: " + info.response);
res.status(200).send("Thanks for sharing your feedback with us..");
}
});
});
/**
*
* Authenticate user login.
* @name POST/api/login
* @function
* @memberof module:Server
* @inner
* @param {string} req.body.email - User's email address.
* @param {string} req.body.password - User's password.
* @returns {Object} Response object containing authentication token and username upon successful login.
*/
app.post("/api/login", async (req, res) => {
try {
const { email, password } = req.body;
// Find user by email
const user = await User.findOne({ email });
if (!user) {
return res.status(400).json({ error: "Invalid credentials" });
}
// Compare passwords
const passwordMatch = await bcrypt.compare(password, user.password);
if (!passwordMatch) {
return res.status(400).json({ error: "Invalid credentials" });
}
// Generate JWT token
const token = jwt.sign({ userId: user._id }, "iServeSL@UniOfBeds");
// Send response with token
res.json({ token });
} catch (error) {
console.error("Error logging in:", error);
res.status(500).json({ error: "Failed to login" });
}
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});