Getting Started
1. Create a new database
Sign up for an invite to Nile if you don’t have one already and choose “Yes, let’s get started”. Follow the prompts to create a new workspace and a database.2. Create todo table
After you created a database, you will land in Nile’s query editor. Since our application requires a table for storing all the “todos” this is a good time to create one:3. Create tables for NextAuth data model
4. Getting credentials
In the left-hand menu, click on “Settings” and then select “Credentials”. Generate credentials and keep them somewhere safe. These give you access to the database.5. Setting the environment
If you haven’t cloned this project yet, now will be an excellent time to do so. Since it uses NextJS, we can usecreate-next-app
for this:
.env.local.example
to .env.local
, and update it with your workspace and database name.
(Your workspace and database name are displayed in the header of the Nile dashboard.)
Also fill in the username and password with the credentials you picked up in the previous step.
Our example includes passwordless email and Github OAuth authentication.
To use either method, you’ll want to fill in the appropriate section of the environment file.
You can refer to NextAuth getting started guides with email
or oauth for more details.
The resulting env fileshould look something like this:
npm install
.
6. Running the app
select * from users.users
in the query editor.
Login with the new user, and you can create a new tenant and add tasks for the tenant. You can see the changes in your Nile database by running
How it works
Setting up NextAuth
NextAuth is a very flexible authentication library that supports a wide range of authentication methods and providers. It is very easy to configure and use. We set it up inapp/api/auth/[...nextauth]/route.js
:
NileAdapter
is a custom adapter that implements the NextAuth adapter interface. It uses the Nile database to store user information and sessions.
This route handles all calls to /api/auth/*
and provides the following endpoints:
/api/auth/signin
- handles sign in requests/api/auth/signout
- handles sign out requests/api/auth/session
- returns the current session/api/auth/providers
- returns a list of configured providers/api/auth/callback/*
- handles callbacks from authentication providers/api/auth/csrf
- returns a CSRF token
Using NextAuth for Login / Logout
NextAuth SDK providessignIn()
method that we call and it handles the login flow for us. We use it in app/pages.tsx
.
app/tenants/page.tsx
we link to the signout endpoint provided by NextAuth:
Using NextAuth for identity and access
NextAuth provides auseSession()
hook that we can use to get the current session.
In order to use it with Nile’s tenant isolation, we refer to it when retrieving a connection to Nile’s tenant databases in /lib/NileServer.ts
This guarantees that all queries executed on behalf of the user will be executed in the context of the tenant the user is currently logged in to.
Nile will also respond with errors if the user is not authorized to access the tenant.
Adding new authentication providers
NextAuth supports a wide range of authentication providers. You can see the full list here. If you want to modify this example to use a different provider, you can do so by first modifying the NextAuth configuration inapp/api/auth/[...nextauth]/route.js
.
Import the provider you want to choose and add a section to the authOptions
object.
Then, you’ll need to modify the UI to use the new provider. For example, if you want to use Google OAuth, you’ll need to add a button to the UI that calls signIn("google")
.
Thats it. NextAuth will handle the rest and you will have a new authentication method for your multi-tenant application.