-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
204 lines (173 loc) · 6.94 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
var express = require('express');
var path = require('path');
var logger = require('morgan');
var compression = require('compression');
var methodOverride = require('method-override');
var session = require('express-session');
var flash = require('express-flash');
var bodyParser = require('body-parser');
var expressValidator = require('express-validator');
var dotenv = require('dotenv');
var exphbs = require('express-handlebars');
var passport = require('passport');
// Load environment variables from .env file
dotenv.load();
// Controllers
var HomeController = require('./controllers/home');
var userController = require('./controllers/user');
var contactController = require('./controllers/contact');
var profileController = require('./controllers/profile');
var newsController = require('./controllers/news');
var eventsController = require('./controllers/events');
var auth = require('./controllers/authenticate');
var voteController = require('./controllers/vote');
// Passport OAuth strategies
require('./config/passport');
var app = express();
var hbs = exphbs.create({
defaultLayout: 'main',
helpers: {
ifeq: function(a, b, options) {
if (a === b) {
return options.fn(this);
}
return options.inverse(this);
},
toJSON : function(object) {
return JSON.stringify(object);
}
}
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.set('port', process.env.PORT || 3000);
app.use(compression());
app.use(logger('dev'));
app.use(bodyParser.json({limit: '20mb' , extended: true }));
app.use(bodyParser.urlencoded({limit: '20mb' , extended: true }));
app.use(expressValidator());
app.use(methodOverride('_method'));
app.use(session({ secret: process.env.SESSION_SECRET, resave: true, saveUninitialized: true }));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(function(req, res, next) {
res.locals.user = req.user ? req.user.toJSON() : null;
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,Authorization');
next();
});
app.use(express.static(path.join(__dirname, 'public')));
// app.get('/', HomeController.index);
// app.get('/contact', contactController.contactGet);
// app.post('/contact', contactController.contactPost);
// app.get('/account', userController.ensureAuthenticated, userController.accountGet);
// app.put('/account', userController.ensureAuthenticated, userController.accountPut);
// app.delete('/account', userController.ensureAuthenticated, userController.accountDelete);
// app.get('/signup', userController.signupGet);
// app.post('/signup', userController.signupPost);
// app.get('/login', userController.loginGet);
// app.post('/login', userController.loginPost);
// app.get('/forgot', userController.forgotGet);
// app.post('/forgot', userController.forgotPost);
// app.get('/reset/:token', userController.resetGet);
// app.post('/reset/:token', userController.resetPost);
// app.get('/logout', userController.logout);
// app.get('/unlink/:provider', userController.ensureAuthenticated, userController.unlink);
app.get("/auth/facebook/token", function(req, res, next){
req.url = "/api/auth/facebook/token";
next();
});
app.get("/auth/facebook", function(req, res, next){
req.url = "/api/auth/facebook";
next();
});
app.get("/auth/facebook/callback", function(req, res, next){
req.url = "/api/auth/facebook/callback";
next();
});
app.get(
"/api/auth/facebook/token",
(req, res) => {
passport.authenticate('facebook-token', function (err, token, info) {
if(err == null){
res.status(200).send(token);
}else{
console.log(err);
}
})(req, res);
}
);
app.get('/api/auth/facebook', passport.authenticate('facebook', { scope: ['email', 'user_location'] }));
app.get('/api/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/', failureRedirect: '/login' }));
app.get(
'/api/auth/check',
(req, res) => {
passport.authenticate('jwt', { session: false},
function (err, token, info) {
if(err == null && info == null){
res.status(200).send(token);
}else if(info!= null){
res.status(401).send(info);
}else{
console.log(err);
}
})(req, res);
}
);
app.post('/api/auth/logout', auth.authenticate, profileController.logout);
app.get('/api/profile', passport.authenticate('jwt', { session: false}), profileController.getProfile);
app.put('/api/profile/:id', auth.authOrAdmin, profileController.editProfile);
app.post('/api/profile/location', auth.authenticate, profileController.userLocation);
app.delete('/api/profile/location', auth.authenticate, profileController.deleteUserLocation);
app.put('/api/profile/location/distance', auth.authenticate, profileController.userLocationDistance);
app.post('/api/register/ios', auth.authenticate, profileController.locationNotification);
app.put('/api/news/promote/:id', auth.modOrAdmin, newsController.promote);
app.get('/api/news/:id', auth.passUser, newsController.getSingleNews);
app.delete('/api/news/:id', auth.authenticate, newsController.delete);
app.get('/api/news', auth.passUser, newsController.getNews);
app.post('/api/news', auth.authenticate, newsController.postNews);
app.post('/api/vote', auth.authenticate, voteController.postVote);
app.get('/api/vote', auth.authenticate, voteController.getWaitVotesUser);
app.get('/api/events/:id', auth.passUser, eventsController.getSingleEvent);
app.get('/api/events', auth.passUser, eventsController.getEvents);
app.post('/api/events', auth.authenticate, eventsController.postEvent);
app.get('/api/users/:id', profileController.getUser);
/*app.get('/auth/profile', passport.authenticate('jwt', { session: false }),
function(req, res) {
res.send(req.user.profile);
}
);*/
var http = require('http');
if (process.env.PRODUCTION == "false"){
var httpServer = http.createServer(app);
httpServer.listen(80,function(){
console.log('Server listening on port ' + 80);
});
}
// Production error handler
if (process.env.PRODUCTION == "true") {
app.use(function(err, req, res, next) {
console.error(err.stack);
res.sendStatus(err.status || 500);
});
var fs = require('fs');
var https = require('https');
var sslPath = process.env.SSLPATH;
var options = {
key: fs.readFileSync(sslPath + 'privkey.pem'),
cert: fs.readFileSync(sslPath + 'fullchain.pem')
};
var httpServer = http.createServer(app);
httpServer.listen(80,function(){
console.log('HTTP Server listening on port ' + 80);
});
var httpsServer = https.createServer(options, app);
httpsServer.listen(443,function(){
console.log('HTTPS Server listening on port ' + 433);
});
}
module.exports = app;