i am trying to write code to retrive all users who exist in the keycloak user list.i am getting all the users inside the keycloak. but i want to get only the usernames or ids from the full list of users and store that value inside an array which i can use to assign role to user
myfile.cs
let userName = []
function GetUser(userName,kc_accessToken) {
let url = `${path}/users`;
return axios_instance.get(url,
{
headers: {
"content-type": "application/json",
"authorization": `Bearer ${kc_accessToken}`
}
}).then(function (response) {
console.log("User names!!");
//full list (working)
//userName = response.data
//console.log(response.data);
//only usernames or ids (not working)
userName = response.data.username
console.log(response.data.username);
})
.catch(function (error) {
console.log("No Users");
});
}
function call
http.createServer(function Test() {
getAccessToken().then(function (response) {
kc_accessToken = response.data.access_token;
GetUser(userName,kc_accessToken).then((resp) => {
})
}).catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});;
}).listen(8081);
my output when i try to list all users
my output when i am trying to get only usernames of the users
You get an array of user objects in response.data
, so you can simply apply a map function to generate an array of strings containing the users names:
const usernames = response.data.map(user => user.username)