|
| 1 | +# MongoDB Charts Embedding Example - Google Authentication |
| 2 | + |
| 3 | +## Background |
| 4 | + |
| 5 | +📄 _[See the MongoDB Charts Embedding Docs for more details](https://docs.mongodb.com/charts/saas/embedding-charts/)_ |
| 6 | + |
| 7 | +🎮 _[Play with a live demo of this sample here](https://codesandbox.io/s/github/mongodb-js/charts-embed-sdk/tree/master/examples/authenticated-google)_ |
| 8 | + |
| 9 | +MongoDB Charts allows you to create visualizations of your MongoDB data using a simple web interface. You can view the visualizations within the Charts UI, or you can use the Embedding feature to render the charts in an external web application. |
| 10 | + |
| 11 | +Charts can be embedded either using a simple IFRAME snippet, or by using the Charts Embedding SDK from your JavaScript code. When using the SDK, embedded charts can be either unauthenticated (meaning anyone who has the embed code can view the chart), or authenticated (whereby the user can only view the chart if they have an active authentication session linked to a Charts authentication provider). |
| 12 | + |
| 13 | +This sample shows how to use the JavaScript Embedding SDK to render **authenticated** embedded charts, specifically via configuring **Google** as an authentication provider. The sample app is already set up to authenticate with a Google Client ID hosted by the Charts Development team. |
| 14 | + |
| 15 | +#### The features included in this demo are as follows: |
| 16 | + |
| 17 | +- 📈 Render an embedded chart on a web page |
| 18 | +- 🔒 Only render charts to valid users |
| 19 | +- 𝗚 Google authentication |
| 20 | + |
| 21 | +## Quickstart |
| 22 | +_The following steps presume the use of npm, though yarn works as well._ |
| 23 | + |
| 24 | +1. Ensure you have Node installed. You can confirm with `node --version`. On some operating systems, Node is available as the `nodejs` binary instead. |
| 25 | + |
| 26 | +2. Clone the Git repository or download the code to your computer. |
| 27 | + |
| 28 | +3. Run `npm install` to install the package dependencies. |
| 29 | + |
| 30 | +4. Run `npm start` to start the application. This will utilise parcel.js |
| 31 | + - Optional Parcel.js documentation https://parceljs.org/ for more information on what this is |
| 32 | + |
| 33 | +This should create a local server running the Charts demo. Open a web browser and navigate to `http://localhost:3000` in the url bar to see the sample. Use Incognito mode to see the full Google login flow rather then signing in with your Chrome Google account. |
| 34 | + |
| 35 | +Logging in with any valid Google account will allow you to render the chart. |
| 36 | + |
| 37 | +## Getting a Google Client ID |
| 38 | + |
| 39 | +This sample is pre-configured with a MongoDB-owned test Client ID. To use Google Sign-In in your own apps, you must create your own Client ID. |
| 40 | + |
| 41 | +Steps on how to configure a Project with Google and attain a Google Client ID can be found [here](https://developers.google.com/identity/sign-in/web/sign-in). You'll need to set the Origin and Redirect URIs to whatever you use in your application. In our sample, that is http://localhost:3000. |
| 42 | + |
| 43 | +### Preparing your Chart for Embedding |
| 44 | +This sample is preconfigured to render a specific chart. You can run the sample as-is, or you can modify it to render your own chart by completing the following steps: |
| 45 | +1. Log onto MongoDB Charts |
| 46 | + |
| 47 | +2. If you haven't done so already, create a chart you would like to embed on any dashboard. |
| 48 | + |
| 49 | +3. Go to the Data Sources tab on the left navigation column. Find the data source that you are using on the chart, and choose External Sharing Options from the ... menu. Make sure that embedding is enabled for this data source and select '**Authenticated Embedding Only**' |
| 50 | + |
| 51 | +4. Go to the Admin Settings tab on the left navigation column. Under 'Embedding Authentication Providers', click 'Add New Provider'. |
| 52 | + |
| 53 | + - Name your Provider e.g `Google` |
| 54 | + - Select the Google Provider. |
| 55 | + - Enter your Google Client ID |
| 56 | + - This should be the entire string `<encodedData>.apps.googleusercontent.com` |
| 57 | + |
| 58 | + |
| 59 | +5. Go to the Dashboards tab on the left navigation column, and select the dashboard that contains the chart you wish to embed. |
| 60 | + |
| 61 | +6. Find the chart you want to embed, click the **...** menu and select **Embed Chart** |
| 62 | + |
| 63 | +7. Ensure the Unauthenticated tab is selected and turn on '**Enable Authenticated access**' |
| 64 | + |
| 65 | +8. Note the Chart ID and the Chart Base URL, as you will need them for running the demo. |
| 66 | + |
| 67 | + |
| 68 | +## Running this Sample with your data |
| 69 | + |
| 70 | +If you do not wish to use our sample data and have completed the above steps to prepare your own chart for embedding, |
| 71 | + - Open the `index.html` file |
| 72 | + - Replace the Charts `baseUrl` string with the base URL you copied from the MongoDB Charts Embedded Chart menu (look for "\~REPLACE\~" in the comments) |
| 73 | + - Replace the `chartId` string with the chart ID you copied from the MongoDB Charts Embedded Chart menu (look for "\~REPLACE\~" in the comments) |
| 74 | + - **Optional** |
| 75 | + - Replace the `<meta content="YourClientIDHere">` with your own Google Client ID. (look for "\~REPLACE\~" in the comments) |
| 76 | + |
| 77 | +## Next Steps |
| 78 | + |
| 79 | +Once you gain an understanding of the API, consider the following |
| 80 | + |
| 81 | +- Take on the optional steps to prepare and manipulate your own data source rather than the sample. |
| 82 | +- Use Incognito Mode to see a pure login experience |
| 83 | +- Consider how you want to secure your data with the Google Embedding Provider. Anybody can create a Google account. Consider using Charts Injected Filters to make your application more secure. |
| 84 | + - For example, consider the following injected filter |
| 85 | + - |
| 86 | + ```javascript |
| 87 | + // Reject tokens that aren't from example.com domain |
| 88 | + if (context.token.hd != 'yourCompany.com') { |
| 89 | + return false; |
| 90 | + } |
| 91 | + return true |
| 92 | + ``` |
| 93 | + -  |
| 94 | + - This will only allow users from your company domain to view the chart data 🔒 |
| 95 | + |
| 96 | +- Think whether an authenticated chart is the feature you're after. If you're simply looking for a way to show off your data, unauthenticated embedding simplifies the workflow even further. |
| 97 | + |
| 98 | +Happy Charting! 🚀📈 |
0 commit comments