Skip to main content

Manage users from your backend

Create, find, change and block users from your own code, with a scoped API key.

RecipeReadyView source
Works with
JavaScript / TypeScriptPythonlumo CLIPlain HTTP

What it shows​

  1. Your backend creates the user Lena Hoffmann, with no password. When the address exists from an earlier run, it finds that user instead.
  2. It changes the name of the user. A PATCH changes only the fields that it sends.
  3. It blocks the user. LumoAuth ends the sessions of the user and revokes the tokens that were issued to the user.
  4. It lists the blocked users whose email address, name or username contains hoffmann, page by page.

The JavaScript and Python programs use the generated API clients, which are made from the description of the Admin API. The CLI script uses lumo users, and the HTTP script uses curl.

Before you start​

  • An API key with the scopes admin:users:read and admin:users:write. Create it in the portal under Applications & developer tools, API keys.
  • For the CLI script: the lumo CLI and jq. For the HTTP script: curl and jq.
VariableValue
LUMOAUTH_URLThe address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region.
LUMOAUTH_ORG_IDYour organization, for example acme-corp.
LUMOAUTH_API_KEYAn API key with the scopes admin:users:read and admin:users:write.

Code​

Run each command from the recipes folder of the examples repository. Set the folder up once for your language. Every version below was run against a LumoAuth organization. The JavaScript program uses the generated client for every call. In Python, create_user and patch_user of the generated client send no body, so the program uses requests for these two calls.

accounts/users-with-the-admin-api/js/users.mjs
// Create a user, change the name, block the user, and find the user in the
// list again, with the generated client @lumoauth/api-client.
//
// node accounts/users-with-the-admin-api/js/users.mjs
//
// The API key needs the scopes admin:users:read and admin:users:write.
import { AdminUsersApi, Configuration } from '@lumoauth/api-client';

const orgId = process.env.LUMOAUTH_ORG_ID;
const users = new AdminUsersApi(
new Configuration({
basePath: process.env.LUMOAUTH_URL ?? 'https://app.lumoauth.dev',
apiKey: process.env.LUMOAUTH_API_KEY,
}),
);

const email = 'lena.hoffmann@example.com';

// 1. Create the user. Without a password, the person signs in with another
// method or sets a password through a reset link. createUser has no body
// parameter, so the body goes in the request options of the client.
let user;
try {
const created = await users.createUser(
{ orgId },
{ data: { email, name: 'Lena Hoffmann', givenName: 'Lena', familyName: 'Hoffmann' } },
);
user = created.data.data;
} catch (error) {
// 409: a user with this address exists, from an earlier run.
if (error.response?.status !== 409) throw error;
const found = await users.listUsers({ orgId, search: email });
user = found.data.data.find((u) => u.email === email);
}
console.log(`user: ${user.email} (${user.id})`);

// 2. Change the name. PATCH changes only the fields that you send.
const updated = await users.patchUser(
{ orgId, userId: user.id },
{ data: { name: 'Lena Hoffmann-Weber', familyName: 'Hoffmann-Weber' } },
);
console.log(`updated: ${updated.data.data.name}`);

// 3. Block the user. This ends the sessions of the user and revokes the
// tokens that were issued to the user.
await users.blockUser({ orgId, userId: user.id });
console.log(`blocked: ${user.email}`);

// 4. List the blocked users whose email, name or username contains
// "hoffmann", page by page. The search is case-sensitive.
const rows = [];
for (let page = 1; ; page++) {
const { data } = await users.listUsers({ orgId, search: 'hoffmann', blocked: true, page, limit: 25 });
rows.push(...data.data);
if (!data.pagination.hasNextPage) break;
}
console.log(`blocked users matching "hoffmann": ${rows.length}`);
for (const row of rows) console.log(` ${row.email} ${row.name}`);
node accounts/users-with-the-admin-api/js/users.mjs

Check the result​

Output
user: lena.hoffmann@example.com (01a21fe2-6db0-7771-acc8-387816ffa0d4)
updated: Lena Hoffmann-Weber
blocked: lena.hoffmann@example.com
blocked users matching "hoffmann": 1
lena.hoffmann@example.com Lena Hoffmann-Weber
  • Run the program again. The output is the same: the user exists, so step 1 finds it.
  • In the portal, open Users and search for Lena. The row shows the new name and the state Blocked.
  • In the portal, open Audit logs. The entries user.created, user.updated and user.blocked name your API key as the actor.
  • A person who is signed in when you block them loses the session at once. Their refresh token answers invalid_grant, and their access token is refused.

Limits​

  • The Admin API takes a password only as plain text, and checks it against the password policy of the organization. It has no field for a password hash from another system. People from another system sign in with a passwordless method or set a password with Forgot password?. See Move users from another provider.
  • The search is case-sensitive: hoffmann finds the email address, Hoffmann finds the name.
  • A user does not have free-form metadata. A request with a non-empty metadata object is refused.
  • Changing the email address, the active state or the verified state needs a caller who holds every permission of the user.
  • To undo step 3, call POST /admin/users/{userId}/unblock, or run lumo users unblock.