-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
35 lines (29 loc) · 1.04 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
const express = require('express');
var cors = require('cors');
const { serverConfig } = require('./server/config');
const cookieParser = require('cookie-parser');
const createRouter = require('./server/routes/create');
var jwt = require('jsonwebtoken');
let app = express();
app.use(cors());
app.use(cookieParser());
//Exercise 1
app.get('/verifytoken', (req, res) => {
if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
let accessToken = req.headers.authorization.split(' ')[1];
jwt.verify(accessToken, 'stackconf', function(err, decoded) {
if (err) {
res.send(`Oops, you are NOT authorised to view this page because: ${err.message}`);
} else {
res.send('Congrats, you are authorised to view this page.');
}
});
} else {
res.send('No Authorisation header with Bearer token found.');
}
});
app.get('/', (req, res) => {
res.send('Running!')
});
app.use('/api', createRouter);
app.listen(serverConfig.port, () => console.log(`Authorisation Server is listening on port ${serverConfig.port}!`));