create-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
.
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
app/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 tokensignIn()
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:
useSession()
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.
app/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.