-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
60 lines (54 loc) · 1.5 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()
const port = 8080
const router = require('./routes/routes');
const { verify_jwt } = require('./functions/auth');
const bodyParser = require('body-parser');
const cookieParser = require("cookie-parser");
const fileUpload = require('express-fileupload');
const dotenv = require('dotenv');
dotenv.config();
const authMiddleware = (req,res,next) => {
const protected_pages = ["post-comment","add-meme", "add-meme-action", "logout-user", "settings", "rate-meme"]
const protected_pages_from_logged_in = ["login", "register"]
const verification = verify_jwt(req.cookies['request-token']);
let stop_request = false;
if (verification.verified) {
req.authStatus = {
logged_in:true,
user_data:verification.data.data
}
protected_pages_from_logged_in.forEach(page=>{
if (req.path.search(page) !== -1) {
res.redirect("/")
}
})
} else {
req.authStatus = {
logged_in:false,
user_data:{
username:"null",
}
}
protected_pages.forEach(page=>{
if (req.path.search(page) !== -1) {
res.redirect("/login")
console.log("YEAHHH")
stop_request = true;
}
})
}
if (!stop_request) {
next()
}
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(fileUpload({
createParentPath: true
}));
app.use(cookieParser());
app.use(authMiddleware);
app.use(express.static('public'));
app.use('/', router);
app.listen(port, () => {});