-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
142 lines (121 loc) · 5 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
/* eslint-disable */
const PROD_WARNING_MESSAGE = `
██ ██ █████ ██████ ███ ██ ██ ███ ██ ██████ ██
██ ██ ██ ██ ██ ██ ████ ██ ██ ████ ██ ██ ██
██ █ ██ ███████ ██████ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██
██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
███ ███ ██ ██ ██ ██ ██ ████ ██ ██ ████ ██████ ██
You are running the app using production config (Firestore Database, Map API keys, etc).
If you are developing the app locally, please use "npm run dev" to start the app.
`;
/* eslint-enable */
// Show production warning message and load API keys.
if (process.env.NODE_ENV === 'production') {
console.log(PROD_WARNING_MESSAGE);
require('dotenv').config({path: '.env.prod'});
} else {
require('dotenv').config({path: '.env.dev'});
}
// Module dependencies.
const express = require('express');
const http = require('http');
const path = require('path');
const handlebars = require('express-handlebars');
const passport = require('passport');
const cookieSession = require('cookie-session');
require('./passport-auth');
const app = express();
// Handlebars helpers.
const handlebarsConfig = handlebars.create({
helpers: {
json: function (data) {
return JSON.stringify(data);
},
},
});
const testMiddleware = (req, res, next) => {
if (req && req.session && req.session.tmp_user) {
req.user = req.session.tmp_user;
}
if (next) next();
};
const testAccountInfo = require('./test/testAccountInfo.json');
const testIsLoggedIn = (req, res, next) => {
if (!req.user) req.user = {};
req.session.tmp_user = testAccountInfo;
next();
};
// Environments configs.
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', handlebarsConfig.engine);
app.set('view engine', 'handlebars');
app.use(express.urlencoded());
app.use(express.json());
app.use('/static', express.static('public'));
app.use(
cookieSession({
name: 'auth-session',
keys: ['key1', 'key2'],
})
);
app.use(passport.initialize());
app.use(passport.session());
// Authentication middleware
const isLoggedIn = (req, res, next) => {
req.user ? next() : res.redirect('/auth/google');
};
// Routes.
app.get('/', (req, res) => res.render('landing', {user: req.user}));
const discover = require('./routes/discover');
app.get('/discover', discover.view);
app.get('/discover/:filter', discover.getOrganizations);
const moreInfo = require('./routes/moreInfo');
app.get('/discover/organization/:id', moreInfo.view);
const dashboard = require('./routes/dashboard');
if (process.env.NODE_ENV === 'testing') {
app.use(testMiddleware);
app.get('/dashboard/:page?', testIsLoggedIn, dashboard.view);
} else {
app.get('/dashboard/:page?', isLoggedIn, dashboard.view);
}
app.get('/auth/google', passport.authenticate('google', {scope: ['profile', 'email']}));
app.get(
'/auth/google/callback',
passport.authenticate('google', {failureRedirect: '/'}),
(req, res) => res.redirect('/dashboard')
);
app.get('/auth/logout', (req, res) => {
req.session = null;
req.logout();
res.redirect('/');
});
const data = require('./routes/data');
app.get('/data/acceptedcategories/:id', data.acceptedCategoriesGet);
app.post('/data/acceptedcategories/:id', data.acceptedCategoriesPost);
app.delete('/data/acceptedcategories/:id', data.acceptedCategoriesDelete);
app.get(
'/data/acceptedcategories/:field(organization|category)/:id',
data.acceptedCategoriesByFieldGet
);
app.post('/data/acceptedcategories/organization/:id', data.acceptedCategoriesOrganizationPost);
app.get('/data/member', data.getMember);
app.get('/data/member/organization/:id', data.getOrganizationFromMember);
app.get('/data/member/favorites', data.getFavorites);
app.post('/data/member/favorites/:organizationID', data.postFavorite);
app.delete('/data/member/favorites/:organizationID', data.deleteFavorite);
app.get('/data/organization/member/:id', data.getMemberFromOrganization);
app.get('/data/organizations/:id', data.organizationsGet);
app.post('/data/organizations/:id', data.organizationsPost);
app.get('/data/categories', (req, res) => {
data.getCategories().then((categories) => res.send(categories));
});
http.createServer(app).listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
if (process.env.NODE_ENV === 'development') {
console.log(`DEV: Open at http://localhost:${app.get('port')}`);
}
app.emit('app_started');
});
// Exporting for running unit tests.
module.exports = app;