-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
62 lines (56 loc) · 1.9 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
//Packages
require('dotenv').config();
const express = require('express');
require('express-async-errors');
const app = express();
const cookieParser = require('cookie-parser');
const start = require('./services/startFunction');
const session = require('express-session');
const passport = require('passport');
const configurePassport = require('./utils/passport-config');
const { notFound, errorHandler } = require('./middlewares');
//Import Routes
const {
authRouter,
userRouter,
adminRouter,
courseRouter,
lessonRouter,
achievementRouter,
searchRouter,
} = require('./routes/routes');
//Use Middlewares
app.use(
'/',
express.Router().get('/', (req, res) => res.send('Welcome!'))
);
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser(process.env.ACCESS_TOKEN_SECRET));
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: { maxAge: 14 * 1000 * 60 * 60 * 24 },
})
);
app.use(passport.initialize());
app.use(passport.session());
app.get('/protected', passport.authenticate('jwt', { session: false }), (req, res) => {
// If the JWT is valid, the user will be stored in req.user
res.json({ msg: 'Accessed protected route', user: req.user });
});
//Use routes
configurePassport(passport);
app.use('/api/v1/auth', authRouter);
app.use('/api/v1/admin', adminRouter);
app.use('/api/v1/user', passport.authenticate('jwt', { session: false }), userRouter);
app.use('/api/v1/courses', passport.authenticate('jwt', { session: false }), courseRouter);
app.use('/api/v1/lessons', passport.authenticate('jwt', { session: false }), lessonRouter);
app.use('/api/v1/achievements', passport.authenticate('jwt', { session: false }), achievementRouter);
app.use('/api/v1/search', passport.authenticate('jwt', { session: false }), searchRouter);
app.use(notFound);
app.use(errorHandler);
start(app);
module.exports = app;