---
title: Workspaces
description: Teams with members, roles and invites.
---

# Workspaces

```ts
createAuth({
  /* … */
  workspaces: { roles: ["owner", "admin", "member"] },
  pages: { acceptInvite: "/invite" },
});
```

- `roles` go from highest to lowest. The first role is the owner. Each workspace has one owner.
- The second role (here `admin`) and up may invite, rename and manage members. Change it with `manage: "member"`.
- `auth.workspaces` is typed with your roles, so `role: "admni"` is a type error.

## The active workspace

Each session has an active workspace. A new session opens in the user's first workspace.

```ts
const current = await auth.workspaces.current();  // { workspace, role } | null
const all = await auth.workspaces.list();          // [{ workspace, role }]
await auth.workspaces.switch({ workspaceId });
```

## Protect a route or a server function

```ts
const { user, workspace, role } = await auth.workspaces.requireMember({ role: "admin" });
```

- Signed out: redirects to `pages.signIn` with a framework adapter. Without one it throws `AuthException("not_signed_in")`.
- Not a member, or below `role`: throws `AuthException` with `forbidden` (or `not_found` for a given `workspaceId`).
- Leave out `role` to allow any member.

Store your app data with a `workspace_id` that points to `auth.workspaces(id)`, and filter by `workspace.id`.

With an API token, `requireMember`, `current`, `list`, `members` and `invites` work, but only in the token's workspace when it has one. Every change (create, invite, roles, delete, …) needs a browser session. The package does not check token scopes: check `session().scopes` yourself.

## Create, rename, delete

```ts
const created = await auth.workspaces.create({ name: "Acme" }); // the creator is the owner; the session switches to it
await auth.workspaces.rename({ name: "Acme Ltd" });
await auth.workspaces.delete();                                 // owner only
```

Every function acts on the active workspace. Pass `workspaceId` to act on another one.

## Members

```ts
await auth.workspaces.members();                              // [{ user, role, joinedAt }], highest role first
await auth.workspaces.setRole({ userId, role: "admin" });
await auth.workspaces.removeMember({ userId });
await auth.workspaces.leave();                                // owner_cannot_leave for the owner
await auth.workspaces.transferOwnership({ userId });          // the old owner gets the second role
```

Nobody can give a role above their own, change the owner's role, or remove the owner.

## Invites

```ts
await auth.workspaces.invite({ email: "ann@example.com", role: "member" });
await auth.workspaces.invites();                 // pending invites
await auth.workspaces.revokeInvite({ email });
```

The e-mail links to `pages.acceptInvite?token=…`, valid for 7 days. A new invite to the same e-mail replaces the old one. Only users with a verified e-mail can invite. A user can send 50 invites an hour, and a workspace 100 a day.

On your invite page:

```ts
// Works signed out: show what the invite is for.
const info = await auth.workspaces.inviteInfo({ token });
// { ok: true, email: string, role: string, workspace: string, invitedBy: string | null }
// | { ok: false, error: "link_expired" }

// After the user signs in or signs up with that e-mail:
const result = await auth.workspaces.acceptInvite({ token });
// { ok: true, workspace: { id, name, createdAt }, role } | errors: invalid_token, link_expired, invite_email_mismatch
```

The signed-in user's verified e-mail must match the invite. A new user can sign up with Google, or with a password and the verification link, and then accept.
