Teams
For: an app developer whose organizations need named groups of members, such as “Design” or “Support”.
What it does: core keeps teams inside each organization. A team groups members and has no role of its own.
Before you start
Section titled “Before you start”- A working setup of organizations.
- The roles of your organizations. Roles and permissions gives the team actions.
- Add
organizations.teamsto the config. - Set
defaultTeamto give each new organization one team with its creator. - Set
keepLastTeamwhen an organization must always hold a team. - Set the limits
teamsPerOrganizationandmembersPerTeamfor the counts that your plan sells.
import { createAuth, type MailPort, type StorageAdapter } from "@authtempo/core";
/** * Teams inside each organization. A team has no role of its own: every right * comes from the organization role. The two limits are counts that core keeps. */export function createTeamsAuth(deps: { storage: StorageAdapter; mail: MailPort; secret: string }) { return createAuth({ baseURL: "https://api.example.com", appURL: "https://app.example.com", trustedOrigins: ["https://app.example.com"], secret: deps.secret, storage: deps.storage, mail: deps.mail, organizations: { teams: { // The create of an organization also creates one team with the creator in it. defaultTeam: true, // The delete of the last team answers last_team. keepLastTeam: true, }, limits: { teamsPerOrganization: 20, membersPerTeam: 100 }, }, });}- Call
client.organizations.teams.create({ organizationId, name }). - Call
client.organizations.teams.members.add({ teamId, userId })for a member of the organization. - Call
client.organizations.teams.setActive({ teamId })when the person picks a team.
import { describe, expect, it } from "vitest";import { call, harness, signIn } from "../support/harness.ts";import { createTeamsAuth } from "./teams.ts";
describe("teams", () => { it("a member removal leaves no team row", async () => { const h = harness(); const auth = createTeamsAuth(h); const ada = await signIn(auth, h, "ada@example.com"); const bo = await signIn(auth, h, "bo@example.com"); const boUser = ( (await (await call(auth, "GET", "/auth/session", undefined, { cookie: bo })).json()) as { user: { id: string }; } ).user; const created = await call( auth, "POST", "/auth/organizations/create", { slug: "acme", name: "Acme" }, { cookie: ada }, ); const { organization } = (await created.json()) as { organization: { id: string } }; const organizationId = organization.id; await call( auth, "POST", "/auth/organizations/members/add", { organizationId, userId: boUser.id, role: "member" }, { cookie: ada }, );
// defaultTeam made the team "Acme" with Ada in it. Ada makes a second team and adds Bo to both. const team = await call( auth, "POST", "/auth/organizations/teams/create", { organizationId, name: "Design" }, { cookie: ada }, ); expect(team.status).toBe(200); const teams = ( (await ( await call( auth, "GET", `/auth/organizations/teams?organizationId=${organizationId}`, undefined, { cookie: ada, }, ) ).json()) as { teams: { id: string; name: string }[] } ).teams; expect(teams.map((t) => t.name)).toEqual(["Acme", "Design"]); for (const t of teams) { const add = await call( auth, "POST", "/auth/organizations/teams/members/add", { teamId: t.id, userId: boUser.id }, { cookie: ada }, ); expect(add.status).toBe(200); } expect( await h.storage.findMany("teamMember", [{ field: "userId", op: "eq", value: boUser.id }]), ).toHaveLength(2);
// The removal from the organization deletes Bo's team rows in the same write. const removed = await call( auth, "POST", "/auth/organizations/members/remove", { organizationId, userId: boUser.id }, { cookie: ada }, ); expect(removed.status).toBe(200); expect( await h.storage.findMany("teamMember", [{ field: "userId", op: "eq", value: boUser.id }]), ).toEqual([]); const mine = await call(auth, "GET", "/auth/organizations/teams/mine", undefined, { cookie: bo, }); expect(await mine.json()).toEqual({ teams: [] }); });});How it behaves
Section titled “How it behaves”No team role
Section titled “No team role”A team has no role. Every right comes from the organization role. A team member reads and writes only what the member’s organization role allows.
| Write | The action it needs |
|---|---|
| Create, update, delete a team | team:create, team:update, team:delete |
| Add a person to a team | member:update |
| Remove another person from a team | member:delete |
| List the members of a team | member:read |
Invite with teamIds |
member:update, beside the invite rule |
- To add or remove another person, the caller also ranks at least as high as that person and holds each of their actions.
- A member may leave a team with their own
userId. - The five team writes need a verified address. The reads and
setActivedo not. - A team name follows the organization name rule. Two teams may have the same name.
The cascades
Section titled “The cascades”- A member removal and a leave delete the person’s team rows in the same write. The person’s sessions lose their active team.
- A revoke of the platform role that deletes the member row deletes the person’s team rows of the platform organization too, with no team hook. A revoke that keeps other role parts keeps them.
createAdminmakes no default team: the platform organization is not an app organization.- A team delete deletes its team rows. Each session in the team loses its active team, and each pending invitation loses the team.
- An organization delete deletes its teams.
- An accept adds the person to each team of the invitation’s
teamIdsthat still exists. - A change of the active organization sets the active team to
null.
The limits
Section titled “The limits”teamsPerOrganization: a team create at the limit answersteam_limit.membersPerTeam: a team member add at the limit answersteam_member_limit. An accept to a full team answers the same, and the invitation stays pending.keepLastTeam: the delete of the last team answerslast_team.- Each count runs in the write’s transaction, after a lock of the parent row. On Postgres, two writes at one moment cannot both pass a count.
- D1 transactions are not isolated. On D1, two writes at one moment can both pass a count.
The hooks
Section titled “The hooks”The hook points list the team hooks of hooks.organization. A cascade fires no team hook. The default team and the team rows of an accept fire none either.
The active team
Section titled “The active team”POST /organizations/teams/active sets session.activeTeamId. The team must belong to the active organization, and the person must be in it. The value is a choice, not a right. Check the organization role on each request.
If it fails
Section titled “If it fails”forbidden_rank: the caller lacks the team or member action, or ranks below the person.email_unverified: a team write needs a verified address.team_limit: the organization holdsteamsPerOrganizationteams.team_member_limit: the team holdsmembersPerTeammembers.last_team:keepLastTeamkeeps the last team. Create another team first.already_member: the person is already in the team.conflict: another write changed the person at the same moment. A team delete answers it after 5 rounds lost to a set of the active team. Retry.hook_refusedandhook_failed: a before hook refused the write or threw.invalid_body: a bad name, a missing field, or another key.unauthorized: the route needs a session. A token answers 401.