I’m trying to implement something like this question, but this person figured it out and didn’t post the exact code. I’m quite new to Express and I’m trying to figure out how to pass the bearer token to the next middleware to use the createUser api.
My code is like this so far:
const getAccessToken = (req, res, next) => {
console.log("HITS GET ACCESS TOKEN")
const token = request(options, function (error, response, body) {
if (error) throw new Error(error);
return res.json(body);
});
req.token = token
next()
};
const getMgmtToken = (req, res, next) => {
console.log("HITS GET MGMT TOKEN", req.token )
var apiOptions = {
method: 'GET',
url: 'https://dev-ojz7tf-3.us.auth0.com/api/v2/users',
headers: {authorization: req.token},
body: {
"email": "caroline.duff@gmail.com",
"phone_number": "+199999999999999",
"user_metadata": {},
"blocked": false,
"email_verified": false,
"phone_verified": false,
"app_metadata": {},
"given_name": "John",
"family_name": "Doe",
"name": "John Doe",
"nickname": "Johnny",
"picture": "https://secure.gravatar.com/avatar/15626c5e0c749cb912f9d1ad48dba440?s=480&r=pg&d=https%3A%2F%2Fssl.gstatic.com%2Fs2%2Fprofiles%2Fimages%2Fsilhouette80.png",
"user_id": "abc",
"connection": "Initial-Connection",
"password": "secret",
"verify_email": false,
"username": "carolineduff"
}
};
axios.request(apiOptions).then(function (response) {
console.log(response.data);
return res.json(response.data);
}).catch(function (error) {
console.error(error);
});
next()
};
app.post("/createUser", getAccessToken, getMgmtToken, (req, res) => {
return;
});
I can get the access token, and I can make a curl request to the createUser endpoint to successfully create a new user with the access token I get - but I can’t pass the bearer token between requests on my backend.
It’s probably very basic, but I think it would help a few people like me!