Invite people
Invite a person by email with a role, and follow the invitation until it is accepted.
What it shows
- An administrator invites an email address in the portal, and selects a role for it.
- The person receives a message with a link, and chooses a name and a password. The link proves the address, so the account starts with a verified email.
- Your backend asks the Admin API whether the address has an account. Before the person accepts, it has none.
- After the person accepts, the account exists and holds the role of the invitation.
Before you start
- A role to assign. The setup script creates the role "Support agent".
- An API key with the scope
admin:users:read, for the program. jq, and for the CLI tab thelumoCLI.
| Variable | Value |
|---|---|
LUMOAUTH_URL | The address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region. |
LUMOAUTH_ORG_ID | Your organization, for example acme-corp. |
LUMOAUTH_API_KEY | An API key with the scope admin:users:read. The setup script also needs admin:roles:write. |
INVITED_EMAIL | The address that you invite. Use one whose mail you can read. |
The setup script creates what the recipe needs, through the Admin API. You can run it again at any time.
node accounts/invitations/setup.mjs
setup.mjs
// Creates the role that the invitation assigns: "Support agent". Run it
// again at any time.
//
// node accounts/invitations/setup.mjs
//
// The API key needs the scope admin:roles:write.
import { ensure } from '../../_lib/admin.mjs';
const role = await ensure('/admin/roles', (r) => r.slug === 'support-agent', {
name: 'Support agent',
slug: 'support-agent',
description: 'Answers the questions of customers.',
});
console.log(`The role "${role.name}" (${role.slug}) is ready.`);
Send the invitation in the portal. The Admin API and the CLI have no call that invites a person to the organization.
- Open Users, and click Invite users.
- Enter the address in Email addresses. You can enter several, one on each line.
- Write a Personal message if you want one in the email.
- Under Roles, select Support agent.
- Click Send invitations. The portal shows the Invitations list, with the invitation under Pending.
Run the program now, and again after the person has clicked Accept invitation in the message and created the account.
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 invitation itself is sent in the portal: neither the Admin API nor the CLI has a call for it. The portal steps were followed on a live organization.
- lumo CLI
- curl
accounts/invitations/cli/follow.sh
#!/bin/sh
# Follow an invitation: ask whether the invited person has an account yet,
# and which roles the account holds, with the lumo CLI.
#
# sh accounts/invitations/cli/follow.sh
#
# The script needs the lumo CLI and jq.
set -eu
# The CLI reads its own three variables. With them it needs no `lumo login`.
export LUMO_BASE_URL="$LUMOAUTH_URL" LUMO_ORG="$LUMOAUTH_ORG_ID" LUMO_API_KEY="$LUMOAUTH_API_KEY"
EMAIL="${INVITED_EMAIL:-maya.chen@example.com}"
# An invitation creates no account. The account exists from the moment at
# which the person accepts, so search for the address.
USER_ID=$(lumo users list --search "$EMAIL" -o json |
jq -r --arg email "$EMAIL" '[.data[] | select(.email == $email) | .id][0] // empty')
if [ -z "$USER_ID" ]; then
echo "$EMAIL: no account yet"
exit 0
fi
# The roles of the invitation are on the account at once.
lumo users roles get "$USER_ID" -o json |
jq -r --arg email "$EMAIL" '"\($email): joined, roles: \([.data[].slug] | join(", "))"'
sh accounts/invitations/cli/follow.sh
accounts/invitations/http/follow.sh
#!/bin/sh
# Follow an invitation: ask whether the invited person has an account yet,
# and which roles the account holds, with curl.
#
# sh accounts/invitations/http/follow.sh
#
# The script needs curl and jq.
set -eu
API="$LUMOAUTH_URL/orgs/$LUMOAUTH_ORG_ID/api/v1"
EMAIL="${INVITED_EMAIL:-maya.chen@example.com}"
# An invitation creates no account. The account exists from the moment at
# which the person accepts, so search for the address.
USER_ID=$(curl --silent --show-error --fail-with-body --get "$API/admin/users" \
--header "X-API-Key: $LUMOAUTH_API_KEY" --data-urlencode "search=$EMAIL" |
jq -r --arg email "$EMAIL" '[.data[] | select(.email == $email) | .id][0] // empty')
if [ -z "$USER_ID" ]; then
echo "$EMAIL: no account yet"
exit 0
fi
# The roles of the invitation are on the account at once.
curl --silent --show-error --fail-with-body "$API/admin/users/$USER_ID/roles" \
--header "X-API-Key: $LUMOAUTH_API_KEY" |
jq -r --arg email "$EMAIL" '"\($email): joined, roles: \([.data[].slug] | join(", "))"'
sh accounts/invitations/http/follow.sh
Check the result
Output
maya.chen@example.com: no account yet
maya.chen@example.com: joined, roles: support-agent
- The first line is the answer before the person accepts, and the second line is the answer after.
- In the portal, the invitation moves from Pending to Accepted on the Invitations list.
- The audit log has the entries
user.invitation.acceptedanduser.created. - LumoAuth sends the webhook event
user.createdwhen the account is created. To receive it, see Receive webhooks.
Limits
- An invitation assigns roles only. Add the person to groups after they accept.
- You can select only roles whose permissions you hold yourself.
- The link is valid for 7 days. A new invitation to the same address replaces the earlier one.
- A role that opens the portal needs a free administrator seat. See the invitations page of the docs.
- This recipe invites a person to your organization. To invite a member of a customer organization, see One organization for each customer.