-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
378 lines (338 loc) · 8.48 KB
/
server.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
* Created by alemjc on 2/22/16.
* Authors: Jimmy Mona, Dong Son, and Jean Carlos Henriquez
*/
var Sequelize = require("sequelize");
var express = require("express");
var expressHandlebars = require("express-handlebars");
var expresssession = require('express-session');
var passport = require("passport");
var LocalStrategy = require("passport-local");
var flash = require('connect-flash');
var bcrypt = require("bcryptjs");
var bodyparser = require("body-parser");
var cookieparser = require("cookie-parser");
var SequelizeStore = require('connect-session-sequelize')(expresssession.Store);
var PORT = process.env.PORT || 9001;
var app = express();
app.engine("handlebars", expressHandlebars({
defaultLayout: "main"
}));
app.set("view engine", "handlebars");
app.use("/static", express.static("public"));
app.use(cookieparser());
app.use(bodyparser.urlencoded({extended:false}));
app.use(flash());
passport.serializeUser(function(user, done){
done(null, user);
});
passport.deserializeUser(function(user, done){
done(null, user);
});
passport.use('local', new LocalStrategy({
usernameField:'userName',
passwordField:'password',
session:true,
passReqToCallback: true
},
function(req, userName, password, done){
Users
.findOne({where: {userName:userName}})
.then(function(user){
console.log('user', user);
if(user){
bcrypt.compare(password, user.dataValues.password, function(err, success){
console.log('success', success);
if(success){
done(null,{userName:userName});
}
else{
done(null,false, {message: "Invalid user name or password."});
}
});
}
else{
done(null,false, {message: "Invalid user name or password."});
}
})
.catch(function(err){
done(err);
});
}
));
if(process.env.NODE_ENV === 'production') {
// FOR HEROKU DEPLOY
}
else {
// FOR TESTING LOCALLY
require("dotenv").config({path:"./DBCreds.env"});
}
var sequelize = new Sequelize(process.env.CLEARDB_DATABASE_URL);
app.use(expresssession({secret:'process.env.SECRET', resave:true, saveUninitialized:true,
cookie : { secure : false, maxAge : (4 * 60 * 60 * 1000) }
//store: new SequelizeStore({
// db: sequelize
//})
}));
app.use(passport.initialize());
app.use(passport.session());
//DB Models
var Places = sequelize.define("place", {
name: {
type:Sequelize.STRING,
allowNull:false
},
address:{
type:Sequelize.STRING,
allowNull:false
},
pictures:{
type:Sequelize.STRING,
defaultValue: "http://www.clipartbest.com/cliparts/dc8/578/dc8578Kgi.jpeg"
},
hours:{
type:Sequelize.STRING
},
category:{
type:Sequelize.STRING,
allowNull:false
}
});
var Users = sequelize.define("user",{
firstName:{
type:Sequelize.STRING,
allowNull:false
},
lastName:{
type:Sequelize.STRING,
allowNull:false
},
userName:{
type:Sequelize.STRING,
allowNull:false,
validate:{
len:[5,20]
}
},
password: {
type: Sequelize.STRING,
allowNull: false,
validate: {
len: [5, 2000]
}
},
birthday: {
type: Sequelize.STRING
}
},
{
hooks:{
beforeCreate: function(input){
input.password = bcrypt.hashSync(input.password,10);
}
}
}
);
var Ratings = sequelize.define("rating",{
stars: {
type:Sequelize.INTEGER
},
comment:{
type:Sequelize.STRING
}
});
//End DB Models
//DB relations
Users.belongsToMany(Places,{through:Ratings});
Places.belongsToMany(Users,{through:Ratings});
//routes
app.get("/", function (req, res) {
Places.findAll().then(function(place) {
if(req.isAuthenticated()){
res.render('home', {
layout: "loggedin",
place: place,
userinfo: req.user
})
}else{
res.render('home', {
place: place
})
}
})
});
app.get("/food", function (req, res) {
Places.findAll({
where: {category: "food"}
}).then(function(place) {
if(req.isAuthenticated()){
res.render('food', {
layout: "loggedin",
place: place,
userinfo: req.user
})
}else{
res.render('food', {
place: place
})
}
})
});
app.get("/entertainment", function (req, res) {
Places.findAll({
where: {category: "entertainment"}
}).then(function(place) {
if(req.isAuthenticated()){
res.render('entertainment', {
layout: "loggedin",
place: place,
userinfo: req.user
})
}else{
res.render('entertainment', {
place: place
})
}
})
});
app.get("/shopping", function (req, res) {
Places.findAll({
where: {category: "shopping"}
}).then(function(place) {
if(req.isAuthenticated()){
res.render('shopping', {
layout:"loggedin",
place: place,
userinfo: req.user
})
}else{
res.render('shopping', {
place: place
})
}
})
});
app.get("/:category/:id", function (req, res){
var id = req.params.id;
Ratings.findAll({
where: {placeId: id}
}).then(function(ratings){
var ratings= ratings
Places.findAll({
where: {id: id},
include: [{
model: Users
}]
}).then(function(place){
if(req.isAuthenticated()){
res.render("placepage", {
layout:"loggedin",
place: place,
ratings: ratings,
userinfo: req.user,
msg: req.session.error
})
delete req.session.error;
}else{
res.render("placepage", {
place: place,
ratings: ratings,
msg: req.session.error
})
delete req.session.error;
}
})
})
});
app.get("/register", function(req, res) {
res.render("register", req.query);
});
app.get("/login", function(req, res) {
res.render("login", {msg:req.flash("message")});
});
app.get("/logout", function(req, res){
req.session.destroy();
req.logout();
res.redirect("/");
});
//end routes
//POSTS
app.post("/login", passport.authenticate('local',{
successRedirect:"/",
failureRedirect:"/login",
failureFlash:true
}
));
app.post("/register", function(req, res){
if(req.body.userName.length < 5 || req.body.password.length < 5){
res.redirect("/register?msg=User name password must be longer than 5 characters.");
return;
}
if(req.body.first_name.length === 0 || req.body.last_name.length === 0 || req.body.birthday.length === 0){
res.redirect("/register?msg=Please fill out all fields.");
return;
}
Users.findAll({
where: {userName: req.body.userName}
}).then(function(result){
if (result.length < 0) {
res.redirect("/register?msg=That user name is already taken, please choose a diffrent one.");
return;
}
});
Users
.create({userName:req.body.userName, firstName:req.body.first_name, lastName:req.body.last_name,
password: req.body.password, birthday:req.body.birthday})
.then(function(){
res.redirect("/login");
})
.catch(function(err){
console.log("error is: ");
console.log(err);
if(err){
console.log(err);
res.redirect("/register?msg=Please fill out all fields.");
}
});
});
app.post("/loggedin", function(req, res) {
Places
.create({name:req.body.business_name, address:req.body.business_address, category:req.body.category})
.then(function() {
res.redirect("/");
})
});
app.post("/ratings", function(req, res){
if (req.isAuthenticated()){
Users.findAll({
where: {userName: req.user.userName}
}).then(function (result){
if (req.body.stars === "0"){
req.session.error = "You must choose a star rating to review a business";
res.redirect("back");
return;
}else if (req.body.comment.length < 10){
req.session.error = "You're review is too short, please make it at least 10 characters";
res.redirect("back");
return;
}
Ratings
.create({stars: parseInt(req.body.stars), comment: req.body.comment, placeId: parseInt(req.body.placeId), userId:parseInt(result[0].dataValues.id)})
.then(function(){
res.redirect("back");
})
})
}else{
req.session.error = "You must be logged in to review a business";
res.redirect("back");
}
});
//End posts
sequelize.sync().then(function(){
app.listen(PORT, function() {
console.log("LISTENING ON %s", PORT);
});
}).catch(function(err){
console.log("could not sync to db because of following error");
console.log(err);
});