-
Notifications
You must be signed in to change notification settings - Fork 18
/
api-server.js
50 lines (40 loc) · 1.24 KB
/
api-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
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const morgan = require("morgan");
const helmet = require("helmet");
const jwt = require("express-jwt");
const jwksRsa = require("jwks-rsa");
const app = express();
const port = 7000 || process.env.API_PORT;
const appOrigin = process.env.APP_ORIGIN;
const audience = process.env.AUTH0_AUDIENCE;
const issuer = process.env.AUTH0_ISSUER;
if (!issuer || !audience) {
throw new Error("Please make sure that .env is in place and populated");
}
app.use(morgan("dev"));
app.use(helmet());
app.use(cors({ origin: appOrigin }));
const checkJwt = jwt({
secret: jwksRsa.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: `${issuer}.well-known/jwks.json`,
}),
audience: audience,
issuer: issuer,
algorithms: ["RS256"],
});
app.get("/api/messages/public-message", (req, res) => {
res.send({
message: "The API doesn't require an access token to share this message.",
});
});
app.get("/api/messages/protected-message", checkJwt, (req, res) => {
res.send({
message: "The API successfully validated your access token.",
});
});
app.listen(port, () => console.log(`API Server listening on port ${port}`));