forked from raymondfeng/loopback4-example-auth0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
53 lines (46 loc) · 1.12 KB
/
client.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
/* eslint-disable @typescript-eslint/camelcase */
const request = require('request');
/**
* Please add `auth0-secret.json` for your credentials
* @example
* ```json
* {
* "client_id": "{CLIENT_ID}",
* "client_secret": "{CLIENT_SECRET}",
* "audience": "http://localhost:3000/ping"
* }
* ```
*/
const secrets = require('./auth0-secret');
/**
* Request an access token using client credentials
*/
const tokenReq = {
method: 'POST',
url: 'https://apitoday.auth0.com/oauth/token',
headers: {'content-type': 'application/json'},
json: true,
body: {
...secrets,
grant_type: 'client_credentials',
scope: 'greet',
},
};
request(tokenReq, function(tokenError, response, body) {
if (tokenError) throw new Error(tokenError);
console.log(body);
/**
* Now try to run the /greet api using the access token
*/
const greetReq = {
method: 'GET',
url: 'http://localhost:3000/greet',
headers: {
authorization: `Bearer ${body.access_token}`,
},
};
request(greetReq, function(greetError, response2, user) {
if (greetError) throw new Error(greetError);
console.log(user);
});
});