Database
Supabase (Postgres) integration for user data and app storage.
shipsaas uses Supabase as its database backend. Users are automatically synced on first sign-in via Auth.js callbacks in lib/auth.js.
Setup
1. Create Supabase Project
- Go to supabase.com and sign up
- Create a new project
- Wait for the database to be provisioned
- Go to Settings → API
- Copy your Project URL and anon key
2. Configure Environment
.env.local
# Get these from your Supabase project settings
SUPABASE_URL=https://your-project.supabase.co
SUPABASE_KEY=your-supabase-anon-key3. Create Users Table
In the Supabase dashboard, go to SQL Editor and run:
Supabase SQL Editor
CREATE TABLE users (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
full_name TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);Supabase Client
The Supabase client is initialized in lib/supabase.js:
lib/supabase.js
Supabase client initialization with project URL and anon key.
lib/supabase.js
import { createClient } from "@supabase/supabase-js";
const supabase = createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_KEY
);
export default supabase;Usage
Query Examples
Use the Supabase client in your API routes or server components:
import supabase from "@/lib/supabase";
// Fetch a user by email
const { data: user } = await supabase
.from("users")
.select("*")
.eq("email", "user@example.com")
.single();
// Insert a new record
const { data, error } = await supabase
.from("users")
.insert([{ email: "new@example.com", full_name: "Jane Doe" }]);
// Update a record
const { data: updated } = await supabase
.from("users")
.update({ full_name: "Updated Name" })
.eq("id", userId);
// Delete a record
await supabase
.from("users")
.delete()
.eq("id", userId);
// Fetch with filters
const { data: admins } = await supabase
.from("users")
.select("*")
.order("created_at", { ascending: false })
.limit(10);Automatic User Sync
Users are automatically created in Supabase when they first sign in. This is handled by the signIn callback in lib/auth.js:
// lib/auth.js — signIn callback
async signIn({ user }) {
const existingUser = await getUser(user.email);
if (!existingUser) {
await createUser({
email: user.email,
full_name: user.name,
});
}
return true;
}✓ Database configured! Users are automatically synced from Auth.js to Supabase. Next, integrate with authentication to customize the user schema.