How to build a basic private application with CARTO login
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.
Architecture diagram of a private application with Login
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>.
Scaffolding your application
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.
git clone https://github.com/CartoDB/carto-for-developers-guides.git
cp -r carto-for-developers-guides/template private-app
cd private-app
npm install
npm run dev
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.
Managing the user login using Auth0 SDK
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.
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.
Visualizing a dataset
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.
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.
What's next?
The full code of this guide is available on GitHub.
git clone https://github.com/CartoDB/carto-for-developers-guides.git
cd carto-for-developers-guides/private-app
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!