-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
174 lines (137 loc) · 6.19 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
var express = require("express") ,
app = express() ,
bodyParser = require("body-parser"),
mongoose = require("mongoose") ,
Campground = require("./models/campgrounds") , //it adds .js implicitly(in front of campgrounds) and also (var Campground) is not a mandatory name
//all mongodb methods like find() , create() , findById() etc will be called using (var Campground)
Comment = require("./models/comments") , //it adds .js implicitly(in front of comments) and also (var Comment)
// is not a mandatory name
//all mongodb methods like find() , create() , findById() etc will be called using (var Comment)
passport = require("passport"),
LocalStrategy = require("passport-local") ,
// FacebookStrategy = require('passport-facebook').Strategy,
TwitterStrategy = require('passport-twitter').Strategy,
GithubStrategy = require('passport-github2').Strategy,
GoogleStrategy = require('passport-google-oauth2').Strategy,
User = require("./models/user"),
methodOverride = require("method-override"),
flash = require("connect-flash"),
// auth = require('./auth') , // uncomment this in localhost
seedDb = require("./seeds") ;
mongoose.set('debug', true) ;
//requiring routes
var commentRoutes = require("./routes/comments.js") ,
campgroundRoutes = require("./routes/campgrounds.js"),
indexRoutes = require("./routes/index.js") ;
var url = process.env.DATABASEURL || "mongodb://localhost/yelp_camp" ;
mongoose.connect(url , {useMongoClient : true } ) ;
app.use(bodyParser.urlencoded( {extended : true} ) );
app.set("view engine" , "ejs");
//for using custom stylesheets
//seedDb() ; //seeding all the data
// local strategy
passport.use(new LocalStrategy(User.authenticate() )) ;
// facebook strategy
// passport.use(new FacebookStrategy({
// clientID: auth.facebook.clientID,
// clientSecret: auth.facebook.clientSecret,
// callbackURL: auth.facebook.callbackURL
// },
// function(accessToken, refreshToken, profile, cb) {
// User.findOrCreate({ facebookId: profile.id }, function (err, user) {
// return cb(err, user);
// });
// }
// ));
//twitter strategy
passport.use(new TwitterStrategy({
// uncomment it before running in local environment
//==================================================
// consumerKey: auth.twitter.consumerKey ,
// consumerSecret: auth.twitter.consumerSecret ,
// comment it before running in local environment
//=================================================
consumerKey: process.env.twitter_key ,
consumerSecret: process.env.twitter_secret ,
//=================================================
callbackURL: "https://post-your-yelpcamps.herokuapp.com/auth/twitter/callback" ,
passReqToCallback: true
},
function(accessToken, refreshToken, profile, done) {
User.findOrCreate({ twitterId: profile.id }, function (err, user) {
return cb(err, user);
});
// process.nextTick(function () {
// return done(null, profile);
// });
}
));
// github strategy
passport.use(new GithubStrategy({
// uncomment it before running in local environment
//==================================================
// clientID: auth.github.clientID ,
// clientSecret: auth.github.clientSecret ,
//=================================================
// comment it before running in local environment
//=================================================
clientID: process.env.github_key ,
clientSecret: process.env.github_secret ,
//================================================
callbackURL: "https://post-your-yelpcamps.herokuapp.com/auth/github/callback"
},
function(accessToken, refreshToken, profile, done) {
process.nextTick(function () {
return done(null, profile);
});
}
));
// google strategy
passport.use(new GoogleStrategy({
// uncomment it before running in local environment
//==================================================
// clientID: auth.google.clientID ,
// clientSecret: auth.google.clientSecret ,
//==================================================
// comment it before running in local environment
//=================================================
clientID: process.env.google_key ,
clientSecret: process.env.google_secret ,
//=================================================
callbackURL: 'https://post-your-yelpcamps.herokuapp.com/auth/google/callback',
passReqToCallback: true
},
function(request, accessToken, refreshToken, profile, done) {
process.nextTick(function () {
return done(null, profile);
});
}
));
//passport configuration
app.use(require("express-session")({
secret : "whole bunch of text means nothing",
resave : false ,
saveUninitialized : false
}));
app.use(express.static(__dirname + "/public")) ;
app.use(flash()) ;
passport.serializeUser(User.serializeUser() ) ;
passport.deserializeUser(User.deserializeUser() );
app.use(passport.initialize());
app.use(passport.session()) ;
//midleware - this will be called after every route confirming that currentUser is passed to every template
app.use(function(req , res , next){
res.locals.currentUser = req.user ;
res.locals.error = req.flash("error") ;
res.locals.success = req.flash("success") ;
next() ;
});
//requiring routes
app.use("/campgrounds",campgroundRoutes); // it appends "/campgrounds in front of every campground routes in campgrounds.js"
app.use("/campgrounds/:id/comments",commentRoutes) ; // it appends /campgrounds/:id/comments
app.use("/",indexRoutes) ;
app.use(methodOverride("_method") ) ;
//setting up the IP and PORT - mandatory for server to start listening
app.listen(process.env.PORT || 5000 ,process.env.IP,function(){
console.log("server started...");
});