I have Nodejs code using request library to get the authentication token from a url.
var request = require("request")
const wellish_dev_url = "https://dev.wellish.com"
function get_auth_token(){
const api_end_point = wellish_dev_url + "/api/v1/auth"
var options = {
url: api_end_point,
headers: {"Content-Type": "application/json"},
auth: {
'user': 'admin',
'pass': 'password'}
}
var r = request.get(options, function(error, response, body){
if(!error && response.statusCode==200){
var token = JSON.parse(body)
var auth_token = token["data"][0]["accessToken"]
// console.log(auth_token)
return auth_token
}
else{
console.log("Code : " + response.statusCode)
console.log("error : " + error)
console.log('body : ' + body)
}
})
}
// get_auth_token()
var auth_token_new = get_auth_token()
console.log(auth_token_new)
I want to return the auth_token
to be used as an argument in another function. However, it shows undefined.
I look up online to use cb
, however, I have no idea how it works. I really want to return the value without using cb
. Any helps?