-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
209 lines (180 loc) · 6.09 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
// require node modules
var dotenv = require('dotenv').load(),
express = require('express'),
path = require('path'),
favicon = require('serve-favicon'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
session = require('express-session'),
MongoStore = require('connect-mongo')(session),
flash = require('connect-flash'),
bodyParser = require('body-parser'),
passport = require('passport'),
ejsLayouts = require('express-ejs-layouts');
// set up connection to database
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/yourguide_development');
// mongoose.connect('mongodb://undiscovered:undiscovered@ds047652.mongolab.com:47652/heroku_r8psfsns');
// require controllers for setting routes
var main = require('./controllers/index'),
users = require('./controllers/users'),
tours = require('./controllers/tours'),
User = require('./models/user').User,
app = express();
// setting up ejsLayouts
app.use(ejsLayouts);
// view engine setup
app.engine('html', require('ejs').renderFile);
app.set('views', path.join(__dirname, 'views/'));
app.set("view engine","ejs");
app.use(express.static(__dirname + '../public'));
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// enable sessions
app.use(cookieParser('keyboard cat'));
app.use(session({
secret: 'yourguide',
saveUninitialized: true,
resave: false,
store: new MongoStore({ mongooseConnection: mongoose.connection }),
cookie: {
secure: false,
maxAge: 864000000 // 10 Days in miliseconds
}}));
app.use(flash());
// passport initialization
app.use(passport.initialize());
app.use(passport.session());
app.use(function(req,res,next) {
res.locals.isAuthenticated = req.isAuthenticated();
res.locals.user = req.user;
next();
})
// set up heroku port
app.set('port', (process.env.PORT || 5000));
app.listen(app.get('port'), function() {
// console.log("Node app is running on port:" + app.get('port'))
})
// set main routes for index controller
app.get('/', main.index);
app.post('/', main.signin);
app.get('/signout', main.signout);
// set routes for Google Oauth
app.get('/auth/google', passport.authenticate('google', { scope: 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/youtube' }));
app.get('/auth/google/callback',
function(req, res) {
req.session.user.googleLogin = true;
req.session.save(function(err){
res.redirect('/tours/new_tour');
})
});
// set routes for user controller
app.post('/signup', users.add);
app.get('/dashboard', users.showDash);
app.get('/show_dashboard', users.renderDash);
app.get('/users/show/:id', users.renderUser);
app.get('/users/show', users.showUsers);
app.get('/users/:id', users.getUser);
app.put('/users/:id', users.update);
app.delete('/users/:id', users.destroy);
// set routes for tour controller
app.get('/tours/new', tours.newTour);
app.get('/tours/new_tour', tours.renderNewTour);
app.get('/tours/show', tours.showTour);
app.post('/tours/new', tours.add);
// set routes for tour ejs files
app.get('/tours/all', tours.allTours);
// app.get('/tours/', tours.findTours);
app.get('/tours/:id', tours.getTour);
app.post('/tours/favorite/:id', tours.wishlistTour);
app.put('/tours/:id', tours.update);
app.delete('/tours/:id', tours.destroy);
app.get('/tours/display/:id', tours.renderTour);
//set routes for browsing tours
app.get('/browse', tours.browse);
app.get('/browse/cities', tours.browseCities);
app.get('/browse/interests', tours.browseInterests);
app.post('/browse', tours.findTours);
app.post('/browse/tags', tours.findByTag);
app.get('/browse_tours', tours.renderBrowse);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// passport module configuration
var LocalStrategy = require('passport-local').Strategy,
GoogleStrategy = require('passport-google-oauth2').Strategy;
passport.use(new LocalStrategy(
// method to find user and validate password
function(username, password, done) {
User.findOne({ username: username }, function (err, user) {
if (err) { return done(err); }
if (!user) {
return done(null, false, { message: 'Incorrect username.' });
}
if (!user.validPassword(password)) {
return done(null, false, { message: 'Incorrect password.' });
}
return done(null, user);
});
}
));
// method to validate user with Google Oauth
if (process.env.OAUTH2_CLIENT_ID) { passport.use(new GoogleStrategy({
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: "http://localhost:3000/auth/google/callback",
passReqToCallback : true
},
function(request, accessToken, refreshToken, profile, done) {
process.nextTick(function() {
console.log("in google strategy");
console.log(profile);
// User.findOrCreate({ googleId: profile.id }, function (err, user) {
// return done(err, user);
// });
})
}
));
}
// method to save user session
passport.serializeUser(function(user, done) {
// var userInfo = { id: user.id, googleId: user.googleId };
// console.log(userInfo);
done(null, user.id);
});
// method to clear user session
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
// res.render('error', {
// message: err.message,
// error: {}
// });
});
module.exports = app;