-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
99 lines (86 loc) · 2.42 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
const express = require('express');
const authRoutes = require('./routes/auth-routes');
const passportSetup = require('./config/passport-setup');
const session = require('express-session');
const passport = require('passport');
const mongoose = require('mongoose');
const redis = require('redis');
const redisConfig = {
host: process.env.REDIS_HOST || '127.0.0.1',
port: process.env.REDIS_PORT || 6379
};
const redisClient = redis.createClient({ host: redisConfig.host, port: redisConfig.port });
if (process.env.NODE_ENV === 'development') {
const dotenv = require('dotenv');
dotenv.config();
const FileStore = require('session-file-store')(session);
};
if (process.env.NODE_ENV === 'production') {
const RedisStore = require('connect-redis')(session);
};
const app = express();
// setup view engine
app.set('view engine', 'ejs');
// mongodb connection
mongoose.connect(process.env.MONOGO_URL, { useNewUrlParser: true })
.then(() => {
console.log('Connected to Mongodb');
})
.catch(error => {
console.log(error);
});
// REDIS connection
redisClient.on('connect', () => {
console.log('Connected to REDIS');
});
redisClient.on('error', (err) => {
console.log('REDIS Error: ' + err);
});
// session
let sess = {
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: {
maxAge: 24 * 60 * 60 * 1000
}
};
if (process.env.NODE_ENV === 'production') {
app.set('trust proxy', 1) // trust first proxy
sess.cookie.secure = true // serve secure cookies
sess.store = new RedisStore({ host: redisConfig.host, port: redisConfig.port, client: redisClient, ttl: 260 })
};
if (process.env.NODE_ENV === 'development') {
sess.store = new FileStore;
};
app.use(session(sess));
// init passport
app.use(passport.initialize());
app.use(passport.session());
// authentication check - check if user is logged in
const authCheck = (req, res, next) => {
if (!req.user) {
// no user found redirect to login
res.redirect('/auth/authentiq');
} else {
// logged in
next();
}
};
// set up routes
app.use('/auth', authRoutes);
// home page
app.get('/', (req, res) => {
res.render('home', { user: req.user });
});
// secure page
app.get('/profile', authCheck, (req, res) => {
res.render('profile', { user: req.user });
});
// login page
app.get('/login', (req, res) => {
res.render('login', { user: req.user });
});
app.listen(5000, () => {
console.log('Server running on port: 5000');
});