-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
60 lines (49 loc) · 1.66 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
const express = require('express');
const app = express();
// npm dependencies
const path = require('path');
const bodyParser = require('body-parser');
const passport = require('passport');
const cookieParser = require('cookie-parser');
const expressSession = require('express-session');
// Database
const db = require('./db_wrapper');
// Parsing
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json())
app.use(cookieParser());
// Session
app.use(expressSession({secret: 'mySecretKey',
saveUninitialized:false,
resave:false}));
// Passport
app.use(passport.initialize());
app.use(passport.session());
const initPassport = require('./passport/init');
initPassport(passport);
// Routes
const isAuthenticated = function (req, res, next) {
// if user is authenticated in the session, call the next() to call the next request handler
// Passport adds this method to request object. A middleware is allowed to add properties to
// request and response objects
if (req.isAuthenticated())
return next();
// if the user is not authenticated then redirect him to the login page
console.log("not logged in");
res.redirect('/');
}
const auth = require('./routes/auth')(db, passport);
app.use('/', auth);
const api = require('./routes/api')(db, isAuthenticated);
app.use('/', api);
// Static dir
const DIST_DIR = path.join(__dirname, 'frontend/public');
app.use(express.static(DIST_DIR));
// app.get('/') will mess up react-router settings
app.get('*', function (req, res) {
res.sendFile(path.join(DIST_DIR, 'index.html'));
});
app.db = db;
module.exports = app;