forked from azat-co/hackhall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
281 lines (247 loc) · 8.52 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
var express = require('express'),
routes = require('./routes'),
http = require('http'),
util = require('util'),
path = require('path'),
oauth = require('oauth'),
fs = require('fs'),
https = require('https'),
querystring = require('querystring');
var favicon = require('serve-favicon'),
logger = require('morgan'),
bodyParser = require('body-parser'),
methodOverride = require('method-override'),
cookieParser = require('cookie-parser'),
session = require('express-session'),
csrf = require('csurf');
// errorHandler = require('errorhandler');
var hs = require(path.join(__dirname, 'lib', 'hackhall-sendgrid'));
var c = require(path.join(__dirname, 'lib', 'colors'));
require(path.join(__dirname, 'lib', 'env-vars'));
var GitHubStrategy = require('passport-github').Strategy,
passport = require('passport');
var app = express();
app.set('port', process.env.PORT || 3000 );
app.use(favicon(path.join(__dirname,'public','favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(methodOverride());
app.use(cookieParser(process.env.COOKIE_SECRET));
app.use(session({
secret: process.env.SESSION_SECRET,
key: 'sid',
cookie: {
secret: true,
expires: false
},
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
// app.use(csrf());
// app.use(function(req, res, next) {
// res.locals.csrf = req.csrfToken();
// return next();
// });
app.use(express.static(__dirname + '/public'));
function logErrors(err, req, res, next) {
if (typeof err === 'string')
err = new Error (err);
console.error('logErrors', err.toString());
next(err);
}
function clientErrorHandler(err, req, res, next) {
if (req.xhr) {
console.error('clientErrors response');
res.status(500).json({ error: err.toString()});
} else {
next(err);
}
}
function errorHandler(err, req, res, next) {
console.error('lastErrors response');
res.status(500).send(err.toString());
}
var dbUrl = process.env.MONGOHQ_URL || 'mongodb://@127.0.0.1:27017/hackhall';
var mongoose = require('mongoose');
var connection = mongoose.createConnection(dbUrl);
connection.on('error', console.error.bind(console, 'connection error:'));
connection.once('open', function () {
console.info('Connected to database')
});
var models = require('./models');
function db (req, res, next) {
req.db = {
User: connection.model('User', models.User, 'users'),
Post: connection.model('Post', models.Post, 'posts')
};
return next();
}
var checkUser = routes.main.checkUser;
var checkAdmin = routes.main.checkAdmin;
var checkApplicant = routes.main.checkApplicant;
app.get('/auth/angellist', routes.auth.angelList);
app.get('/auth/angellist/callback',
routes.auth.angelListCallback,
routes.auth.angelListLogin,
db,
routes.users.findOrAddUser);
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
if (process.env.NODE_ENV ==='production') {
var gitHubOptions = {
clientID: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
callbackURL: 'http://hackhall.com/auth/github/callback'
};
app.set('stripePub', process.env.STRIPE_PUB);
app.set('stripeSecret', process.env.STRIPE_SECRET);
} else {
var gitHubOptions = {
clientID: process.env.GITHUB_CLIENT_ID_LOCAL,
clientSecret: process.env.GITHUB_CLIENT_SECRET_LOCAL,
callbackURL: 'http://localhost:' + app.get('port') + '/auth/github/callback'
};
app.set('stripePub', process.env.STRIPE_PUB_LOCAL);
app.set('stripeSecret', process.env.STRIPE_SECRET_LOCAL);
}
//ALWAYS TEST BEFORE RELEASING!
// app.set('stripePub', process.env.STRIPE_PUB_LOCAL);
// app.set('stripeSecret', process.env.STRIPE_SECRET_LOCAL);
app.use(function(req, res, next){
req.conf = {
stripeSecret: app.get('stripeSecret'),
stripePub: app.get('stripePub')
}
return next()
})
passport.use(new GitHubStrategy(gitHubOptions,
function(accessToken, refreshToken, profile, done) {
// console.log(profile)
if (!profile._json.name) return done(new Error('No first name and last name set on GitHub. We need both names please. You can fill it at https://github.com/settings/profile'))
var firstName = profile._json.name,
lastName = '';
var spaceIndex = profile._json.name.indexOf(' ');
if (spaceIndex>-1) {
firstName = profile._json.name.substr(0, spaceIndex);
lastName = profile._json.name.substr(spaceIndex);
} else {
return done(new Error('We need both names please. No last name set on GitHub. You can fill it at https://github.com/settings/profile'))
}
connection
.model('User', models.User, 'users')
.findOrCreate({
email: profile._json.email,
}, {
githubId: profile.id,
displayName: profile.displayName,
email: profile._json.email,
lastName: lastName,
firstName: firstName,
githubProfile: profile._json,
githubToken: accessToken,
githubUrl: profile.profileUrl,
photoUrl: profile._json.avatar_url
}, function (err, user, created) {
if (user.approved || !created) return done(err, user);
hs.notifyNewApplication(user, function(error_hs, data){
if (error_hs) return done(error_hs)
done(err, user);
})
}
);
}
));
app.get('/auth/github',
passport.authenticate('github'),
function(req, res){
// The request will be redirected to GitHub for authentication, so this
// function will not be called.
});
app.get('/auth/github/callback',
passport.authenticate('github', { failureRedirect: '/#login' }),
function(req, res) {
if (req.isAuthenticated()) {
req.session.auth = true;
req.session.userId = req.user._id;
req.session.user = req.user;
req.session.admin = req.user.admin;
}
if (req.user.approved) {
res.redirect('/#posts');
} else {
res.redirect('/#application');
}
// res.redirect('/');
});
//MAIN
app.get('/api/profile', checkUser, db, routes.main.profile);
app.delete('/api/profile', checkUser, db, routes.main.delProfile);
app.post('/api/login', db, routes.main.login);
app.post('/api/logout', routes.main.logout);
//POSTS
app.get('/api/posts', checkUser, db, routes.posts.getPosts);
app.post('/api/posts', checkUser, db, routes.posts.add);
app.get('/api/posts/:id', checkUser, db, routes.posts.getPost);
app.put('/api/posts/:id', checkUser, db, routes.posts.updatePost);
app.delete('/api/posts/:id', checkUser, db, routes.posts.del);
//USERS
app.get('/api/users', checkUser, db, routes.users.getUsers);
app.get('/api/users/:id', checkUser, db,routes.users.getUser);
app.post('/api/users', checkAdmin, db, routes.users.add);
app.put('/api/users/:id', checkAdmin, db, routes.users.update);
app.delete('/api/users/:id', checkAdmin, db, routes.users.del);
app.get('/api/users.csv', checkAdmin, db, routes.users.getUsersCsv);
//APPLICATION
app.post('/api/application', checkAdmin, db, routes.application.add);
app.put('/api/application', checkApplicant, db, routes.application.update);
app.get('/api/application', checkApplicant, db, routes.application.get);
app.get('/api/vars', checkUser, function(req, res){
var FirebaseTokenGenerator = require("firebase-token-generator");
var tokenGenerator = new FirebaseTokenGenerator(process.env.FIREBASE);
var token = tokenGenerator.createToken(
{uid: req.session.user._id, name: req.session.user.displayName}
);
res.set('Content-type', 'text/javascript').send('var FIREBASE_TOKEN="' + token +'";')
})
app.get('*', function(req, res){
res.status(404).send();
});
app.use(logErrors);
app.use(clientErrorHandler);
app.use(errorHandler);
// var ops = {
// key: fs.readFileSync('host.key'),
// cert: fs.readFileSync('server.crt') ,
// passphrase: ''
// };
// console.log (ops)
if (require.main === module) {
var server = http.createServer(app)
var io = require('socket.io')(server);
io.on('connection', function(socket){
console.log('a user connected');
socket.on('chat message', function(msg){
console.log('message: ' + msg);
});
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
server.listen(app.get('port'), function(){
console.info(c.blue + 'Express server listening on port ' + app.get('port') + c.reset);
});
// https.createServer(ops, app).listen(app.get('port'), function(){
// console.info('HTTPS is running!')
// });
}
else {
console.info(c.blue + 'Running app as a module' + c.reset)
exports.app = app;
}