-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (62 loc) · 2.24 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
66
67
68
69
70
71
72
73
74
const ClientOAuth2 = require('client-oauth2');
const express = require('express');
const popsicle = require('popsicle');
const app = express();
const clientRequest = require('client-oauth2/src/request');
const loggingRequest = (method, url, body, headers) => {
console.log({
method,
url,
body,
headers
});
return clientRequest(method, url, body, headers);
}
const fdcBaseUrl = process.env.fdcApiUri;
const fdcAuth = new ClientOAuth2({
clientId: process.env.clientId,
clientSecret: process.env.clientSecret,
accessTokenUri: process.env.accessTokenUri,
authorizationUri: process.env.authorizationUri,
redirectUri: process.env.redirectUri,
scopes: ['oms']
}, loggingRequest);
app.get('/auth/fdc', function (req, res) {
const uri = fdcAuth.code.getUri();
console.log(`Redirecting to ${uri} to allow the user to login and receive an auth code`);
res.redirect(uri)
})
app.get('/auth/fdc/callback', async function (req, res) {
let user = await fdcAuth.code.getToken(req.originalUrl);
// Store the `access_token` and `refresh_token` token somewhere
// and optionally store `expires` if you would like to optimistically
// refresh the token before it expires
console.log('Successful Authorization Response');
console.log(user.data); //=> { access_token: '...', token_type: 'bearer', ... }
// Example of refreshing a token
// which should be used when an access token returns 401 because it has expired
user = await user.refresh();
// Get user info from FDC API
console.log({
url: `${fdcBaseUrl}/users/me`,
header: {
'Content-Type': 'application/json',
Authorization: `Bearer ${user.data.access_token}`
},
method: 'GET'
})
const userInfoFromApiResponse = await popsicle.fetch(`${fdcBaseUrl}/users/me`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${user.data.access_token}`
}
});
const userInfo = await userInfoFromApiResponse.json();
const responseData = {
auth: user.data,
user: userInfo
};
return res.send(JSON.stringify(responseData));
})
app.listen(8001, 'localhost');