-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
209 lines (164 loc) · 5.59 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('dotenv').config();
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const express = require('express');
const favicon = require('serve-favicon');
const hbs = require('hbs');
const mongoose = require('mongoose');
const logger = require('morgan');
const path = require('path');
const session = require("express-session");
const MongoStore = require('connect-mongo')(session);
const flash = require("connect-flash");
const passport = require("passport");
const User = require("./models/User");
const GreenSpace = require("./models/GreenSpace");
mongoose
.connect(process.env.MONGODB_URI || 'mongodb://localhost/greenspace', {
useNewUrlParser: true
})
.then(x => {
console.log(`Connected to Mongo! Database name: "${x.connections[0].name}"`)
})
.catch(err => {
console.error('Error connecting to mongo', err)
});
const app_name = require('./package.json').name;
const debug = require('debug')(`${app_name}:${path.basename(__filename).split('.')[0]}`);
const app = module.exports = express();
// Middleware Setup
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
// we serialize only the "_id" field of the user to keep the information stored minimum
// passport.serializeUser((user, done) => {
// done(null, user._id);
// });
// //when we need the information for the user, the deserializeUser function is
// //called with the id that we previously serialized to fetch the user from the database
// passport.deserializeUser((id, done) => {
// User.findById(id)
// .then(dbUser => {
// done(null, dbUser);
// })
// .catch(err => {
// done(err);
// });
// });
// Express View engine setup
app.use(require('node-sass-middleware')({
src: path.join(__dirname, 'public'),
dest: path.join(__dirname, 'public'),
sourceMap: true
}));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.use(express.static(path.join(__dirname, 'public')));
app.use(favicon(path.join(__dirname, 'public', 'images', 'favicon.ico')));
hbs.registerHelper('ifUndefined', (value, options) => {
if (arguments.length < 2)
throw new Error("Handlebars Helper ifUndefined needs 1 parameter");
if (typeof value !== undefined) {
return options.inverse(this);
} else {
return options.fn(this);
}
});
// default value for title local
app.locals.title = 'GreenSpace';
//######################################### INSTAGRAM STRATEGY ########################################
const InstagramStrategy = require("passport-instagram").Strategy;
passport.use(
new InstagramStrategy({
clientID: process.env.INSTAGRAM_CLIENTID,
clientSecret: process.env.INSTAGRAM_CLIENTSECRET,
callbackURL: "https://greenspaceberlin.herokuapp.com/auth/instagram/callback"
},
(accessToken, refreshToken, profile, done) => {
//find a user with a profile.id as instagramID or create one
//console.log(profile);
User.findOne({
instagramId: profile.id
}).then(found => {
//USER IS FOUND, meaning user with that Instagram id already exists. Then user is logged in
if (found !== null) {
done(null, found)
//User doesn't yet exist
} else {
return User.create({
instagramId: profile.id
}).then(dbUser => {
done(null, dbUser);
});
}
})
.catch(err => {
done(err);
});
}
)
);
//######################################### TWITTER STRATEGY ########################################
const TwitterStrategy = require("passport-twitter").Strategy;
passport.use(
new TwitterStrategy({
consumerKey: process.env.TWITTER_CLIENTID,
consumerSecret: process.env.TWITTER_CLIENTSECRET,
callbackURL: "https://greenspaceberlin.herokuapp.com/auth/twitter/callback"
},
(accessToken, refreshToken, profile, done) => {
//find a user with a profile.id as TwitterID or create one
//console.log(profile);
User.findOne({
twitterId: profile.id
}).then(found => {
//USER IS FOUND, meaning user with that Instagram id already exists. Then user is logged in
if (found !== null) {
done(null, found)
//User doesn't yet exist
} else {
return User.create({
twitterId: profile.id
}).then(dbUser => {
done(null, dbUser);
});
}
})
.catch(err => {
done(err);
});
}
)
);
//######################################### GENERAL FOR PASSPORT ########################################
// Enable authentication using session + passport
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
store: new MongoStore({
mongooseConnection: mongoose.connection
})
}))
app.use(flash());
require('./passport')(app);
const googleMapsClient = require('@google/maps').createClient({
key: process.env.GOOGLE_API_KEY
});
//######################################### ROUTES ########################################
const index = require('./routes/index');
app.use('/', index);
const authRoutes = require('./routes/auth');
app.use('/auth', authRoutes);
const search = require('./routes/search');
app.use('/search', search)
const user = require('./routes/userprofile');
app.use('/user', user)
const greenspace = require('./routes/greenspace');
app.use('/greenspace', greenspace)
const image = require('./routes/image');
app.use('/image', image)
module.exports = app;