Authentication

User accounts that live in your database

Each project has its own user directory. Sign users up with email and password, keep sessions fresh automatically, and use the signed-in user directly in SQL with auth.uid().

const { data, error } = await stitchdb.auth.signUp({
  email: '[email protected]',
  password: 'a-strong-password',
})

await stitchdb.auth.signInWithPassword({ email, password })

stitchdb.auth.onAuthStateChange((event, session) => {
  if (event === 'SIGNED_IN') console.log('Hello', session.user.email)
})

What you get

Sign-up, sessions and authorization

Email and password

Sign-up, sign-in, sign-out and password updates through the SDK or the REST endpoint at /auth/v1/.

Sessions handled for you

Access tokens are valid for one hour; the SDK stores the session and refreshes it automatically.

Authorization in SQL

Policies use auth.uid() and auth.jwt() so the same rules protect the API, Realtime and Storage.

How it works

Get started with Authentication

  1. Add sign-up to your app

    Call auth.signUp and auth.signInWithPassword from the browser with the anon key.

  2. Write a policy

    using (user_id = auth.uid()) limits every query to the signed-in user's rows.

  3. Manage users

    See, create and delete users in Studio → Authentication, or on a server with the service_role key.

SQL
alter table public.todos enable row level security;

create policy "Users manage their own todos" on public.todos
  for all to authenticated
  using (user_id = auth.uid())
  with check (user_id = auth.uid());

Example

See it in code

const { data: { user } } = await stitchdb.auth.getUser()

await stitchdb.auth.signOut()

Start building on StitchDB today

Create your first project in under a minute.