Build

Sign-in

Let people sign in to the app you build with a six-digit code by email, their Google account or their GitHub account. The sign-in screen is your app's own; these are the steps behind it. Each database has its own users.

Set it up

  1. Choose a databaseOpen Authentication in the Kaiva Cloud console and choose a database. Its users are kept with it.
  2. Name your appOn Settings, give your app a name of up to 40 characters. It appears in every sign-in email: Your sign-in code for your app's name.
  3. Add your addressesAdd your site's address, and the return addresses people come back to after Google or GitHub: matched exactly, https or http://localhost while you build, up to 10.
  4. Turn it onTurn on the ways to sign in your app offers, then turn sign-in on.

Owners, admins and editors can change these settings.

Ways to sign in

Email codeA six-digit code by email. It works once and expires in 10 minutes.
GoogleThrough your own Google OAuth client. Only a verified email address is accepted.
GitHubThrough your own GitHub OAuth app. Only a verified email address is accepted.

For Google and GitHub, the Settings tab shows the callback address to paste: in Google Cloud console under APIs and services, Credentials, your OAuth client (Web application), Authorized redirect URIs; in GitHub under Settings, Developer settings, OAuth apps, your app, Authorization callback URL. The client secret is kept encrypted and never shown again.

Turning one way off stops new sign-ins with it. People already signed in stay signed in.

Use it from your app

Every call starts with the database's sign-in address, shown on the Use from your app tab. It belongs to this database only; its users and tokens never reach another app. The tab shows each call in full, with your own address.

Email codeYour app asks for the address, sends a code, then signs the person in with it.
Google or GitHubYour app sends the person to sign in, and they come back to one of your return addresses with a one-time code that only the same browser can exchange.

A sign-in answers with an access token, a refresh token and the user. Calls from a browser must come from your site or a return address on Settings; other sites are refused.

Sign in with an email code

Every code needs a quick check first, so a sign-in with an email code takes these steps. Each call goes to the database's sign-in address.

  1. Ask for a codeSend POST /email/start with { email }. Without a check, it answers 428 with the error human_check_required and a checkUrl.
  2. Send the person to the checkKeep the address they typed. Add return_to to checkUrl, set to your site's address or one of your return addresses exactly as on Settings, and open it in their browser. You can add a state value too, and it comes back with them.
  3. Come back from the checkOnce they pass the check, they come back to that address with ?kaiva_check= and a value, and your state if you sent one.
  4. Ask again with the checkSend POST /email/start again with the same email and human_check set to the kaiva_check value. The code goes out by email, and the call answers { sent: true, expiresIn: 600 }. A check is good for one code, to that address, for 10 minutes.
  5. Sign in with the codeSend POST /email/verify with { email, code }, the code as text. It answers with the access token, the refresh token and the user. A wrong code answers 401 with invalid_code.
JavaScript
const AUTH = 'YOUR_SIGN_IN_ADDRESS';

// 1. Send a six-digit code to the address the person typed.
//    Every code first needs a quick check: the first call answers
//    428 with checkUrl. Keep the address and send the person there
//    (from your server too, pass checkUrl to the page). They come
//    back with ?kaiva_check=, which you send once, with the same
//    address. The code expires in 10 minutes.
const params = new URLSearchParams(location.search);
const human_check = params.get('kaiva_check') || undefined;
const address = human_check
  ? sessionStorage.getItem('kc_email')
  : email;
const started = await fetch(`${AUTH}/email/start`, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ email: address, human_check }),
});
if (started.status === 428) {
  sessionStorage.setItem('kc_email', address);
  const { checkUrl } = await started.json();
  const check = new URL(checkUrl);
  check.searchParams.set('return_to', 'YOUR_RETURN_ADDRESS');
  location.href = check.href;
}

// 2. Sign them in with the code they received.
const res = await fetch(`${AUTH}/email/verify`, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    email: address,
    code,
  }),
});

// { access_token, refresh_token, expires_in, user }
const session = await res.json();

The code works once and expires in 10 minutes. Your server can call email/start too: pass checkUrl to the page, and send the kaiva_check value back to your server when the person returns.

Keep people signed in

The access token lasts 1 hour. The refresh token lasts 30 days and is replaced each time it is used: keep the new one, since the old one stops working.

JavaScript
const AUTH = 'YOUR_SIGN_IN_ADDRESS';

// Who is signed in.
const me = await fetch(`${AUTH}/user`, {
  headers: { Authorization: `Bearer ${access_token}` },
}).then((r) => r.json());

// A new access token before the old one runs out (it lasts 1 hour), and
// at once when a call answers 401 (the database's signing key changed).
// Keep the new refresh_token: the old one stops working.
const next = await fetch(`${AUTH}/token`, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    grant_type: 'refresh_token',
    refresh_token,
  }),
}).then((r) => r.json());

// Sign out on this device, or on every device with
// { scope: 'global' } and the access token.
await fetch(`${AUTH}/signout`, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ refresh_token }),
});

Send the access token to the Data API as Authorization: Bearer, next to the publishable key. A table rule set to Each user's own rows then limits each person to the rows that carry their id.

Users

The Users tab lists the people who have signed in to your app.

Turn offThey are signed out, and their access token stops working at the Data API at once.
End sign-insTheir refresh tokens stop working. Access tokens already issued keep working until they expire, at most an hour.
DeleteThey are removed with how they sign in and signed out at once, and their access token stops working at the Data API. The same address can sign up again as a new user.

Owners and admins can turn users off and delete them.

Signing key and turning sign-in off

Access tokens are signed with the database's own key. Owners and admins can make a new one on Settings, then choose what happens to the tokens the old key signed: let them expire, at most an hour, or end them now if the key leaked, so every one is refused within a minute.

Turning sign-in off stops sign-ins and token refreshes at once. Access tokens already issued keep working until they expire, at most an hour.