Express.js
Integrate Nile Auth with Express.js applications
Express.js Integration
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.
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
server.mjs
Obtain user credentials
Nile auth uses cookies to store session information. To obtain them via cURL, create the following file
get_cookies.sh
Set the permissions to be executable
Run the command with the required params
You can then curl the API with the cookies
Advanced usage
Store the tenant ID as a param in your URL so it is easily accessible. You are then able to set your tenant ID for the request any time the param is present.
server.mjs
Custom endpoints
Define the API endpoints for managing tasks. These endpoints use Nile’s tenant-aware context to interact with the database.
Create Tenant-Aware Todos Table
Add route
Add a route that takes a tenant Id and queries the database. if app.param
is set, the query will automatically be tenant-aware.
server.mjs
Protecting Routes
Routes can be protected by calling nile.api.auth.getSession()
.
Related Topics
Was this page helpful?