Skip to main content

fetchUserGroups

fetchUserGroups function is used to retrieve user groups that are linked to a specific owner and user within the RBAC (Role-Based Access Control) system. The function returns an array of objects, where each object represents a user group and contains information about the user and group associations.

Parameters:

  • owner (string, required): address of the owner for whom to fetch user groups.
  • userId (string, required): ID of the user for whom to fetch user groups.

Code example


import { Sdk } from "@peaq-network/sdk";

/**
* Fetches user groups associated with a specific owner and user from the RBAC system.
* @param {string} owner - The address of the owner for whom to fetch user groups (required).
* @param {string} userId - The ID of the user for whom to fetch user groups (required).
* @returns {Promise<Array<Object>>} - A promise that resolves to an array of objects containing fetched user group details.
*/
const fetchUserGroups = async (owner, userId) => {
const sdkInstance = await Sdk.createInstance({
baseUrl: "wss://wsspc1-qa.agung.peaq.network",
});

try {
const userGroups = await sdkInstance.rbac.fetchUserGroups({
owner,
userId,
});

return userGroups;
} finally {
await sdkInstance.disconnect();
}
};

// Example usage
const ownerAddress = "5GsA2xjSBY6DhpTGKKdJ4BjUYpbBaP5wxZnvBoHSytURn6N2";
const userId = "44759999-az06-2112-7130-b5kk4321";

fetchUserGroups(ownerAddress, userId)
.then((userGroups) => {
console.log("Fetched User Groups:", userGroups);
})
.catch((error) => {
console.error("Error fetching user groups:", error);
});


Response object

[
{
"user": "44759999-az06-2112-7130-b5kk4321",
"group": "02709409-kd01-4444-8222-b5zz8512",
}
]