Navigation
This documentation refers to the MongoDB Charts service in MongoDB Atlas. Read the on-premises documentation to learn how to use the MongoDB Charts on site.

Embed a Chart Authenticated with Realm

Note

For the purposes of this tutorial, the names Realm and Stitch are used interchangably. They both refer to the MongoDB platform as a service.

MongoDB Realm is a serverless platform that enables developers to quickly build applications without having to set up server infrastructure. Realm offers several authentication options, including Google OAuth, Facebook Login, and email/password. You can use the authentication mechanism from a Realm app to control data access for an embedded chart.

This tutorial shows you how to:

  • Enable authenticated embedding for a chart.
  • Use the Embedding SDK to embed a chart authenticated by a custom Realm provider.
  • Use your Realm app to control the data in your embedded chart.

Note

This tutorial does not cover creating a Realm app with authentication. See the Realm documentation for app creation tutorials.

Prerequisites

  • You must be an Atlas Project Owner to configure embedding authentication providers for your linked Charts instance.
  • You must have a chart which uses your Realm app’s backing database as its data source. For more information about creating a chart, see Build Charts.

Procedures

Enable Authenticated Embedding for a Chart

Enable authenticated embedding to generate a chart ID and Charts Base URL. You will need your chart ID and Charts Base URL to display your chart on a web page.

1

Select a dashboard.

From your dashboard page, select the dashboard containing the chart you wish to embed.

2

Select a chart.

From the dashboard, click ellipsis icon at the top-right of the chart to access its embedding information. Select Embed Chart from the dropdown menu.

3

Enable external sharing on the data source

If you have already enabled external sharing on the data source this chart uses, skip this step. If you haven’t yet enabled embedding on the data source, you can do so now. Click the Configure external sharing link.

4

Select the Authenticated tab in the dialog window.

Embed authenticated chart
5

Toggle Enable authenticated access to On.

6

(Optional) Specify a filter function to inject per user.

You can specify a function to inject a MongoDB filter document for each user who views the chart. The function has access to your Embedding Authentication Provider’s token via context.token, and can filter the chart data based on the token.

This filter ensures that viewers of an embedded chart see only their own data, which is useful when embedding charts with potentially sensitive information.

Example

The following filter function only renders data where the ownerId field of a document matches the value of the Embedding Authentication Provider’s token’s sub field:

function getFilter(context) {
  return { ownerId: context.token.sub };
}
7

(Optional) Specify filterable fields for your chart.

Specify the fields on which chart viewers can filter data. By default, no fields are whitelisted, meaning the chart cannot be filtered until you explicitly whitelist at least one field.

To learn more about filterable fields, see Specify Filterable Fields.

8

Copy the Chart ID and Chart Base URL

Use these values in your application code together with your Embedded Authentication Provider attributes to embed your chart.

Configure Charts to use your Custom Realm Provider

1
2

From the Embedding Authentication Providers section, click Add New Provider.

3

In the Name field, enter a descriptive name for the provider.

4

From the Provider list, select Realm.

5

From the Realm Project list, select the Cloud project that contains your Realm app.

6

From the Realm App list, select the Realm app to use for authentication.

7

Set toggle switch for using Realm rules for data access.

In addition to authenticating users for embedded chart access, you can perform fine-grained data access control with your Realm app’s service rules. Set the toggle switch to On if you want to enable rule enforcement.

When this option is enabled, any Realm rules defined on collections will be observed by Charts when retrieving chart data. You can use this feature to restrict access to the data shown on your chart, including showing different subsets of data to different users, depending on their role.

Note

  • This option assumes that the data source for this chart uses the same MongoDB database and collection as your Realm app.
  • Using this option may have a performance impact, so you should leave it off if you do not need to restrict data access based on Realm rules.
8

Enter Realm service name

Enter the name of the service in your Realm app that fetches data from MongoDB. To find the name of your data service:

  1. Navigate to your Realm app.
  2. Click Clusters in the sidebar navigation.
  3. The service name is listed under Stitch Service Name.
9

Click the Save button.

Create a Web App to Display Your Chart

If you already have an app in which to display your chart, you’re ready to add an embedded chart. If not, proceed with the remaining steps to create a new app.

MongoDB offers a pre-built example app that shows how to use the Embedding SDK to display an embedded chart using Realm authentication.

Clone the GitHub repository and follow the instructions in the Readme file to begin using the app. You can run the app as-is, or you can customize it to use the chart you created earlier.

Note

The example app assumes that your Realm app uses email/password authentication. If your Realm app uses another authentication mechanism, the example app will require additional customization.

Customize the Node.js App

All the lines you need to edit are marked with a comment containing ~REPLACE~.

After you finish customizing the app, it’s ready to run.

1

Open the file index.js in a text editor.

The file index.js is located in the src directory.

2

Enter your Realm App ID

Replace the existing Realm App ID with your Realm App ID, which you can find on the left side of your Realm app UI.

const client = Stitch.initializeAppClient(
  'realm-authentication-sample-eibkj', // Optional: ~REPLACE~ with your Realm App ID
{
3

Enter your Charts Base URL

Replace the existing Charts Base URL with the Base URL of the chart you want to display. Your Charts Base URL is visible in the embedding options modal window. See Embed Charts with the Embedding SDK for detailed instructions on enabling embedding for a chart.

const sdk = new ChartsEmbedSDK({
  baseUrl: "https://charts-dev.mongodb.com/charts-test2-pebbp", // Optional: ~REPLACE~ with your Charts URL
  getUserToken: () => getRealmUserToken(client),
});

Note the getRealmUserToken() function in the code snippet above, which returns the JWT from the existing Realm authentication session. getRealmUserToken() must be imported from the Embedding SDK, as you can see in the import statement on line 2 of the example app:

import ChartsEmbedSDK, { getRealmUserToken } from "@mongodb-js/charts-embed-dom";

You can also include the Embedding SDK with inline Javascript in an HTML page, as shown in the following code snippet:

<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.6.0/stitch.js"></script>
<script src="https://unpkg.com/@mongodb-js/charts-embed-dom"></script>

To use the getRealmUserToken() function in this scenario, import it from the SDK:

ChartsEmbedSDK.getRealmUserToken()
4

Enter your chart ID

Replace the existing chart ID with the ID of the chart you want to display. Your chart ID is visible in the embedding options modal window. See Embed Charts with the Embedding SDK for detailed instructions on enabling embedding for a chart.

const chart = sdk.createChart({
  chartId: "a2e775e6-f267-4c2c-a65d-fbf3fad4a4f2", // Optional: ~REPLACE~ with your Chart ID
});