@sinequa/atomic

Username and password

A sign-in form wired to login(), including what to do with a refused password and why the form owns the flow.

The mode where your interface does the work: nothing can be signed in automatically, so a form collects the credentials and hands them to login().

the call
await client.auth.login({ username, password });

Declaring the mode skips a request

initializeConfig() makes no pre-login request when authMode is already credentials — a form needs nothing from the server. It also clears any provider or token configuration, since none of it applies.

A complete sign-in form

Sign in, with the three failure cases handled
src/sign-in.ts
import { AuthMode, UnauthorizedError, createAtomicClient } from "@sinequa/atomic";

const client = createAtomicClient({
  app: "<your-app>",
  backendUrl: "https://<your-sinequa-server>",
  authMode: AuthMode.credentials(),
});

await client.initializeConfig();

// A session may already exist — from another tab, or a page reload. login() with no
// arguments reports that truthfully, so do not render the form before asking.
if (await client.auth.login()) {
  start();
} else {
  renderForm();
}

function renderForm() {
  const form = document.querySelector("form")!;
  const error = document.querySelector("#error")!;

  form.addEventListener("submit", async (event) => {
    event.preventDefault();
    error.textContent = "";

    const data = new FormData(form);
    const username = String(data.get("username") ?? "");
    const password = String(data.get("password") ?? "");

    try {
      await client.auth.login({ username, password });
      start();
    } catch (cause) {
      // A refused password throws — it does not return false.
      error.textContent =
        cause instanceof UnauthorizedError
          ? "Wrong username or password."
          : "Could not reach the server.";
    }
  });
}

function start() {
  console.log("signed in");
}

The three outcomes

Prop

Type

What login() does behind the form

Worth knowing for two reasons — one benign, one that has caused a bug:

  • It records that this sign-in was a form, in localStorage. That marker is later used to resolve an ambiguous mode; see Auto-detection.
  • It temporarily disables impersonation for the duration of the exchange, and restores it in a finally — so a failed sign-in no longer leaves user override permanently switched off, which it once did.

Pitfalls

What's next

On this page