-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (61 loc) · 2.1 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const express = require('express');
const path = require('path');
const jwt = require('jsonwebtoken');
require('dotenv').config();
const app = express();
// Authorization middleware
const authorizeUser = (req, res, next) => {
const token = req.query.Authorization?.split('Bearer ')[1];
if (!token) {
return res.status(401).send('<h1 align="center"> Login to Continue </h1>');
}
try {
// Verify and decode the token
const decodedToken = jwt.verify(token, process.env.SECRET_KEY, { algorithms: ['HS256'] });
req.user = decodedToken;
next(); // Proceed to the next middleware
} catch (error) {
return res.status(401).json({ message: 'Invalid authorization token' });
}
};
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'src/html/login.html'));
});
app.get('/js/login.js', (req, res) => {
res.sendFile(path.join(__dirname, 'src/js/login.js'))
});
app.get('/css/login.css', (req, res) => {
res.sendFile(path.join(__dirname, 'src/css/login.css'))
});
app.get('/css/index.css', (req, res) => {
res.sendFile(path.join(__dirname, 'src/css/index.css'))
});
app.get('/css/admin.css', (req, res) => {
res.sendFile(path.join(__dirname, 'src/css/admin.css'))
});
app.get('/assets/eth5.jpg', (req, res) => {
res.sendFile(path.join(__dirname, 'src/assets/eth5.jpg'))
});
app.get('/js/app.js', (req, res) => {
res.sendFile(path.join(__dirname, 'src/js/app.js'))
});
app.get('/admin.html', authorizeUser, (req, res) => {
res.sendFile(path.join(__dirname, 'src/html/admin.html'));
});
app.get('/index.html', authorizeUser, (req, res) => {
res.sendFile(path.join(__dirname, 'src/html/index.html'));
});
app.get('/dist/login.bundle.js', (req, res) => {
res.sendFile(path.join(__dirname, 'src/dist/login.bundle.js'));
});
app.get('/dist/app.bundle.js', (req, res) => {
res.sendFile(path.join(__dirname, 'src/dist/app.bundle.js'));
});
// Serve the favicon.ico file
app.get('/favicon.ico', (req, res) => {
res.sendFile(path.join(__dirname, 'public/favicon.ico'));
});
// Start the server
app.listen(8080, () => {
console.log('Server listening on http://localhost:8080');
});