Remix
Integrate Nile Auth with Remix applications
Remix API Integration with Nile Database
This guide explains how to integrate Nile Database with Remix and set up routes for handling various HTTP requests (GET
, POST
, PUT
, DELETE
). Additionally, you’ll see how to include client-side components for user authentication and interaction using Nile’s React SDK.
Create a new Remix project
Run the following command in your terminal to create a new Remix project:
Follow the prompts and install your app. After creating the project, navigate into the newly created project directory:
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. - While you are there, click on the
PostgreSQL
icon and also copy your database url for drizzle to use
Update create-react-router output
Now we must update the output from the default create-react-router
for use with Nile. We want to switch to using node-postgres
, and be able to do a top level await
for nile configuration.
Replace /server/app.ts
with the following
Replace database/context.ts
with the following
Update the placeholder table drizzle/0000_short_donald_blake.sql
to generate correctly
Modify vite.config.ts
to allow for top level awaits
Add Nile to the server
Now we need to add the nile instance and route handlers to allow our server to respond to authentication, user, and tenant requests.
Create a file to house the main Nile instance. You can use this file to access nile from a central location any where in the app.
app/nile.ts
Create the API route file at app/routes/nile-api.ts
. This file will handle different HTTP methods (GET, POST, PUT, DELETE) using the Nile SDK.
This code handles different HTTP methods (GET
, POST
, PUT
, DELETE
) for the /api/*
route and delegates the logic to Nile Database.
Update your routes to respond to the api
app/routes.ts
Add Client-Side Code for Authentication
You can use the components from @niledatabase/react
to handle authentication. Replace the boilerplate of the main _index.tsx
file with the following:
This component will render:
- User info and the guest book if the user is signed in.
- Sign-up form if the user is not signed in.
Update the render of home.tsx
to use the following components:
app/routes/home.tsx
Running the Project
To run your project, execute the following:
This will start the development server at http://localhost:3000
, and you can test your API endpoints and authentication components.
Summary
Now you can interact with your Nile Database through Remix API routes and manage authentication in your app!
Creating a loader
In some cases, you may want to create specific action and loader around the API, to do that, use the server side functions in the sdk in your loader. This code loads and updates a user’s profile.
Related Topics
Was this page helpful?