forked from YashGupta29/CareForYou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
302 lines (267 loc) · 8.77 KB
/
app.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
291
292
293
294
295
296
297
298
299
300
301
302
require("dotenv").config();
const express = require("express");
const path = require("path");
const sendMail = require("./mail/mail");
require("./db/conn");
const User = require("./models/SignUpModel");
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const cookieParser = require("cookie-parser");
const port = process.env.PORT || 3000;
const app = express();
app.use(express.static(path.join(__dirname, "views")));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
// app.get("/users",(req,res)=>{
// const users = [];
// User.find({},function(err,data){
// if(err)
// console.log(err);
// else{
// // console.log(data[0]);
// users.push(data[0]);
// console.log(users);
// res.send(users);
// // res.send(data);
// // return res.json(data);
// }
// });
// return users;
// });
// app.post("/change-password",(req,res)=>{
// const {token} = req.body;
// const userDetails = jwt.verify(token,JWT_SECRET);
// console.log(userDetails);
// res.json({status : "ok"});
// });
app.post("/booking" ,async (req,res)=>{
const { date,time,reason,username } = req.body;
try {
const newBooking = {
date:date,
time:time,
reason:reason
}
const user = await User.findOne({username : username});
user.bookings = user.bookings.concat({booking : newBooking});
const token = await user.generateAuthToken();
await user.save();
// console.log(newBooking);
// console.log(user.bookings[0]);
const result = await sendMail(
user.name,
user.email,
"",
"",
"Booking Done Successfully",
`User ${user.name} Has Done a booking`,
`Bookings Details :
Date : ${date}
Time : ${time}`,
`Bookings Details :
Name : ${user.name}
Reason : ${reason}
Date : ${date}
Time : ${time}`);
console.log("Email sent successfully");
return res.json({ status: "ok", data: token });
} catch (error) {
console.log(error.message);
res.json({status:'error',error:'something went wrong'})
}
});
app.get("/dashboard", (req, res) => {
res.sendFile(path.join(__dirname, "views/dashboard/index.html"));
});
app.get("/signin", (req, res) => {
res.sendFile(path.join(__dirname, "views/signin.html"));
});
app.post("/signin", async (req, res) => {
const { username, pass } = req.body;
// const user = await User.findOne({ username }).lean();
try {
const user = await User.findOne({ username });
if (!user) {
return res.json({ status: "error", error: "Inavalid Login Credentials" });
}
if (await bcrypt.compare(pass, user.pass)) {
// const token = jwt.sign(
// {
// id: user._id,
// name: user.name,
// email: user.email,
// username: user.username,
// },
// JWT_SECRET
// );
// console.log(user);
const token = await user.generateAuthToken();
res.cookie("jwt",token);
// console.log(cookie);
return res.json({ status: "ok", data: token });
} else {
return res.json({ status: "error", error: "Inavalid Login Credentials" });
}
} catch (error) {
console.log(error.message);
res.json({ status: "error", error: "Inavalid Login Credentials" });
}
});
// try {
// const username = req.body.username;
// const pass = req.body.pass;
// console.log(`Username : ${username} Password : ${pass}`);
// const userCred = await User.findOne({username : username});
// const passMatched = await bcrypt.compare(pass,userCred.pass);
// if(passMatched){
// res.status(201).redirect("/");
// }
// else{
// console.log("alert not shown");
// res.redirect("/signin");
// }
// // res.redirect("/signin");
// } catch (error) {
// console.log(error);
// res.status(400);
// // prompt("Invalid Login Credentials");
// res.redirect("/signin");
// }
// });
app.get("/signup", (req, res) => {
res.sendFile(path.join(__dirname, "views/signup.html"));
});
app.post("/signup", async (req, res) => {
const { name, email, phone, username, pass: password, cpass } = req.body;
const pass = await bcrypt.hash(password, 10);
try {
const newUser = await User.create({
name,
email,
phone,
username,
pass,
cpass: pass,
});
const token = await newUser.generateAuthToken();
res.cookie("jwt",token);
console.log("User Created Successfullly");
return res.json({ status: "ok", data: token });
} catch (error) {
// console.log(error.message);
const duplicateKey = error.keyValue;
for (var key in duplicateKey) {
// console.log(key);
if (key === "username")
return res.json({ status: "error", error: "Username already taken" });
else if (key === "email")
return res.json({ status: "error", error: "Email already registered" });
else if (key === "phone")
return res.json({
status: "error",
error: "Phone Number already used",
});
}
}
});
// try {
// const pass = req.body.pass;
// const cpass = req.body.cpass;
// const hashedPass = await bcrypt.hash(pass,10);
// const email = req.body.email;
// const phone = req.body.number;
// const username = req.body.username;
// // const userEmail = User.findOne({email : email});
// // const userPhone = User.findOne({phone : phone});
// // const userUsername = User.findOne({username : username});
// // console.log(userEmail);
// // console.log(userPhone);
// // console.log(userUsername);
// // if(email === userEmail){
// // res.send("Email already used");
// // }
// // else if(phone === userPhone){
// // res.send("Phone already used");
// // }
// // else if(username === userUsername){
// // res.send("Username already used");
// // }
// // else{
// const addUser = new User({
// name : req.body.name,
// email : email,
// phone : phone,
// username : username,
// pass : hashedPass,
// cpass : hashedPass
// });
// const userAdded = await addUser.save();
// console.log(`Username : ${req.body.username} Password : ${pass}`);
// console.log(userAdded);
// res.status(201).redirect("/");
// // }
// } catch (error) {
// // console.log(error.keyValue);
// const duplicateKey = error.keyValue;
// for(var key in duplicateKey){
// console.log(key);
// if(key === "username")
// res.write('<script>alert("Usernaame already taken");</script>');
// else if(key === "email")
// res.write('<script>alert("Email already registered");</script>');
// else if(key === "phone")
// res.write('<script>alert("Phone Number already used");</script>');
// }
// res.status(400).redirect("/signup");
// }
app.post("/contact",async (req, res) => {
// console.log(req.body);
try {
const result = await sendMail(
req.body.name,
req.body.email,
req.body.subject,
req.body.message,
"Thank You",
"User Contacted",
"We received your details. We will contact you soon.",
`User Details:
Name: ${req.body.name}
Email: ${req.body.email}
Subject: ${req.body.subject}
Message: ${req.body.message}`);
// console.log(result);
res.redirect("/");
} catch (error) {
res.json({status:'error',error:error.message});
}
});
app.post("/contact2",async (req, res) => {
// console.log(req.body);
try {
const result = await sendMail(
req.body.name,
req.body.email,
req.body.subject,
req.body.message,
"Thank You",
"User Contacted",
"We received your details. We will contact you soon.",
`User Details:
Name: ${req.body.name}
Email: ${req.body.email}
Subject: ${req.body.subject}
Message: ${req.body.message}`);
// console.log(result);
res.redirect("index2.html");
} catch (error) {
res.json({status:'error',error:error.message});
}
});
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "views/index.html"));
});
app.listen(port, () => {
console.log(`Connection Successful at port ${port} `);
});