-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
188 lines (178 loc) · 4.75 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
var express= require ("express"),
methodOverride = require("method-override"),
expressSanitizer = require("express-sanitizer"),
app = express(),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
passport = require('passport'),
LocalStrategy = require('passport-local')
passportLocalMongoose = require('passport-local-mongoose'),
User = require('./models/user'),
Blog = require('./models/blog')
require('dotenv').config()
//console.log(process.env.DBURL)
//App config
mongoose.connect(process.env.DBURL,{ useNewUrlParser: true,useUnifiedTopology: true })
app.set("view engine","ejs")
app.use(express.static("public"))
app.use(bodyParser.urlencoded({extended:true}))
app.use(expressSanitizer())
app.use(methodOverride("_method"))
//PASSPORT CONFIG
app.use(require('express-session')({
secret:"Once Again rusty wins cutest dog",
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.use((req,res,next)=>{
res.locals.currentUser = req.user
next()
})
//RESTFUL ROUTES
app.get("/",(req,res)=>{
res.redirect("/blogs")
})
//INDEX PAGE
app.get("/blogs",(req,res)=>{
Blog.find({},(err,blogs)=>{
if(err)
console.log("ERROR")
else{
res.render("index",{blogs:blogs})
}
})
})
//new - shows the form
app.get("/blogs/new",isLoggedIn,(req,res)=>{
res.render("new")
})
//create route
app.post("/blogs",isLoggedIn,(req,res)=>{
//create blog
req.body.blog.body = req.sanitize(req.body.blog.body)
var author={
id: req.user._id,
username: req.user.username
}
var newBlog = {title: req.body.blog.title,image: req.body.blog.image,body: req.body.blog.body,author:author}
//console.log(req.body.blog.body)
console.log(newBlog)
Blog.create(newBlog,(err,blog)=>{
if(err)
res.render("new")
else
res.redirect("/blogs")
})
})
//SHOW ROUTE
app.get("/blogs/:id",(req,res)=>{
Blog.findById(req.params.id,(err,foundBlog)=>{
if(err)
res.redirect("/blogs")
else{
res.render("show",{blog:foundBlog})
}
})
})
//EDIT ROUTE
app.get("/blogs/:id/edit",checkBlogOwnership,(req,res)=>{
Blog.findById(req.params.id,(err,foundBlog)=>{
if(err)
res.redirect("/blogs")
else{
res.render("edit",{blog:foundBlog})
}
})
})
//UPDATE ROUTE
app.put("/blogs/:id",checkBlogOwnership,(req,res)=>{
req.body.blog.body = req.sanitize(req.body.blog.body)
Blog.findByIdAndUpdate(req.params.id,req.body.blog,(err,updatedBlog)=>{
if(err)
res.redirect("/blogs")
else{
res.redirect("/blogs/"+req.params.id)
}
})
})
//DELETE ROUTE
app.delete("/blogs/:id",checkBlogOwnership,(req,res)=>{
Blog.findByIdAndRemove(req.params.id,(err)=>{
if(err)
res.redirect("/blogs")
else{
res.redirect("/blogs")
}
})
})
function checkBlogOwnership(req,res,next){
if(req.isAuthenticated()){
Blog.findById(req.params.id,(err,foundBlog)=>{
if(err){
res.redirect("back")
}
else{
//foundCampground.author.id is obj
//req.user._id is string so we can't use === to compare
if(foundBlog.author.id.equals(req.user._id))
{
next();
}else{
res.redirect("back")
}
}
})
}else{
res.redirect("back")
}
}
//====================
//AUTH ROUTES
//====================
app.get("/register",(req,res)=>{
res.render("register")
})
//handle signup
app.post("/register",(req,res)=>{
var newUser = new User({username: req.body.username})
User.register(newUser,req.body.password,(err,user)=>{
if(err){
console.log(err)
return res.render('/register')
}
passport.authenticate("local")(req,res,()=>{
res.redirect('/blogs')
})
})
})
//show login form
app.get("/login",(req,res)=>{
res.render("login")
})
//handling login logic
app.post("/login",passport.authenticate("local",{
successRedirect:"/blogs",
failureRedirect:"/login"
}),(req,res)=>{
})
app.get("/logout",(req,res)=>{
req.logOut()
res.redirect("/blogs")
})
function isLoggedIn(req,res,next){
if(req.isAuthenticated()){
return next();
}
res.redirect("/login")
}
/* Blog.remove({},(err,c)=>{
console.log(c)
}) */
app.listen(process.env.PORT || 4001 ,()=>{
console.log("Connected Blog App")
})