forked from auth0/nextjs-auth0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (58 loc) · 1.45 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
const app = require('express')();
const cors = require('cors');
const dotenv = require('dotenv');
const jwt = require('express-jwt');
const jwksRsa = require('jwks-rsa');
const fetch = require('isomorphic-unfetch');
// Load settings from the .env file
dotenv.config();
// Allow all cors, not recommended for production.
app.use(cors());
// Require access tokens.
const requireAuth = jwt({
secret: jwksRsa.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: `https://${process.env.AUTH0_DOMAIN}/.well-known/jwks.json`
}),
audience: process.env.AUTH0_API_IDENTIFIER,
issuer: `https://${process.env.AUTH0_DOMAIN}/`,
algorithm: ['RS256']
});
/**
* This endpoint is open to all.
*/
app.get('/api/shows', (req, res) => {
fetch('https://api.tvmaze.com/shows')
.then(r => r.json())
.then(shows => {
res.send({
shows
});
})
.catch(err =>
res.status(500).send({
error: err.message
})
);
});
/**
* This endpoint requires authentication.
*/
app.get('/api/my/shows', requireAuth, (req, res) => {
fetch('https://api.tvmaze.com/search/shows?q=identity')
.then(r => r.json())
.then(shows => {
res.send({
shows
});
})
.catch(err =>
res.status(500).send({
error: err.message
})
);
});
const port = process.env.PORT || 3001;
app.listen(port, () => console.log(`Sample API listening on http://localhost:${port}`));