GUIDE

How to add sign-in to your app with email codes, Google and GitHub

Updated

Turn on sign-in for a Kaiva Cloud database: email codes, Google and GitHub, behind your app's own sign-in screen. Each person who signs in gets a token your app sends to the Data API, and a table rule set to each user's own rows lets them read and change only the rows that carry their id.

With Kaiva Cloud

Open Authentication in the Kaiva Cloud console and choose a database: its users are kept with it. Name your app (the name appears in every sign-in email), add your site and return addresses, and turn on the ways to sign in: a six-digit code by email that works once and expires in 10 minutes, Google, and GitHub. Google and GitHub use your own OAuth app, whose secret is kept encrypted, and only a verified email address is accepted.

Your app calls the database's sign-in address and gets an access token that lasts an hour and a refresh token that lasts 30 days, replaced each time it is used. The Use from your app tab shows every call with your database's own address.

The steps below are the same setup with another sign-in service.

Step by step

  1. Choose the ways to sign inA code by email works for anyone with an inbox and leaves no password to store or reset. Google and GitHub sign in people who already have those accounts.
  2. Register your app with Google and GitHubCreate an OAuth client in the Google Cloud console and an OAuth app in GitHub's developer settings, each with your app's return address. The client secrets stay on the server.
  3. Connect a sign-in serviceHosted services such as Clerk, Auth0, Firebase Authentication and Supabase Auth, or a library such as Auth.js inside your own app. Each handles the email step and the Google and GitHub redirects, and gives your app tokens for the person who signed in.
  4. Give every table an ownerAdd a column such as user_id to each table that holds people's data, and fill it with the signed-in person's id whenever a row is added.
  5. Check the token on every requestYour server or API verifies the token, then reads and changes only rows whose owner column holds that id. With row level security, Postgres refuses the rest by itself. Then sign in with a second account in a private window: it should see none of the first account's rows.

Each user's own rows, with the Data API

Send the access token to the Data API as Authorization: Bearer, next to the publishable key. Give a table a text or uuid column for the owner and set its rule to Each user's own rows: each person then reads and changes only rows whose owner column holds their id. Rows they add get their id in that column, and a row naming someone else is refused.

Turn a user off or delete them on the Users tab, and their access token stops working at the Data API at once.

Email codes, Google and GitHub

What each way to sign in needs from you, and what the person does.

Way to sign inWhat you set upWhat the person does
Email codeA sign-in service that sends the emailTypes their address, then the code from the email
GoogleAn OAuth client in the Google Cloud console, with your return addressChooses their Google account
GitHubAn OAuth app in GitHub's developer settings, with your return addressApproves your app on GitHub

Tokens, and where they go

After sign-in your app holds a short-lived access token and a longer-lived refresh token. Send the access token as Authorization: Bearer on each request, refresh it before it runs out, and keep both out of URLs, where logs and browser history keep them.

Each person's own rows

A sign-in screen tells your app who someone is; it does not limit what they can read. That check belongs where the data is read: in your server's queries, as below, or in the database with row level security.

Node.js
// On your server, after verifying the token:
const userId = token.sub;

const { rows } = await db.query(
  'select id, title from notes where user_id = $1 order by id desc',
  [userId],
);

Other ways to do it

  • A hosted sign-in serviceClerk, Auth0, Firebase Authentication and Supabase Auth run sign-in for you, some with ready-made screens. Your users' accounts live with the service.
  • A library in your own appAuth.js and similar libraries run inside your server code and keep users in your own database. You choose and set up the email sender yourself.
  • PasswordsMore to build: storage with a slow hash such as Argon2 or bcrypt, resets by email, and limits on guessing. An email code signs in the same people with nothing to store.

Questions

Email codes or passwords?
Email codes leave nothing to store or reset: each code works once and expires. Passwords need hashing, resets and limits on guessing.
Do I need my own Google and GitHub apps?
For your live app, yes: you register it with each, so people see your app's name, and give the client ID and secret to your sign-in service.
Is a sign-in screen enough to keep each person's data to them?
No. It says who someone is. Each request must also be checked against the rows it reads or changes, in your server or with row level security in the database.
How does my app send the token?
As Authorization: Bearer on each request to your API. Never in a URL, where logs and browser history keep it.
Does each database have its own users?
Yes. A database's users and their tokens open only that database's data, never another app's.
Can my app keep its own sign-in screen?
Yes. The sign-in screen is your app's own; Kaiva Cloud sends the codes and handles the Google and GitHub steps and the tokens behind it.