# Manage Users Roles and Groups

## 1.0 Introduction

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](https://docs.authgear.com/~/changes/S9PzpQKqtjCE4EDWHGO4/reference/apis/oauth-2.0-and-openid-connect-oidc/userinfo) 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.

<figure><img src="https://2638622528-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAjXpMovvVxeIY33s_K%2Fuploads%2FXxVFfs6tfS0270N8jVg2%2Froles-group-flowchart.png?alt=media&#x26;token=8640c93a-68e6-4b76-8d79-1da3ec648b78" alt=""><figcaption><p>roles and group flowchart</p></figcaption></figure>

### 1.1 Properties of Roles and Groups

Each role or group has three properties that describe it. These include a required `key`, and optional `name` and `description`.

#### 1. Role/Group Key

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`

#### 2. Name

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.

#### 3. Description

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.

### 1.2 Example of Role in UserInfo Endpoint Response

The. following JSON document shows how roles are returned when your application requests for UserInfo:

```json
{
  "custom_attributes": [],
  "email": "user@example.com",
  "email_verified": true,
  "family_name": "John",
  "gender": "male",
  "given_name": "Doe",
  "https://authgear.com/claims/user/can_reauthenticate": true,
  "https://authgear.com/claims/user/is_anonymous": false,
  "https://authgear.com/claims/user/is_verified": true,
  "https://authgear.com/claims/user/roles": [
    "department_lead",
    "team_member"
  ],
  "sub": "e3079029-f230-4c24-91c1-c2cd63a6a4af",
  "updated_at": 1694947082,
  "x_web3": {
    "accounts": []
  }
}
```

The value of the `https://authgear.com/claims/user/roles` attribute is an array that contains the roles of the current user.

## 2.0 How to Create a New Role

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](https://docs.authgear.com/reference/apis/admin-api) (using GraphQL).

### 2.1 Creating Roles from the Authgear Portal

Follow these steps to create new roles in your Authgear project via the Portal.

#### Step 1

First, log in to Authgear Portal, select your project then navigate to **User Management** > **Roles** to open the **Roles** page.

<figure><img src="https://2638622528-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAjXpMovvVxeIY33s_K%2Fuploads%2FLjMRc1qz24Ax2gg9Kxbf%2Fauthgear-roles.png?alt=media&#x26;token=0c38bc22-52d3-4d08-8505-3f9f8cddd29e" alt=""><figcaption><p>navigate to roles management page</p></figcaption></figure>

#### Step 2

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.

<figure><img src="https://2638622528-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAjXpMovvVxeIY33s_K%2Fuploads%2FZewv0iFc4UbDC3NDoTjh%2Fauthgear-roles-create.png?alt=media&#x26;token=bc082e08-39af-4fde-86e7-8bde48be7ae9" alt=""><figcaption><p>create new role page</p></figcaption></figure>

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.

### 2.2 Creating Roles using the Admin API

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.

#### Step 1

Set up your custom code with the [necessary authorization to interact with the Admin API](https://docs.authgear.com/reference/apis/admin-api/authentication-and-security) GraphQL endpoint. Or use the GraphiQL Explorer provided by Authgear (Navigate to **Advanced** > **Admin API** > **GraphiQL Explorer** in Authgear Portal to access).

#### Step 2

Run the following mutation to create a new role:

{% tabs %}
{% tab title="Query" %}

```graphql
mutation {
  createRole(input: { key:"department_lead", name:"Team Lead", description: "Role for a leader of a specific department" }) {
    role {
      id
      key
    }
  }
}
```

{% endtab %}

{% tab title="Response" %}

```json
{
  "data": {
    "createRole": {
      "role": {
        "id": "Um9sZTozNDM5MWQ1Ni00OTEwLTQ1ZDAtOTI1Yi1lMjQxODFhYmMxODc",
        "key": "department_lead"
      }
    }
  }
}
```

{% endtab %}
{% endtabs %}

## 3.0 How to Create a New Group

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.

### 3.1 Creating Groups from the Authgear Portal

The steps for creating a new group from the Portal are similar to that of roles. The steps are as follows:

#### Step 1

First, log in to Authgear Portal, select your project then navigate to **User Management** > **Groups** to open the **Groups** page.

<figure><img src="https://2638622528-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAjXpMovvVxeIY33s_K%2Fuploads%2Fb6y88JuQNIOZPGG7Ferk%2Fauthgear-groups.png?alt=media&#x26;token=4e2d56df-cd4a-44ea-892e-4e016b08525a" alt=""><figcaption><p>navigate to groups management page</p></figcaption></figure>

#### Step 2

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.

<figure><img src="https://2638622528-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAjXpMovvVxeIY33s_K%2Fuploads%2FZJN9QMJHc4nZHZcvNo5Y%2Fauthgear-groups-create.png?alt=media&#x26;token=52ccdc75-e007-4ce9-bde8-facfdacda0d5" alt=""><figcaption></figcaption></figure>

Enter the **Name**, **Group Key** and **Description** in the group creation form then click the **Create** button to finish.

### 3.2 Creating Groups using the Admin API

It's possible to create a new group using the Admin API GraphQL. The following guide shows the steps for doing that.

#### Step 1

Implement your own custom code that has [access to the Admin API endpoint](https://docs.authgear.com/reference/apis/admin-api/authentication-and-security). Alternatively, you can use the GraphiQL Explorer in Authgear Portal (Navigate to **Advanced** > **Admin API** > **GraphiQL Explorer**).

#### Step 2

Execute the following GraphQL mutation to create a new group:

{% tabs %}
{% tab title="Query" %}

```graphql
mutation {
  createGroup(input: { key:"managers", name:"Managers", description: "Group for managers with access to all departments" }) {
    group {
      id
      key
    }
  }
}
```

{% endtab %}

{% tab title="Response" %}

```json
{
  "data": {
    "createGroup": {
      "group": {
        "id": "R3JvdXA6NDM2ZDg4OTctMmNjYi00ZjQ3LWJiOGYtN2U2MzE4ZTVhNDM3",
        "key": "managers"
      }
    }
  }
}
```

{% endtab %}
{% endtabs %}

## 4.0 Managing Existing Roles

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.

### 4.1 Add Role to a User

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.

<figure><img src="https://2638622528-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAjXpMovvVxeIY33s_K%2Fuploads%2FzWYf4dCxOCQuVe5u9z59%2Fauthgear-users-w-roles.png?alt=media&#x26;token=7f4a51bc-098b-4b31-9e3b-2b678ac9afa4" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://2638622528-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAjXpMovvVxeIY33s_K%2Fuploads%2FxdsGQepxliXZUSbCuZdr%2Fauthgear-add-roles-to-user.png?alt=media&#x26;token=1e997206-f588-48c2-ac7e-0ac116adc9c0" alt=""><figcaption></figcaption></figure>

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](#id-2.0-how-to-create-a-new-role).

<figure><img src="https://2638622528-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAjXpMovvVxeIY33s_K%2Fuploads%2FlFU3w0ueNa7Nlec06rIm%2Fauthgear-select-role-for-user.png?alt=media&#x26;token=a53c43c9-9207-4749-a788-c1c5515d70ed" alt=""><figcaption></figcaption></figure>

To remove an existing role from a user, click on the **Remove** button next to the role in the user's **Roles** tab.

### 4.2 Add Group to Role

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.

<figure><img src="https://2638622528-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAjXpMovvVxeIY33s_K%2Fuploads%2F5qRvFXpMGEN9glR5qfXW%2Fauthgear-role-group-tab.png?alt=media&#x26;token=158f73cd-8257-4342-920e-de80a8190b0b" alt=""><figcaption></figcaption></figure>

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.

<figure><img src="https://2638622528-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAjXpMovvVxeIY33s_K%2Fuploads%2FFuUeo6gZdeJt9Gppd2JX%2Fauthgear-add-group-to-role.png?alt=media&#x26;token=47593686-24c9-4afd-a80e-8700908e17e3" alt=""><figcaption></figcaption></figure>

### 4.3 Update or Delete a Role

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.

<figure><img src="https://2638622528-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAjXpMovvVxeIY33s_K%2Fuploads%2F6eV2XkJGbNMUJHSr2jt1%2Fauthgear-update-role.png?alt=media&#x26;token=f62be1ef-d458-484d-b0c0-96e11de8ff16" alt=""><figcaption></figcaption></figure>

To delete a role, click on the **Delete** button once you open the role's **Settings** tab.

## 5.0 Managing Existing Rules

### 5.1 Add Group to User

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**.

<figure><img src="https://2638622528-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAjXpMovvVxeIY33s_K%2Fuploads%2Fd8nEafQSAsW0Ay9pJcpV%2Fauthgear-add-group-to-user.png?alt=media&#x26;token=688993f3-5e8e-4598-a853-bfc7e95b9bb7" alt=""><figcaption></figcaption></figure>

Select the group you wish to add the user to from the drop-down then click on **Add**.

<figure><img src="https://2638622528-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAjXpMovvVxeIY33s_K%2Fuploads%2FhQc9onv8FNL5a1oEfl5d%2Fauthgear-add-user-to-group-select-group.png?alt=media&#x26;token=338b663e-f13b-4f47-ba83-dd1ba5ee4223" alt=""><figcaption></figcaption></figure>

### 5.2 Add Role to Group

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.

<figure><img src="https://2638622528-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAjXpMovvVxeIY33s_K%2Fuploads%2F6a0WXHSA4VfHJO1rD77C%2Fauthgear-group-add-role.png?alt=media&#x26;token=f80e1546-e079-4d79-b10c-2a4c599922f1" alt=""><figcaption></figcaption></figure>

Select the roles you wish to add to the group from the drop-down then click on the **Add** button.

<figure><img src="https://2638622528-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAjXpMovvVxeIY33s_K%2Fuploads%2FXK9ysPvazza9mExoKtGB%2Fauthgear-group-select-role.png?alt=media&#x26;token=5cbd56cb-9024-4fef-ad86-76671391f6a2" alt=""><figcaption></figcaption></figure>

### 5.3 Update or Delete Group

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.

<figure><img src="https://2638622528-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAjXpMovvVxeIY33s_K%2Fuploads%2FLPmDmLlIOvqi75nfSm45%2Fauthgear-update-group.png?alt=media&#x26;token=13bd1866-d0ee-4158-915f-d44d53d49184" alt=""><figcaption></figcaption></figure>

To delete an existing group, click on the Delete Group button once you're in the Settings tab for that group.
