-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
273 lines (273 loc) · 8.03 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
var flash = require("connect-flash");
var express = require("express");
var methodOverride = require("method-override");
var app = express();
var bodyParser = require("body-parser");
var passport = require("passport");
var localStrategy = require("passport-local");
var passportLocalMongoose = require("passport-local-mongoose");
var mongoose = require("mongoose");
var Post = require("./models/posts");
var User = require("./models/user");
var Comment = require("./models/comment");
mongoose.connect("mongodb://localhost/posts");
mongoose.set('useFindAndModify', false);
app.use(bodyParser.urlencoded({extended: true}));
app.use(methodOverride("_method"));
app.use(flash());
// Post.create({
// name: "Welcome Everybody in the website.",
// author:"Owner of the website.",
// link:"https://wdbapp3.run-ap-south1.goorm.io/posts"
// },function(err, post){
// if(err){
// console.log(err);
// }else{
// console.log("NEWLY CREATED POST: ");
// console.log(post);
// }
// });
app.use(express.static("public"));
//PASSPORT CONFIGRATION
app.use(require("express-session")({
secret: "New User Model In Post App",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new localStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.get("/", function(req, res){
res.render("home.ejs", {currentUser: req.user});
});
app.get("/posts/new", isLoggedIn, function(req, res){
res.render("newpost.ejs", {currentUser: req.user})
})
app.get("/posts", function(req, res){
Post.find({}, function(err, posts){
if(err){
console.log(err);
}
else{
res.render("posts.ejs", {posts: posts, currentUser: req.user, message: req.flash("success")});
}
});
});
app.post("/addpost", isLoggedIn, function(req, res){
var name = req.body.postname;
var author = req.body.postauthor;
var link = req.body.postlink;
var desc = req.body.description;
var newpost = {name: name, author:author, link:link, description: desc};
Post.create(newpost, function(err, newlyCreated){
if(err){
console.log(err);
}else{
newlyCreated.author.id = req.user.id;
newlyCreated.author.username = req.user.username;
newlyCreated.save();
res.redirect("/posts");
}
});
});
app.get("/posts/:id", function(req,res){
Post.findById(req.params.id).populate("comments").exec(function(err, foundPost){
if(err){
console.log(err);
}else{
res.render("show.ejs", {posts:foundPost, currentUser: req.user, message: req.flash("error1"), message1: req.flash("error2")});
}
});
});
app.get("/posts/:id/edit", checkCampgroundOwnership, function(req, res){
Post.findById(req.params.id, function(err, updatePost){
res.render("edit.ejs", {posts: updatePost, currentUser: req.user});
});
});
app.put("/posts/:id",checkCampgroundOwnership, function(req, res){
var data = {name: req.body.postname, link: req.body.postlink, description: req.body.description};
Post.findByIdAndUpdate(req.params.id, data, function(err, editPost){
if(err){
res.redirect("/posts");
}else{
res.redirect("/posts/"+ editPost._id);
}
});
});
app.delete("/posts/:id",checkCampgroundOwnership, function(req, res){
Post.findByIdAndRemove(req.params.id, function(err){
if(err){
res.redirect("/posts");
}else{
res.redirect("/posts");
}
});
});
// AUTH ROUTES
// show register form
app.get("/register", function(req, res){
res.render("register.ejs",{currentUser: req.user, message2: req.flash("errorinsignup")});
});
//handle sign up logic
app.post("/register", function(req, res){
var newUser = new User({username: req.body.username});
User.register(newUser, req.body.password, function(err, user){
if(err){
req.flash("errorinsignup", err.message)
res.redirect("/register");
}
passport.authenticate("local")(req, res, function(){
req.flash("success", "Nice To Meet You " + req.user.username + ", You Have Successfully Signed up!");
res.redirect("/posts")
});
});
});
// show login form
app.get("/login", function(req, res){
res.render("login.ejs", {currentUser: req.user, message: req.flash("error")})
});
//handling login logic
app.post("/login", passport.authenticate("local",
{
successRedirect: "/posts",
failureRedirect: "/login"
}), function(req, res){
});
//Logout logic
app.get("/logout", function(req, res){
req.logout();
res.redirect("/posts");
});
function isLoggedIn(req, res, next){
if(req.isAuthenticated()){
return next();
}
req.flash("error", "You Need To Be Logged In First!!");
res.redirect("/login");
}
function checkCampgroundOwnership(req, res, next){
//is user logged In?
if(req.isAuthenticated()){
Post.findById(req.params.id, function(err, updatePost){
if(err){
res.redirect("back");
}
else{
//does the user own the post?
if(updatePost.author.id.equals(req.user._id)){
next();
}
//otherwise, redirect
else{
req.flash("error2", "You Do Not Have Permission to do That!!");
res.redirect("/posts/" + req.params.id);
}
}
});
//if not, redirect
}else{
req.flash("error1", "You Need To Be Logged In First!!");
res.redirect("/posts/" + req.params.id)
}
};
function checkCommentOwnership(req, res, next){
//is user logged In?
if(req.isAuthenticated()){
Comment.findById(req.params.comment_id, function(err, foundComment){
if(err){
res.redirect("back");
}
else{
//does the user own the comment?
if(foundComment.author.id.equals(req.user._id)){
next();
}
//otherwise, redirect
else{
req.flash("error2", "You Do Not Have Permission to do That!!");
res.redirect("/posts/" + req.params.id);
}
}
});
//if not, redirect
}else{
req.flash("error1", "You Need To Be Logged In First!!");
res.redirect("/posts/" + req.params.id)
}
};
//===========================================================================================================================\\
// Comment Routes
app.get("/posts/:id/comments/new", isLoggedIn, function(req, res){
Post.findById(req.params.id, function(err, post){
if(err){
console.log(err);
}else{
res.render("commentnew.ejs", {posts: post,currentUser: req.user});
}
});
});
app.post("/posts/:id/comments",isLoggedIn, function(req, res){
//lookup posts using ID
Post.findById(req.params.id, function(err, post){
if(err){
console.log(err);
res.redirect("/posts")
}else{
Comment.create(req.body.comment, function(err, comment){
if(err){
console.log(err);
res.redirect("/posts");
}else{
post.comments.push(comment);
post.save();
comment.author.id = req.user.id;
comment.author.username = req.user.username;
comment.save();
res.redirect("/posts/" + post._id);
}
});
}
});
});
app.get("/posts/:id/comments/:comment_id/edit", checkCommentOwnership, function(req, res){
Comment.findById(req.params.comment_id, function(err, commentedit){
if(err){
console.log(err);
res.redirect("/posts")
}else{
var id = req.params.id;
res.render("commentedit.ejs", {comment: commentedit, currentUser: req.user, id: id});
}
});
});
//=================
app.put("/posts/:id/comments/:comment_id", checkCommentOwnership, function(req, res){
Comment.findByIdAndUpdate(req.params.comment_id, req.body.comment, function(err, commentupdate){
if(err){
console.log(err)
res.redirect("/posts");
}else{
res.redirect("/posts/" + req.params.id);
}
});
});
//================
app.delete("/posts/:id/comments/:comment_id", checkCommentOwnership, function(req, res){
Comment.findByIdAndRemove(req.params.comment_id, function(err, commentdelete){
if(err){
console.log(err)
res.redirect("/posts");
}else{
res.redirect("/posts/" + req.params.id);
}
});
});
//===========================================================================================================================\\
app.get("*", function(req, res){
res.render("notfound.ejs");
});
app.listen(process.env.PORT || 4003, process.env.IP, function(){
console.log("Server has STARTED!!");
});