-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
47 lines (37 loc) · 1.2 KB
/
Copy pathserver.js
File metadata and controls
47 lines (37 loc) · 1.2 KB
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
const express = require('express');
const morgan = require('morgan');
const helmet = require('helmet');
const jwt = require('express-jwt');
const jwksRsa = require('jwks-rsa');
const { join } = require('path');
const authConfig = require('./auth_config.json');
const app = express();
if (!authConfig.domain || !authConfig.audience) {
throw 'Please make sure that auth_config.json is in place and populated';
}
app.use(morgan('dev'));
app.use(helmet());
const checkJwt = jwt({
secret: jwksRsa.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: `https://${authConfig.domain}/.well-known/jwks.json`
}),
audience: authConfig.audience,
issuer: `https://${authConfig.domain}/`,
algorithms: ['RS256']
});
app.get('/api/external', checkJwt, (req, res) => {
res.send({
msg: 'Your access token was successfully validated!'
});
});
if (process.env.NODE_ENV === 'production') {
app.use(express.static(join(__dirname, 'dist')));
app.get('/*', (_, res) => {
res.sendFile(join(__dirname, 'dist', 'index.html'));
});
}
const port = process.env.NODE_ENV === 'production' ? 3000 : 3001;
app.listen(port, () => console.log(`Server started on port ${port}`));