Registration and Login

The most common register, login, and user_token-returning flow in @downcity/services.

AccountsService gives a downcity the shared account infrastructure, but email registration and login are available only when an email accounts provider is mounted and configured.

Before rendering an email form, ask the server which providers are enabled:

const providers = await guest.service("accounts").get("providers");

Render email registration or login only when the response contains an enabled email provider with type: "input". The form fields can come directly from that provider's inputs.

register

const registration = await guest.service("accounts").action("register").invoke({
  email: "[email protected]",
  password: "password123",
});

Complete email verification using the verification message before logging in. Registration does not deliver a User Token.

login

const start = await guest.service("accounts").action("login/start").invoke({
  provider: "email",
  client_type: "cli",
  bureau_id: "demo",
});

await guest.service("accounts").action("login/continue").invoke({
  login_id: start.login_id,
  login_secret: start.login_secret,
  input: {
    email: "[email protected]",
    password: "password123",
  },
});

const status = await guest.service("accounts").action("login/result").invoke({
  login_id: start.login_id,
  login_secret: start.login_secret,
});

// Retain this ID and reuse it if the exchange request must be retried.
const exchange_id = crypto.randomUUID();
if (status.status !== "done") throw new Error("Login is not ready");
const session = await guest.service("accounts").action("login/exchange").invoke({
  login_id: start.login_id,
  login_secret: start.login_secret,
  exchange_id,
});

Why bureau_id is included here

Login requests specify the Bureau context for the User Token that Federation issues after authorization. Registration creates the account and starts email verification.

So it needs to know:

  • which downcity is currently being entered
  • which downcity context the token should be issued for

What happens after login succeeds

Do not keep using the guest client forever. Switch into a normal user-context client:

const user = new City({
  role: "user",
  city_url,
  user_token: session.user_token,
});