Sign-up, OAuth & Invitations
Multi-tenancy
A user can belong to multiple teams simultaneously, each with a different role.
|
Tip
|
9.6.0 breaking change: the |
|
Warning
|
9.12.0 breaking change: the tokens restheart-accounts issues carry no |
The active team and its role are read by ACL rules through @team; switching teams reissues the JWT without requiring a new login, and the response says the team.
|
Note
|
The membership model — field names, role vocabulary, storage collection — can be customised via the |
Data model
Since 9.12.0 who belongs to a team, and as what, is kept in one place: the team’s members. The user document keeps only the id of its active team, which is the user’s own choice. The DefaultMembershipProvider stores team identifiers as MongoDB ObjectId values:
// teams collection
{
"_id": { "$oid": "64a1b2c3d4e5f6a7b8c9d0e1" },
"name": "Acme Corp",
"createdBy": "alice@example.com",
"createdAt": { "$date": "…" },
"members": [
{ "userId": "alice@example.com", "role": "owner", "joinedAt": { "$date": "…" } }, // membership role
{ "userId": "bob@example.com", "role": "member", "appRoles": ["manager"], "joinedAt": { "$date": "…" } }
]
}
// users collection
{
"_id": "alice@example.com",
"roles": ["user"], // system ACL role (from default-role)
"team": { "_id": { "$oid": "64a1b2c3d4e5f6a7b8c9d0e1" } } // the active team
}
|
Note
|
roles contains the system ACL role (e.g. user), not the membership role.
The membership role (owner/member) is per-team and stored in the team’s members; @team.role reads it from there.
appRoles is optional: the application’s own roles in the team, since 9.19.0. See Application roles: appRoles.
|
A membership exists only in the team’s members: a user listed nowhere in them cannot use the team, and has no active membership while that team is the active one. Before 9.12.0 the user document also carried a teams array and team.role; they are neither read nor written any more, and accountsConfig.migrate-memberships removes them from existing data (see Configuration).
Custom providers can use a different identifier type — see Custom Membership Providers.
Adding a user to a second team
Inviting an existing user to a different team adds them to that team’s members without requiring account reactivation (the user is already active):
POST /auth/invite
Authorization: Bearer <owner-or-admin-token>
Content-Type: application/json
{ "email": "alice@example.com", "role": "member" }
-
If the user already belongs to the caller’s team →
409 Conflict. -
If the user is new → standard invite flow (see Team Invitations).
-
If the user is active in another team → invitation sent; membership added when the user accepts;
201 Created.
List team memberships
GET /auth/teams — requires authentication.
Returns all teams the current user belongs to. The entry whose active field is true is the active team, the one @team resolves to.
GET /auth/teams
Authorization: Bearer <token>
[
{ "id": {"$oid": "64a1b2c3d4e5f6a7b8c9d0e1"}, "name": "Acme Corp", "description": "Main workspace", "role": "owner", "active": true, "appRoles": [] },
{ "id": {"$oid": "64a1b2c3d4e5f6a7b8c9d0e2"}, "name": "Other Corp", "role": "member", "active": false, "appRoles": ["manager"] }
]
The description field is included when the team document has a non-null description. Omitted otherwise.
The id field is serialized as MongoDB extended JSON, preserving the native BSON type.
For ObjectId team identifiers (the default) it is {"$oid": "…"}.
Custom providers that use string or integer team IDs will produce a plain JSON string or number.
Switch active team
POST /auth/switch-team — requires authentication.
Verifies that the user belongs to the requested team, makes it the active team, issues a new JWT and updates the auth cookie.
The new JWT carries the user’s system roles from the database. It carries no team and no team role: ACL rules read them through @team.
The browser (or client) does not need to re-enter credentials.
POST /auth/switch-team
Authorization: Bearer <token>
Content-Type: application/json
{ "teamId": {"$oid": "64a1b2c3d4e5f6a7b8c9d0e2"} }
The teamId value must be in MongoDB extended JSON format, matching the id field returned by GET /auth/teams.
On success (200 OK):
-
The response body carries the new active team as
{_id, role, appRoles}, under the key set byteam-claim-name(defaultteam). -
The reissued JWT is delivered per the optional
deliveryquery parameter —cookie(default;Set-Cookiecarries the new JWT) orbody(since 9.6.0;access_token/token_type/expires_inin the response body plus theAuth-Tokenheader, no cookie).
{ "team": { "_id": {"$oid": "64a1b2c3d4e5f6a7b8c9d0e2"}, "role": "owner", "appRoles": [] } }
Error responses
| Status | Reason |
|---|---|
|
Missing or blank |
|
Not authenticated |
|
User does not belong to the requested team, or account not active |
|
User document not found |
Typical frontend flow
// 1. Load team list on app init
authService.getTeams() // GET /auth/teams
.subscribe(teams => this.teams.set(teams));
// 2. User picks a different team
authService.switchTeam(teamId) // POST /auth/switch-team
.subscribe(res => {
this.activeTeam.set(res.team); // { _id, role, appRoles }
router.navigate(['/time']);
});
After switchTeam, read the new team and role from the switch-team response, or call GET /auth/teams and take the entry with active: true.
GET /token does not help here: it returns the username and the system roles, and no team.
All the caller’s teams: @teams
Since 9.18.0. @team is the active team. @teams is every team the caller belongs to, read from the membership provider at every request.
| variable | value |
|---|---|
|
an array of |
|
the ids of all the caller’s teams |
|
the ids of the teams where the caller has that role, e.g. |
Use it when a client works in several teams at once, like an agent with an API key. The document keeps its team in a field, and the client names the team in the request.
Reads see the documents of all the caller’s teams. Changes are allowed only in the teams the caller owns:
{
"readFilter": { "team": { "$in": "@teams._id" } },
"writeFilter": { "team": { "$in": "@teams.owner" } }
}
writeFilter does not cover an insert. On POST, check the team in the body with the predicate:
in(value=@request.body.team, array=@teams.owner)
A body that is an array is refused by this rule. A caller in no team, or a failure reading the memberships, gives an empty array, which matches nothing. When a user leaves a team, or their role changes, the next request sees it.
The name follows team-claim-name, with an s: with team-claim-name: org the variable is @orgs.
Application roles: appRoles
Since 9.19.0. A membership has one role, owner or member. An application often needs roles of its own inside a team: a project manager, a reviewer, a billing contact. Keep them in appRoles, an array of names, next to role.
restheart-accounts stores the names and gives them to ACL rules. It does not interpret them.
An owner sets them with PATCH /auth/member-role, and an invitation can carry them. See Team Invitations.
| variable | value |
|---|---|
|
the caller’s application roles in the active team, always an array, empty when there are none |
|
the ids of the teams where the caller has that application role, e.g. |
Only a manager of the active team can insert:
in(value='manager', array=@team.appRoles)
Reads see the documents of the teams where the caller is a manager:
{ "readFilter": { "team": { "$in": "@teams.manager" } } }
A change of appRoles is seen by the next request, with the same token.
A name is free, with a few exceptions that keep @teams.<name> unambiguous. It cannot be blank, contain . or start with $. It cannot be _id, nor the ownership-role or the member-role-name. Names are trimmed and keep their case; duplicates are dropped.
GET /auth/teams, POST /auth/switch-team and GET /auth/team/members return appRoles, an empty array when there are none.
ACL considerations
No manual ACL entries are needed for /auth/teams or /auth/switch-team.
Both endpoints register their own allow rules via ACLRegistry at startup.
They are disabled when accountsConfig.membership-endpoints-enabled: false.
See Custom Membership Providers to replace the built-in storage strategy.