Manage Users Roles and Groups
Define and manage roles and groups that you can apply to your users
Last updated
Define and manage roles and groups that you can apply to your users
Last updated
Roles and Groups can extend access management on your application that Authgear powers. Authgear includes a user's roles in the response from the UserInfo endpoint and in the JWT access token.
The following case is an example of how you can use roles to deliver unique features and interfaces to different categories of users of your application. Let's say you have an application XYZ that powers a transportation business. The app has roles for admin
, driver
and passenger
. You can use the value Authgear returns in UserInfo or JWT access token to enable features for managing drivers to a user with admin
role only. Similarly, you can allow only users with driver
role view features to manage their vehicle, passengers, and trip. Finally, you can restrict users with the passenger
role to only view their ticket and trip details.
In this post, you'll learn how to create roles and groups for your Authgear project and how to manage them.
You can create a group in your Authgear project and add one or more roles to the group. Both groups and roles can be applied to a user. For instance, if your Authgear project, has the following roles:
management
department_lead
team_member
And a managers
group, you can add the management
and department_lead
roles to the group. Then, adding a user to the managers
group will grant them both management
and department_lead
roles.
The following figure shows the core relationship with Groups, Roles, and Users in Authgear.
Each role or group has three properties that describe it. These include a required key
, and optional name
and description
.
The key
property serves as the identifier for the role or group. The value for key
is what AUthgear will include in the UserInfo and JWT access token of a user. A valid key must meet the following conditions:
The value of key
can not be empty.
A key must be between 1 and 40 characters long.
The valid characters are [a-zA-Z0-9:_]
.
The prefix authgear:
is reserved for Authgear internal use.
The following are some examples of valid keys:
reader
app:editor
store_manager
Name is an optional label for a role or group. The value for the name property is what will be displayed in Authgear portal. It has less strict constrict for allowed characters. For example, you can use white spaces here.
The description property is also optional. You can use it to give more details about a role or group.
In the next section of this post, we'll show you how to create roles and groups for your project.
The. following JSON document shows how roles are returned when your application requests for UserInfo:
The value of the https://authgear.com/claims/user/roles
attribute is an array that contains the roles of the current user.
It is up to you to create roles for your Authgear project based on your unique need and use case. You can create new roles using the Authgear Portal or the Admin API (using GraphQL).
Follow these steps to create new roles in your Authgear project via the Portal.
First, log in to Authgear Portal, select your project then navigate to User Management > Roles to open the Roles page.
Click the Create Role button in the middle of the Roles page (or the top right corner if you have existing roles) to start creating a new role.
Enter the Name, Role Key, and Description of your new role in their respective fields. Click on the Create button at the bottom of the form to finish creating your new role.
Repeat the above steps to add more roles.
As mentioned earlier in this post, you can also create new roles using the Admin API. In this section, we'll walk you through the steps for doing that.
Set up your custom code with the necessary authorization to interact with the Admin API GraphQL endpoint. Or use the GraphiQL Explorer provided by Authgear (Navigate to Advanced > Admin API > GraphiQL Explorer in Authgear Portal to access).
Run the following mutation to create a new role:
Just like roles, you are responsible for creating groups for your Authgear project. You can also create new groups either via Authgear Portal or using the Admin API.
The steps for creating a new group from the Portal are similar to that of roles. The steps are as follows:
First, log in to Authgear Portal, select your project then navigate to User Management > Groups to open the Groups page.
Click the Create Group button in the middle of the screen (or in the top right corner if you already have some groups) to access the Create Group form.
Enter the Name, Group Key and Description in the group creation form then click the Create button to finish.
It's possible to create a new group using the Admin API GraphQL. The following guide shows the steps for doing that.
Implement your own custom code that has access to the Admin API endpoint. Alternatively, you can use the GraphiQL Explorer in Authgear Portal (Navigate to Advanced > Admin API > GraphiQL Explorer).
Execute the following GraphQL mutation to create a new group:
You can manage the roles you've created via the Portal or using the Admin API. In this section, we'll cover how to manage roles.
To add a role to a user, first, in the Authgear Portal, navigate to User Management > Users. From this page, you can view a list of all the users who have signed up on your Authgear project. You can also search users by their current roles there.
Next, click on the user you wish to add a new role to. This will open their User Details page. From that page, navigate to the Roles tab
Then, click the Add Roles button, select a role from the drop-down menu, and click Add. If the drop-down is empty, it is possible that you have not created any roles yet. See how to create roles.
To remove an existing role from a user, click on the Remove button next to the role in the user's Roles tab.
Adding a group to a role is a way of connecting a role and a group from the role management side.
To do this in Authgear Portal, first navigate to User Management > Roles, then click on the role you wish add a group to.
Next, in the details page for the selected role, go to the Groups tab, then click the Add to Group button. Select the group you wish to add from the drop-down then click Add. If there's no groups in the dropdown, try creating some new groups first.
To update the properties of an existing role such as the name, description or key, navigate to User User Management > Roles in Authgear Portal. Click on the role you wish to modify to open the Settings tab, then change the value for the property you wish to update in the form. Once you're done, click on the Save button.
To delete a role, click on the Delete button once you open the role's Settings tab.
To add a group to a user's profile in the Portal, first navigate to User Management > Users and select a user.
In the User Details page, switch to the Groups tab then click on Add to Group.
Select the group you wish to add the user to from the drop-down then click on Add.
To add a role to a group, navigate to User Management > Groups then click on the group you wish to add roles to.
In the group details page, switch to the Roles tab, then click on the Add Role button.
Select the roles you wish to add to the group from the drop-down then click on the Add button.
To update an existing group, go to User Management > Groups and click on the group you wish to update. Modify the property you wish to update using the form in the group's Setting tab. Once you're done, click on Save to keep your changes.
To delete an existing group, click on the Delete Group button once you're in the Settings tab for that group.