forked from auth0/express-openid-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccess-an-api.js
34 lines (29 loc) · 878 Bytes
/
access-an-api.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
const express = require('express');
const request = require('request-promise-native');
const { auth } = require('../');
const app = express();
const { API_PORT = 3002 } = process.env;
app.use(
auth({
authorizationParams: {
response_type: 'code',
audience: 'https://api.example.com/products',
scope: 'openid profile email offline_access read:products',
prompt: 'consent',
},
})
);
app.get('/', async (req, res) => {
let { token_type, access_token, isExpired, refresh } = req.oidc.accessToken;
if (isExpired()) {
({ access_token } = await refresh());
}
const products = await request.get(`http://localhost:${API_PORT}/products`, {
headers: {
Authorization: `${token_type} ${access_token}`,
},
json: true,
});
res.send(`Products: ${products.map(({ name }) => name).join(', ')}`);
});
module.exports = app;