External Web Applications

External Web applications let users log in to Now and call APIs with their own permissions. This avoids distributing an Account API token to browsers.

Configure the Origin and callback

An owner registers the application's Origin in Allowed Browser Origins at /admin/account. An Origin contains the scheme, host, and port, such as https://app.example.com, without a path. Use HTTPS; HTTP is allowed for localhost development.

The callback must load the same application on the same Origin. Do not add a fragment or the reserved query names code, state, error, error_description, or error_uri. Serve it with Cache-Control: no-store and Referrer-Policy: no-referrer. Initialize the SDK before analytics that records the current URL.

Log in and read the current User

The following initializes an application using a compatible SDK. Provide #login and #profile buttons and a #status element for results. Initialize once per application lifetime.

import { BrowserSessionClient } from '@obniz/obniz-now-sdk/browser';

const login = document.querySelector<HTMLButtonElement>('#login')!;
const profile = document.querySelector<HTMLButtonElement>('#profile')!;
const status = document.querySelector<HTMLElement>('#status')!;
login.disabled = profile.disabled = true;

function showError(_error: unknown) {
  status.textContent = 'The result could not be confirmed. Check the current state.';
}

async function initialize() {
  const client = await BrowserSessionClient.create({
    baseUrl: 'https://now.example.com', // Replace with your Now environment
    onAuthStateChanged(state) {
      login.disabled = state !== 'signed_out' && state !== 'login_required';
      profile.disabled = state !== 'active';
    },
    onError: showError,
  });
  login.onclick = () => {
    void client.startLogin(`${location.origin}/callback`).catch(showError);
  };
  profile.onclick = () => {
    void client.api.users.getCurrentUser().then((user) => {
      status.textContent = user.id;
    }).catch(showError);
  };
  return client;
}
void initialize().catch(showError);

create handles the callback URL and restores the session. Do not complete the same callback again elsewhere. In your application, also display errors to users and keep tokens and authorization URLs out of logs.

Finish logout before switching Accounts

Tabs sharing browser storage also share the session and selected Account when the application Origin and Now base URL match. To select another Account, call await client.logout() and start login only after it returns signed_out. A result of logging_out means completion is still pending.

In login_required, ask the user to log in again and wait for their action. Do not erase SDK storage or loop through automatic login attempts. dispose() releases resources; it does not log out.

Create an Account

Services authorized for registration can use startSignup(redirectUri). Service registration permission is separate from an existing Account's Allowed Browser Origins. Adding an Origin to an Account does not enable signup.

If creation succeeds but the application callback is lost, finish logout, log in again, and select the Account already created. Read preparing an Account before repeating signup.

Handle uncertain results

The SDK does not automatically resend ordinary business API requests. result_unknown means failure has not been established. Check the result of a creation or update before offering a retry. Supported browsers need a secure context, IndexedDB, Web Locks, Web Crypto, and related APIs. Initialization fails if required storage is unavailable.