Build a private application
How to build a basic private application with CARTO login
Last updated
How to build a basic private application with CARTO login
Last updated
Here you will learn how to create an application that requires a login to access. This application will be only accessible to users inside your CARTO organization (regardless of their role), so you will need a CARTO user to access it.
If you're building an application for an Enterprise organization that requires an integration with your own login system, you'll need to first connect and enable the Single Sign-On (SSO) feature in CARTO. We'll cover that in the next guide: Build a private application using SSO.
The CARTO login uses the popular OAuth 2.0 Authorization Framework protocol, so in this guide you will find an easy process to implement it, including login and logout methods.
After completing this guide you will be familiar with the following concepts:
Scaffolding your application.
Creating a Single Page Application OAuth Client (SPA OAuth Client).
Managing the user login using Auth0 SDK.
Visualizing a dataset.
Executing a query against CARTO APIs.
During this guide, we're using the CARTO Data Warehouse. The process explained here is also compatible with other Warehouses like BigQuery, Snowflake, Redshift, or Postgres. Instead of using connection=carto_dw
, you need to use connection=<your_connection>.
As explained here, we recommend Vite to manage the tooling of your app.
To make this guide faster, we're going to start using the basic CARTO template in Vite that already includes a Basemap and deck.gl.
After that, you should have a project up and running at https://127.0.0.1:5173/
As explained in Key Concepts section we need to create a SPA OAuth Client to obtain a client_id + client_secret that we will use to then obtain an OAuth token after the user successfully authenticates (with user and password or other methods)
Go to Workspace -> Developers -> Credentials -> Create New -> SPA OAuth Client. And complete the form as follows:
Then, go to the .env file, and uncomment VITE_CLIENT_ID with the value of the app that was just created.
CARTO platform uses Auth0 under the hood to manage the authentication and authorization. When you created a SPA application in the previous step, CARTO created a SPA application in Auth0.
Auth0 provides SDKs to manage the OAuth 2.0 workflow for Angular, Vue, React, and Vanilla Javascript. In this guide, we're going to use the Vanilla Javascript SDKs to manage the login with CARTO.
We're going to implement here two things:
Authenticate a user following the standard OAuth 2.0 workflow.
Use the authenticated user to obtain an OAuth Access Token to call CARTO APIs.
Install the Auth0 SDK:
Create a file src/auth.ts:
Replace src/main.ts with:
Add this at the end of src/style.css:
Your application is now ready to authenticate users by sending them to the CARTO login, which will require the user to enter their own credentials, before successfully returning to the application in a classic OAuth 2.0 workflow that returns an OAuth Access Token.
Once the user is logged in an OAuth Access Token will be available, and we just need to tell deck.gl to use this token.
Let's modify src/main.ts:
And prepare src/map.ts to receive the new parameters, including imports:
Then add the following source and layer to the Deck instance at src/map.ts:
Remember that OAuth Access Tokens impersonate the user, so it will inherit all the permissions of the user.
At this point, we're all clear to execute a query against the CARTO SQL API using the OAuth Access Token we've previously obtained.
Create a file src/api.ts:
Modify src/main.ts to call the getAirports
function:
Congratulations! You now have successfully built a private application that will require the user to log in. And after a successful login, you're able to show the user a map layer, and you're able to query an airports dataset right from your application.
The full code of this guide is available on GitHub.
We recommend you to visit Visualization with deck.gl to learn more about the visualizations you can create in deck.gl. There are many examples in our gallery that might be useful to improve your application!