-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
122 lines (108 loc) · 3 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
const express = require('express');
const createHttpError = require('http-errors');
const morgan = require('morgan');
const mongoose = require('mongoose');
require('dotenv').config();
const session = require('express-session');
const connectFlash = require('connect-flash');
const passport = require('passport');
const connectMongo = require('connect-mongo');
const { ensureLoggedIn } = require('connect-ensure-login');
const { roles } = require('./utils/constants');
// Initialization
const app = express();
app.use(morgan('dev'));
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
//const MongoStore = connectMongo(session);
// Init Session
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
// secure: true,
httpOnly: true,
},
//store: new connectMongo({ mongooseConnection: mongoose.connection }),
})
);
// For Passport JS Authentication
app.use(passport.initialize());
app.use(passport.session());
require('./utils/passport.auth');
app.use((req, res, next) => {
res.locals.user = req.user;
next();
});
// Connect Flash
app.use(connectFlash());
app.use((req, res, next) => {
res.locals.messages = req.flash();
next();
});
// Routes
app.use('/', require('./routes/index.route'));
app.use('/auth', require('./routes/auth.route'));
app.use(
'/user',
ensureLoggedIn({ redirectTo: '/auth/login' }),
require('./routes/user.route')
);
app.use(
'/admin',
ensureLoggedIn({ redirectTo: '/auth/login' }),
ensureAdmin,
require('./routes/admin.route')
);
// 404 Handler
app.use((req, res, next) => {
next(createHttpError.NotFound());
});
// Error Handler
app.use((error, req, res, next) => {
error.status = error.status || 500;
res.status(error.status);
res.render('error_40x', { error });
});
// Setting the PORT
const PORT = process.env.PORT || 3000;
// Making a connection to MongoDB
mongoose
.connect(process.env.MONGO_URI, {
dbName: process.env.DB_NAME,
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log('💾 connected...');
// Listening for connections on the defined PORT
app.listen(PORT, () => console.log(`🚀 @ http://localhost:${PORT}`));
})
.catch((err) => console.log(err.message));
// function ensureAuthenticated(req, res, next) {
// if (req.isAuthenticated()) {
// next();
// } else {
// res.redirect('/auth/login');
// }
// }
function ensureAdmin(req, res, next) {
if (req.user.role === roles.admin) {
next();
} else {
req.flash('warning', 'you are not Authorized to see this route');
res.redirect('/');
}
}
function ensureModerator(req, res, next) {
if (req.user.role === roles.moderator) {
next();
} else {
req.flash('warning', 'you are not Authorized to see this route');
res.redirect('/');
}
}