-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (51 loc) · 1.56 KB
/
index.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
const express = require('express');
const _ = require('lodash');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const path = require('path');
const methodOverride = require('method-override');
const bcrypt = require('bcrypt');
const passportSetup = require('./config/passport-setup');
const cookieSession = require('cookie-session');
const passport = require('passport');
const keys = require('./config/keys');
const dateFormat = require('dateformat');
const forceSsl = require('force-ssl-heroku');
mongoose.connect(process.env.MONGOLAB_URI)
mongoose.Promise = global.Promise;
const app = express();
app.use(forceSsl)
app.use(cookieSession({
maxAge: 25 * 60 * 60 * 1000,
keys: [process.env.COOKIE_KEY]
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/auth', require('./routes/auth'));
app.use('/profile', require('./routes/profile'));
app.use('/waypoints', require('./routes/waypoints'));
const authCheck = (req, res, next) => {
if(!req.user){
res.redirect('/auth/login');
} else {
next();
}
};
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.get('/create', authCheck, (req, res) => {
res.render('index', {user: req.user })
});
app.get('/', (req, res) => {
if(req.user) {
res.redirect('profile');
} else {
res.render('login', { user: req.user, dateFormat });
}
});
app.listen(process.env.PORT || 3000, () => {
console.log('listening')
});