Integrate Nile Auth with Express.js applications
Create a new app
Install Dependencies
Obtain Database Credentials
.env
file so they can be used in the application to connect to the Nile auth service.
Implement server
server.mjs
and add the following code:Run the server
Obtain user credentials
get_cookies.sh
and add the following code:Create a Tenant
Extract the tenant ID from the request params
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
todos
and populate it with some example tasks.Add route
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
server.mjs
file, just after the app.get("/api/tenants/:tenantId/todos", async (req, res) => {
line:Run the server
Test the route