Express.js
Integrate Nile Auth with Express.js applications
Learn how to integrate Nile Auth with your Express.js application. The integration allows the application to interact with Nile’s APIs and databases, providing tenant-aware data management.
This guide provides an overview of how to use Nile-Auth core functionality with an Express.js application. We’ll cover the following topics:
- Authentication, cookies, sessions
- Tenant isolation
- Securing endpoints
Authentication
Create a new app
Install Dependencies
Obtain Database Credentials
- If you haven’t signed up for Nile yet, sign up here and follow the steps to create a database.
- Navigate to Database Settings in your database’s UI at console.thenile.dev.
- Go to Connection settings.
- Select the CLI icon, and click Generate credentials
- Copy the required credentials and store them in an
.env
file so they can be used in the application to connect to the Nile auth service.
Implement server
Create a new file called server.mjs
and add the following code:
Run the server
Obtain user credentials
Nile auth uses cookies to store session information. To obtain them via cURL, create a file called get_cookies.sh
and add the following code:
Set the permissions to be executable
Run the command with the required params
You can then curl the API with the cookies
Tenant Isolation and Secure Endpoints
Since Nile-Auth is designed to work with B2B applications, it is important to understand how to work with tenants, their access to data, and how to secure endpoints.
We are going to extend the previous example with new functionality. We’ll add a new table to the database, and a new endpoint that queries the data, making sure the endpoint is both secure and isolated to the tenant.
Create a Tenant
You do not need a new endpoint in order to extand your application with tenant functionality. Nile’s SDK includes generated routes for managing tenants. We just need to call them:
Extract the tenant ID from the request params
There are multiple ways to pass the current tenant to the web app on each request. You can pass it as a param, a header, or a cookie. In this example we’ll pass it as a param.
Add the following code to your server.mjs
file, just after the app.use(express.urlencoded({ extended: true }));
line.
This will extract the tenant ID from the request params, and configure the nile client to use it as
the current tenant before handling any request.
server.mjs
Create a Tenant-Aware Todos Table
In Nile console or another database client of your choice, run the following SQL to create a new table called todos
and populate it with some example tasks.
Add route
Add a route that takes a tenant Id and queries the database. if app.param
is set
(as we did in the previous step), the query will automatically be isolated to the
current tenant. See how it returns data onlyfor the tenant we requested even
if there are multiple tenants in the database and even though the query does not include a tenant_id filter.
Add the following code to your server.mjs
file, just after the app.delete(paths.delete, handler);
line:
server.mjs
Securing Routes
The route we created is isolated to a specific tenant, however at this point, any user can call it. It is not secure.
Lets protect it by checking if the user is authenticated. Add the following code to your server.mjs
file, just after the app.get("/api/tenants/:tenantId/todos", async (req, res) => {
line:
You can add this logic in a middleware function, so it will be applied to all routes that need to be protected.
Run the server
If you haven’t already, run the server:
Test the route
First, lets try to access the route without authentication. Make sure you replace the tenantId with the one you created in the previous step.
Now, lets try to access the route with authentication. Make sure you replace the tenantId with the one you created in the previous step.
Related Topics
Was this page helpful?