-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
362 lines (312 loc) · 8.68 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
require('dotenv').config()
const express = require("express");
const http = require("http");
var path = require("path");
const _ = require("lodash");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const findOrCreate = require("mongoose-findorcreate")
const mongoose = require("mongoose");
const session = require("express-session");
const passport = require("passport");
const passportLocalMongoose = require("passport-local-mongoose");
var expressLayouts = require('express-ejs-layouts');
const bodyParser = require("body-parser")
const app = express();
const ejs = require("ejs");
//view engine setup
app.set('view engine', 'ejs');
//static folder
app.use(express.static("public"));
app.use(bodyParser.urlencoded({
extended: true
}));
//routes
// app.use(Router);
//Session initialization
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
}))
//passport-plugins
app.use(passport.initialize());
app.use(passport.session());
//mongoose connection
mongoose.connect(process.env.Mongoose, {
useNewUrlParser: true,
useUnifiedTopology: true,
bufferCommands: false,
useFindAndModify: false
}, ()=>
console.log('db connected'),);
mongoose.set('useCreateIndex', true);
//Schema Define
const bookSchema = new mongoose.Schema({
booktitle: String,
genre: String,
bookauthor: String,
url: String,
language: String,
link: String,
bookContent: String
})
const userSchema = new mongoose.Schema({
email: String,
password: String,
googleId: String,
Mybook: [bookSchema]
});
userSchema.plugin(passportLocalMongoose);
const User = new mongoose.model("User", userSchema);
const Book = new mongoose.model("Book", bookSchema);
passport.use(User.createStrategy());
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
// <!-- Google Auth -->
passport.use(new GoogleStrategy({
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: "https://shielded-crag-73381.herokuapp.com/auth/google/home",
userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo"
},
function(accessToken, refreshToken, profile, cb) {
User.findOne( {googleId : profile.id}, function( err, foundUser ){
if( !err ){ //Check for any errors
if( foundUser ){ // Check for if we found any users
return cb( null, foundUser ); //Will return the foundUser
}else { //Create a new User
const newUser = new User({
googleId : profile.id
});
newUser.save( function( err ){
if(!err){
return cb(null, newUser); //return newUser
}
});
}
}else{
console.log( err );
}
});
}
));
//routes
function isLoggedIn(req,res,next){
if(req.isAuthenticated())return next();
res.redirect('/signin')
}
function isLoggedOut(req,res,next){
if(!req.isAuthenticated())return next();
res.redirect('/')
}
// <!-- Google login route -->
app.get('/auth/google',
passport.authenticate('google', { scope: ['profile'] }));
app.get('/auth/google/home',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect("/home");
});
app.get("/", function(req, res) {
res.render("register");
})
//Register Route
app.post("/register", function(req, res) {
console.log(req.body.username);
console.log(req.body.password);
User.register({
username: req.body.username
}, req.body.password, function(err, user) {
if (err) {
console.log(err);
res.redirect("/")
} else {
passport.authenticate("local")(req, res, function() {
res.redirect("/home")
})
}
})
});
//Sign in Route
app.get("/signin", function(req, res) {
res.render("signin");
})
app.post("/signin", function(req, res) {
//console.log(req.body.username);
//console.log(req.body.password);
const user = new User({
username: req.body.username,
password: req.body.password
});
req.login(user, function(err) {
if (err) {
console.log(err);
} else {
passport.authenticate("local")(req, res, function() {
res.redirect("/home");
})
}
});
});
app.get('/logout',(req,res)=>{
req.session.destroy();
res.redirect('/');
})
// <!-- Category Route -->
app.get("/home/category/:genre", function(req, res) {
if (req.isAuthenticated()) {
const list = _.capitalize([req.params.genre]);
Book.find({genre: list}, function(err, found) {
res.render("catogory", {newListItem: found, new: list})
})
}
else {
res.redirect("/signin")
}
})
//home route
app.get("/home",isLoggedIn, function(req, res) {
if (req.isAuthenticated()) {
Book.find({}, function(err, foundList) {
if(err) {
res.send(err)
}
else {
res.render("home", {newListItem: foundList})
}
})
} else {
res.redirect("/signin")
}
})
// <!-- addbook route -->
app.get("/addbook", function(req, res) {
if (req.isAuthenticated()) {
console.log(req.user.id);
console.log(req.user.email);
res.render("addbook")
}
else {
res.render("signin")
}
})
app.post("/addbook", function(req, res) {
if (req.isAuthenticated()) {
console.log(req.body.title);
console.log(req.body.author);
console.log(req.body.content);
const genre = _.capitalize(req.body.genre);
const title = req.body.title;
const author = req.body.author;
const content = req.body.content;
const image = req.body.image;
const link = req.body.link;
const lan = req.body.language;
console.log(genre);
const book1 = new Book ({
genre: genre,
booktitle: req.body.title,
bookauthor: req.body.author,
url: req.body.image,
language: req.body.language,
link: req.body.link,
bookContent: req.body.content
})
book1.save();
res.redirect("/home");
}
else {
res.redirect("/signin")
}
})
// <!-- View Book Route -->
app.get("/home/:id", function(req, res) {
if (req.isAuthenticated()) {
console.log(req.user.id);
const name = req.params.id;
Book.findOne({booktitle: req.params.id}, function(err, found) {
res.render("viewpage", {img: found.url, title: found.booktitle, author: found.bookauthor, genre: found.genre, content: found.bookContent, link: found.link})
})
}
else {
res.redirect("/signin")
}
})
// <!-- My Book Route -->
app.get("/home/mybooks/:title", function(req, res) {
if (req.isAuthenticated()) {
console.log(req.params.title);
console.log(req.user.id);
User.findOne({_id: req.user.id}, function(err, found) {
if (err) {
res.send(err);
}
else {
Book.findOne({booktitle: req.params.title}, function(err, foundList) {
if (err) {
res.send(err);
}
else {
const book1 = new Book({
booktitle: foundList.booktitle,
genre: foundList.genre,
bookauthor: foundList.bookauthor,
url: foundList.url,
language: foundList.language,
link: foundList.link,
bookContent: foundList.bookContent
})
found.Mybook.push(book1);
found.save();
res.render("mybook", {newListItem: found.Mybook});
}
})
}
})
}
else {
res.send("/signin")
}
})
app.get("/mybook", function(req, res) {
if (req.isAuthenticated()) {
User.findOne({_id: req.user.id}, function(err, found) {
res.render("mybook", {newListItem: found.Mybook})
})
}
else {
res.redirect("/signin")
}
})
// <!-- Search Route -->
app.post("/home", function(req, res) {
if (req.isAuthenticated()) {
const search = _.startCase(_.toLower(req.body.search));
console.log(search);
Book.find({booktitle: search}, function(err, foundList) {
if (err) {
res.send(err);
}
else {
console.log(foundList);
res.render("searchbook", {newListItem: foundList})
}
})
}
else {
res.send("/signin");
}
})
//listening port setup
let port = process.env.PORT;
if (port == null || port == "") {
port = 3000;
}
app.listen(port);