-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
241 lines (207 loc) · 5.74 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
require("dotenv").config();
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const connectBD = require("./models/config");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const userModal = require("./models/userModel");
const addUserModel = require("./models/addUserModel");
const bcrypt = require("bcryptjs");
const auth = require("./middleware/auth");
const path = require("path");
const {
india,
england,
nepal,
russia,
sri_lanka,
bangladesh,
afghanistan,
pakistan,
} = require("./models/regiatrationModel");
const port = process.env.PORT || 1000;
connectBD();
const staticPath = path.join(__dirname, "./public");
app.use(cookieParser());
app.use(express.static(staticPath));
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(
session({
secret: process.env.SECRET_KEY,
resave: false,
saveUninitialized: false,
})
);
app.use((req, res, next) => {
res.set("Cache-Control", "no-store, no-cache, must-revalidate, private");
next();
});
app.get("/", (req, res) => {
res.render("Login", { message: req.session.logMsg });
});
app.get("/signup", (req, res) => {
res.render("SignUp", { message: req.session.sigMessage });
});
app.get("/registration", auth, async (req, res) => {
try {
const indiaStateList = await india.find();
const russiaStateList = await russia.find();
const nepalStateList = await nepal.find();
const sri_lankaStateList = await sri_lanka.find();
const englandStateList = await england.find();
const bangladeshStateList = await bangladesh.find();
const afghanistanStateList = await afghanistan.find();
const pakistanStateList = await pakistan.find();
// console.log(nepalStateList);
const allCountryState = [
...indiaStateList,
...afghanistanStateList,
...bangladeshStateList,
...russiaStateList,
...nepalStateList,
...sri_lankaStateList,
...pakistanStateList,
...englandStateList,
];
if (req.user) {
res.render("registration_form", {
countryData: allCountryState,
email: req.session.currentUser,
});
} else {
res.redirect("/");
}
} catch (err) {
console.log(err);
}
});
app.post("/signup", async (req, res) => {
const newUsername = req.body.username;
const newPassword = req.body.password;
const newUser = new userModal({
username: newUsername,
password: newPassword,
});
try {
const foundUser = await userModal.find({ username: newUsername });
if (foundUser.length === 0) {
const token = await newUser.generateAuthToken();
res.cookie("jwt", token, {
expires: new Date(Date.now() + 60000),
httpOnly: true,
}); // 60sec
await newUser
.save()
.then(() => {
// console.log("Successfully added........");
req.session.sigMessage = "Successfully Signup please login!";
res.redirect("/signup");
})
.catch((err) => {
console.log(err);
res.redirect("/signup");
});
} else {
req.session.sigMessage = "User Already Exist!";
res.redirect("/signup");
}
} catch (err) {
res.status(400).send(err);
}
});
app.post("/login", async (req, res) => {
const Username = req.body.username;
const password = req.body.password;
try {
const foundData = await userModal.findOne({ username: Username });
const userDetail = await addUserModel.findOne({ email: req.body.username });
if (foundData) {
const isMatch = await bcrypt.compare(password, foundData.password);
const token = await foundData.generateAuthToken();
res.cookie("jwt", token, { maxAge: 30 * 60 * 1000 });
if (isMatch) {
req.session.currentUser = foundData.username;
if (userDetail) {
res.redirect("/user");
} else {
res.redirect("/registration");
}
} else {
req.session.logMsg = "Incorrect Password!";
res.redirect("/");
}
} else {
req.session.logMsg = "user does not exists!";
res.redirect("/");
}
} catch (err) {
res.status(400).send(err);
}
});
app.get("/logout", auth, async (req, res) => {
try {
req.user.tokens = req.user.tokens.filter((currentELe) => {
return currentELe.token != req.token;
});
res.clearCookie("jwt");
await req.user.save();
res.redirect("/");
} catch (err) {
res.status(401).send(err);
}
});
app.get("/logoutAll", auth, async (req, res) => {
try {
req.user.tokens = [];
res.clearCookie("jwt");
await req.user.save();
res.redirect("/");
} catch (err) {
res.status(401).send(err);
}
});
app.post("/user", async (req, res) => {
const userDetails = new addUserModel({
first_name: req.body.fname,
last_name: req.body.lname,
email: req.body.email,
country: req.body.country,
state: req.body.state,
city: req.body.city,
date_of_birth: req.body.dob,
age: req.body.age,
gender: req.body.gender,
});
try {
await userDetails.save();
res.redirect("/user");
} catch (err) {
console.log(err);
res.status(400).send(err);
}
});
app.get("/user", auth, async (req, res) => {
const userDetail = await addUserModel.findOne({
email: req.session.currentUser,
});
if (req.user) {
res.render("UserDetails", { user: userDetail });
} else {
res.redirect("/");
}
});
app.post("/api/user/delete", async (req, res) => {
try {
const removedDocument = await addUserModel.findByIdAndRemove(
req.body.deluser
);
res.redirect("/");
} catch (err) {
res.status(500).send(err);
}
});
app.listen(port, () => {
console.log(`we are listening at port number ${port}`);
});