-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
58 lines (46 loc) · 1.42 KB
/
server.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
const path = require('path');
const cors = require('cors')
const express = require('express');
const OktaJwtVerifier = require('@okta/jwt-verifier');
const clientId = "0oa1047vvhR9PytaF357";
const oktaDomain = "https://dev-499390.okta.com";
const oktaJwtVerifier = new OktaJwtVerifier({
issuer: `${oktaDomain}/oauth2/default`,
clientId: clientId
});
const app = express();
const port = 3000;
app.use(cors());
// public route
app.get('/api/publicInfo', (req, res) => {
res.status(200).send('You are viewing public info');
});
// protected route
app.get('/api/profile', verifyToken, (req, res) => {
oktaJwtVerifier.verifyAccessToken(req.token)
.then(jwt => {
res.send('You are viewing private profile info');
})
.catch(err => {
res.sendStatus(403);
});
});
app.get('/css/bootstrap.min.css', function (req, res) {
res.sendFile(path.join(__dirname, 'src/css/bootstrap.min.css'));
});
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, 'public/index.html'));
});
function verifyToken(req, res, next) {
const bearerHeader = req.headers['authorization'];
if (bearerHeader) {
const bearer = bearerHeader.split(' ');
const bearerToken = bearer[1];
req.token = bearerToken;
next();
} else {
// Forbidden
res.sendStatus(403);
}
}
app.listen(port, () => console.log(`Listening on port: ${port}!`))