Skip to main content

fetchUserRoles

The fetchUserRoles function is designed to retrieve roles associated with a specific user from the network's RBAC (Role-Based Access Control) system. The function returns an array of roles.

Parameters:

  • userId (type: string, required) - unique identifier of the user for whom roles are to be fetched.

  • owner (type: address, required) - address of the owner of the roles. This is typically the account that manages the roles and permissions.

  • address (type: address, optional) - address of the entity initiating the request. This parameter is optional and can be provided for transaction signing purposes.

  • seed (type: string, optional) - seed for transaction signing. This parameter is optional and can be used for signing transactions related to fetching user roles.

Code example


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

/**
* Fetches roles associated with a specific user from the peaq network's RBAC system.
* @param {string} userId - The ID of the user for whom roles are to be fetched.
* @param {string} owner - The address of the owner of the roles.
* @param {string} [address] - The address of the entity initiating the request (optional).
* @param {string} [seed] - The seed for transaction signing (optional).
* @returns {Promise<Array<Object>>} - A promise that resolves to an array of objects containing user and role details.
*/
async function fetchUserRoles(userId, owner) {
const sdkInstance = await Sdk.createInstance({
baseUrl: "wss://wsspc1-qa.agung.peaq.network",
});

try {
const roles = await sdkInstance.rbac.fetchUserRoles({
userId,
owner,
});

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

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

fetchUserRoles(userId, ownerAddress)
.then((roles) => {
console.log("Fetched Roles:", roles);
})
.catch((error) => {
console.error("Error fetching roles:", error);
});


Response object


[
{
role: "78709009-cf06-4224-8632-b5ee8512",
user: "44759999-az06-2112-7130-b5kk4321"
}
]