Integrate Auth.js with Tiger Data
Store Auth.js users, accounts, and sessions in Tiger Cloud or self-hosted TimescaleDB using the official PostgreSQL adapter.
Auth.js (formerly NextAuth.js) is an open-source authentication library that handles sign-in, sessions, and account management for web applications across many frameworks and providers.
This page shows you how to integrate Auth.js with Tiger Data using the official PostgreSQL adapter, so that Auth.js persists users, accounts, and sessions in your service or database instead of an external store.
In this integration guide, you:
- Create the tables that Auth.js requires in your service or database.
- Configure the PostgreSQL adapter in your application to connect to Tiger Data.
- Verify that authentication data is persisted.
Prerequisites for this integration guide
To follow these steps, you'll need:
- A Tiger Cloud service, or a running instance of self-hosted TimescaleDB.
- Your connection details.
Create the Auth.js schema in your service or database
Section titled “Create the Auth.js schema in your service or database”The PostgreSQL adapter expects four tables: users, accounts, sessions, and verification_token. Because Tiger Cloud and self-hosted TimescaleDB are built on PostgreSQL, the adapter works against them with no changes.
- Connect to your service or database
Connect to your service or database with your connection details, using
psqlor the SQL editor of your choice. - Create the Auth.js tables
Run the following SQL to create the schema the adapter expects:
CREATE TABLE verification_token (identifier TEXT NOT NULL,expires TIMESTAMPTZ NOT NULL,token TEXT NOT NULL,PRIMARY KEY (identifier, token));CREATE TABLE accounts (id SERIAL,"userId" INTEGER NOT NULL,type VARCHAR(255) NOT NULL,provider VARCHAR(255) NOT NULL,"providerAccountId" VARCHAR(255) NOT NULL,refresh_token TEXT,access_token TEXT,expires_at BIGINT,id_token TEXT,scope TEXT,session_state TEXT,token_type TEXT,PRIMARY KEY (id));CREATE TABLE sessions (id SERIAL,"userId" INTEGER NOT NULL,expires TIMESTAMPTZ NOT NULL,"sessionToken" VARCHAR(255) NOT NULL,PRIMARY KEY (id));CREATE TABLE users (id SERIAL,name VARCHAR(255),email VARCHAR(255),"emailVerified" TIMESTAMPTZ,image TEXT,PRIMARY KEY (id));Run
\dtinpsqlto confirm that all four tables exist.
Configure the adapter in your application
Section titled “Configure the adapter in your application”Point Auth.js at your service or database, then wire the adapter into your Auth.js configuration.
- Install the adapter and the PostgreSQL driver
In your application directory, install the PostgreSQL adapter and the
pgdriver:Terminal window npm install @auth/pg-adapter pg - Set your connection environment variables
Add your connection details to your environment, for example in
.env.local:Terminal window DATABASE_HOST=<host>DATABASE_NAME=<dbname>DATABASE_USER=<user>DATABASE_PASSWORD=<password>NoteAuth.js also requires an
AUTH_SECRETand at least one configured authentication provider. See Auth.js installation for the base setup. - Configure Auth.js to use the adapter
Create
./auth.tsand pass apgconnection pool toPostgresAdapter:import NextAuth from "next-auth"import PostgresAdapter from "@auth/pg-adapter"import { Pool } from "pg"const pool = new Pool({host: process.env.DATABASE_HOST,user: process.env.DATABASE_USER,password: process.env.DATABASE_PASSWORD,database: process.env.DATABASE_NAME,max: 20,idleTimeoutMillis: 30000,connectionTimeoutMillis: 2000,})export const { handlers, auth, signIn, signOut } = NextAuth({adapter: PostgresAdapter(pool),providers: [],})Add your authentication providers to the
providersarray. Auth.js now reads and writes session and account data through your service or database.
Verify the integration
Section titled “Verify the integration”To confirm Auth.js is persisting data in your service or database:
- Sign in through your application
Start your application with
npm run dev, open it in a browser, and complete a sign-in with one of your configured providers. - Query your service or database to confirm the data arrived
Connect to your service or database and query the
userstable:SELECT id, name, email FROM users;You see a row for the account you just signed in with, confirming that Auth.js is writing authentication data to Tiger Data.
You have successfully integrated Auth.js with Tiger Data.
Troubleshooting
Section titled “Troubleshooting”relation "users" does not exist: create the Auth.js schema before starting your application. Run the SQL in Create the Auth.js schema.column "userId" does not exist: the adapter relies on case-sensitive, double-quoted column names. Create the tables with the exact SQL above so the quoted identifiers match.
For other connectivity and authentication issues, see Troubleshoot Tiger Cloud integrations.