-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
248 lines (228 loc) Β· 6.53 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
const express = require("express");
const app = express();
const path = require("path");
const userModel = require("./models/user.model");
const postModel = require("./models/post.model");
const cookieParser = require("cookie-parser");
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
require("dotenv").config();
// setting view engine
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
// parsing
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
const port = process.env.PORT || 3000;
// routes are completed β
app.get("/", isLogin, async (req, res) => {
const userid = req.user.userid;
const user = await userModel.findOne({ _id: userid });
const isOwner = req.user.userid === userid;
console.log(await user.populate("post"));
res.render("profile", { user, isOwner });
});
app.get("/register", async (req, res) => {
res.render("register");
});
app.post("/register", async (req, res) => {
const { name, username, email, password, age, phone, img } = req.body;
const user = await userModel.findOne({ email: email });
if (user) {
res.send("user is already exist..");
}
bcrypt.genSalt(10, (err, salt) => {
if (err) {
res.send(err);
}
bcrypt.hash(password, salt, async (err, hash) => {
if (err) {
res.send(err);
}
const newUser = await userModel.create({
name,
username,
email,
password: hash,
age,
phone,
img,
});
const token = jwt.sign({ email: email, userid: newUser._id }, "shhhh");
res.cookie("token", token);
res.redirect("/");
});
});
});
app.get("/login", (req, res) => {
res.render("login");
});
app.post("/login", async (req, res) => {
// res.cookie("token", "");
const { email, password } = req.body;
if (!email || !password) {
console.log("both field are required");
res.redirect("/login");
}
const user = await userModel.findOne({ email: email });
if (!user) {
console.log(
"please fill all field with correct data or register if new π"
);
res.redirect("/register");
} else {
bcrypt.compare(password, user.password, function (err, result) {
if (err) {
console.log(err);
}
if (result === true) {
jwt.sign({ email: email, userid: user._id }, "shhhh", (err, token) => {
if (err) {
console.log(err, "error in signing");
}
res.cookie("token", token);
res.redirect("/");
});
} else {
console.log("wrong password");
res.redirect("login");
}
});
}
});
app.get("/logout", (req, res) => {
// res.cookie("token", "");
res.clearCookie("token");
res.redirect("/");
});
app.get("/account/:id", isLogin, async (req, res) => {
const userid = req.params.id;
const guest = await userModel.findOne({ _id: userid });
const isOwner = req.user.userid === userid;
if (isOwner) {
res.redirect("/");
} else {
res.render("profile", { user: guest, isOwner });
}
});
app.get("/post/create", isLogin, async (req, res) => {
res.render("createpost");
});
app.post("/post/create", isLogin, async (req, res) => {
const { desc, img } = req.body;
// jwt.verify(req.cookies.token, "shhhh", async (err, userData) => {
// if (err) {
// res.send(err);
// }
const email = req.user.email;
const currentUser = await userModel.findOne({ email });
const post = await postModel.create({
desc,
img,
owner: currentUser._id,
});
currentUser.post.push(post._id);
await currentUser.save();
res.redirect("/posts");
});
// });
app.get("/posts", isLogin, async (req, res) => {
const posts = await postModel.find().populate("owner");
if (posts.length == 0) {
res.redirect("/post/nothing");
}
res.render("postShow", { posts });
});
app.get("/post/nothing", isLogin, (req, res) => {
res.render("emptyPosts");
});
app.get("/profile/edit", isLogin, async (req, res) => {
const user = await userModel.findOne({_id:req.user.userid})
res.render("editProfile",{user});
});
app.post("/profile/edit", isLogin, async (req, res) => {
jwt.verify(req.cookies.token, "shhhh", async (err, userData) => {
if (err) {
res.send(`<h2>${err}</h2>`);
}
let { name, phone, img } = req.body;
const user = await userModel.findOne({ email: userData.email });
name = name ? name : user.name;
phone = phone ? phone : user.phone;
img = img ? img : user.img;
const updatedUser = await userModel.findOneAndUpdate(
{ _id: userData.userid },
{
name,
phone,
img,
}
);
res.redirect("/");
});
});
app.get("/post/like/:postId", isLogin, async (req, res) => {
const posts = await postModel.find();
const post = await postModel.findOne({ _id: req.params.postId });
const user = req.user;
if (post.likes.indexOf(user.userid) === -1) {
post.likes.push(user.userid);
} else {
post.likes.splice(post.likes.indexOf(user.userid), 1);
}
await post.save();
res.redirect("/posts");
});
app.get("/post/delete/:postId", isLogin, async (req, res) => {
const postId = req.params.postId;
const post = await postModel.findOne({ _id: postId });
if (req.user.userid == post.owner) {
const owner = await userModel.findOne({ _id: req.user.userid });
owner.post.splice(owner.post.indexOf(postId), 1);
await owner.save();
await postModel.findOneAndDelete({ _id: postId });
}
res.redirect("/posts");
});
app.get("/post/edit/:postId", isLogin, async (req, res) => {
const postId = req.params.postId;
const post = await postModel.findOne({ _id: postId });
const userId = req.user.userid;
if(userId == post.owner){
res.render("editPost", { post })}
else{ res.redirect("/posts");}
});
app.post("/post/edit/:postId", isLogin, async (req, res) => {
const postId = req.params.postId;
const post = await postModel.findOne({ _id: postId });
const userId = req.user.userid;
let { desc, img } = req.body;
desc = desc ? desc : post.desc;
img = img ? img : post.img;
await postModel.findOneAndUpdate(
{ _id: postId },
{
desc,
img,
}
);
res.redirect("/posts");
});
function isLogin(req, res, next) {
if (!req.cookies.token) {
res.redirect("/login");
}
let token = req.cookies.token;
jwt.verify(token, "shhhh", (err, userData) => {
if (err) {
res.send(err);
}
req.user = userData;
next();
});
}
app.listen(port, () => {
console.log(`application running on ${port}`);
});