UserGroups
Read the user groups of your project: the flat list, the top level, a single group, or the children of one.
More information about user groups in the OneEntry admin panel: https://doc.oneentry.cloud/docs/category/users (opens in a new tab)
The UserGroups module reads the groups your visitors belong to. A group decides what a visitor may see. Groups form a tree: a group can have a parent and children.
You create and nest groups in the OneEntry admin panel; the SDK only reads them. Every project has at least the guest group, which unauthenticated visitors belong to.
Quickstart
Initialize the module from defineOneEntry:
const { } = defineOneEntry( , { });
Fetch the whole list, or start at the top of the tree:
// Every group, flat, regardless of nesting.
const groups = await UserGroups.getUserGroups();
if ('statusCode' in groups) throw new Error(groups.message);
console.log(groups.length);
// Only the groups without a parent.
const roots = await UserGroups.getRootUserGroups();
if ('statusCode' in roots) throw new Error(roots.message);
roots.forEach((group) => {
console.log(group.identifier, group.localizeInfos.title);
});
Walking the tree
Fetch a group's children by the parent's marker instead of matching parentId:
const children = await UserGroups.getChildUserGroupsByMarker('guest');
For a group with no children, the method resolves to [].
childrenCount is a string
The API sends childrenCount as a string, not a number:
{ "identifier": "guest", "childrenCount": "0" }
Convert it before comparing:
if (Number(group.childrenCount) > 0) {
const children = await UserGroups.getChildUserGroupsByMarker(group.identifier);
}
Methods
| Method | What it returns |
|---|---|
| getUserGroups() | Every group as a flat array |
| getRootUserGroups() | Groups without a parent |
| getUserGroupById() | One group by its numeric ID |
| getUserGroupByMarker() | One group by its marker |
| getChildUserGroupsByMarker() | Children of the given group |
Related
- Users Module:
groupson a user lists the IDs of its groups