> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gatojs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Handing the app over to a client

> Set up production accounts in a GatoJS app, hand it over to the client with superadmin service accounts and a client admin, and remove support access when the support period ends.

GatoJS is built for agencies and teams that build an app, hand it over to a client, and keep supporting it for a while. Two global roles make that work:

* **`superadmin`** is the **service account** of the team that builds and maintains the app. It keeps access for the support period, and the client can't change or remove it.
* **`admin`** is the client's administrator. It manages users and data, but can't touch service accounts.

See [Superadmin and service mode](https://docs.gatojs.com/concepts/superadmin-and-service-mode.md) for what each role can do inside the app.

| Account | For | Global role | Membership after `admin:create` | Who can change or remove it |
| - | - | - | - | - |
| Service | People on your team | `superadmin` | `owner` of the oldest organization | Only a `superadmin`. A client admin gets `403`. |
| Client admin | The person the client names | `admin` | `owner` of the oldest organization | An `admin` or a `superadmin`. |

Because the client can't remove service accounts, **your team decides when support access ends**, and removes it as described [below](#remove-support-access).

## At handover

Create the accounts right after the first successful deploy, before you open sign-up. On Railway, run `admin:create` from your machine through `railway run` with the database's public URL (see [Create the first accounts](https://docs.gatojs.com/deployment/railway.md#create-the-first-accounts)).

**1. Create the service account with the client's organization name**

The first `admin:create` on an empty database creates the organization, and `--org-name` only works then. Without it, the organization gets the placeholder project name.

```bash theme={null}
railway run sh -c 'DATABASE_URL="$DATABASE_PUBLIC_URL" npm run admin:create -- --email support@your-team.com --role superadmin --org-name "Acme Ltd"'
```

**2. Create the client admin**

Omit `--role`; the default is `admin`.

```bash theme={null}
railway run sh -c 'DATABASE_URL="$DATABASE_PUBLIC_URL" npm run admin:create -- --email it@acme.com'
```

Both accounts become `owner` of the same organization. The order of accounts doesn't change that.

**3. Let the client set their own password**

Don't hand over the generated password. Ask the client to use "forgot password" (`/forgot-password`) to set their own. It also proves that production email works.

If you do share the generated password, use a secure channel and ask them to change it on `/account`. Neither a reset nor a change signs out other sessions, so don't sign in to the client's account yourself.

**4. Walk through it with the client**

The client signs in and opens **Users**. Service accounts show a service badge and no **Sign in as** button.

**5. Agree on the flags**

Decide with the client on `ADMIN_IMPERSONATION` (may their admin impersonate regular users?), `MULTI_ORG` and `AUTH_PUBLIC_SIGNUP`. Set them on the service and redeploy. Until now, `AUTH_PUBLIC_SIGNUP` must stay `false`.

With `AUTH_PUBLIC_SIGNUP=true` and `MULTI_ORG=false`, anyone who signs up becomes a `member` of the client's organization.

> [!TIP]
>
> The badge text for service accounts comes from the `role-superadmin` key in `src/features/users/i18n/en.json` and `pl.json`. Change it to your team's name.

A service account is an `owner` of the client's organization, so it works there like any owner: no service-mode banner and no confirmation dialog on data changes. Service mode applies only when a superadmin works in an organization they're not a member of.

### Access outside the app

App roles don't cover infrastructure. Anyone with access to the Railway project has the database and every variable, including `BETTER_AUTH_SECRET`. The kit doesn't manage access to Railway, the repository, Resend or your log collector; agree on transferring those with the client separately.

> [!WARNING]
>
> When your team loses access to the hosting project, consider rotating `BETTER_AUTH_SECRET`. It signs session cookies, including the cookie cache that some code trusts without a database lookup, so anyone who knows it can forge a cookie. After rotation, every existing session stops working and everyone signs in again.

## Remove support access

At the end of the support period, a superadmin removes the team's access. The order matters because of how better-auth and the kit work:

* **An account can't delete itself.** `/admin/remove-user` answers `400 "You cannot remove yourself"`, so the last service account can't remove itself.
* **Changing the role to `user` doesn't remove the `owner` membership**, so the account would still see organization data.
* **The change is one-way.** Once the role is `user`, it can't be restored: only a superadmin can grant `superadmin`.
* **Impersonation sessions outlive the impersonator.** A **Sign in as** session belongs to the impersonated account, expires after 3600 seconds, and isn't deleted when the service account is removed.

So your team removes every other service account, strips the last one of its role and memberships, and the client deletes what's left.

| Step | Who | What | Result |
| - | - | - | - |
| 0 | Client | Signs in as their admin and confirms access. | After step 5, only this account manages users. |
| 1 | Your team, in every browser | Ends every impersonation with **Back to my account**, or waits an hour since the last **Sign in as**. | Impersonation sessions are gone. |
| 2 | Your team, signed in with the service account that stays until the end | Opens `/api/auth/get-session` (own `user.id`) and `/api/auth/admin/list-users?searchField=email&searchValue=<address>` for each other service account (`users[0].id`). | A list of IDs. |
| 3 | Your team | Removes the other service accounts with `/admin/remove-user`. A superadmin can remove another superadmin. | `{"success":true}` for each. |
| 4 | Your team | Opens `/api/auth/organization/list` and leaves each organization with `/organization/leave`. | `/organization/list` returns an empty list. |
| 5 | Your team | Sets its own role to `user` with `/admin/set-role`. | No service role, no memberships. A new session lands on the "no organization" page. |
| 6 | Your team | Signs out. | The current session is deleted. Other sessions remain, but the account has no role or membership. |
| 7 | Client | Removes the remaining account with `/admin/remove-user` and the ID from step 2. Before step 5, the same call fails with `403`. | The account disappears from **Users**. |

After step 6, your team has no access to app data. Deleting the account in step 7 cascades to its sessions, sign-in methods, memberships and the invitations it sent.

### The API calls

The kit has no UI for changing roles, deleting accounts or leaving organizations, so these steps call better-auth endpoints under `/api/auth/` from the browser's developer console. Open the app at the `APP_URL` address and sign in first: `fetch` from that tab sends the session cookie and `Origin` header, and better-auth rejects other origins.

```js Remove a user (steps 3 and 7) theme={null}
await fetch("/api/auth/admin/remove-user", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ userId: "<account id>" }),
}).then((response) => response.json());
```

```js Leave an organization (step 4) theme={null}
await fetch("/api/auth/organization/leave", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ organizationId: "<organization id>" }),
}).then((response) => response.json());
```

```js Change own role (step 5) theme={null}
await fetch("/api/auth/admin/set-role", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ userId: "<own id>", role: "user" }),
}).then((response) => response.json());
```

Hand the client a ready-made call for step 7 with the ID filled in, or run it together on a shared screen.

### When you're the only owner

If step 4 fails with `"You cannot leave the organization as the only owner"`, the service account is the sole owner, for example of an organization it created itself with `MULTI_ORG=true`. This doesn't happen in the organization created by `admin:create`, because the client admin is an owner too.

The members form doesn't assign `owner`, and better-auth lets only an owner grant it. So before step 5, while you're still an owner, promote someone from the client who is a member:

1. Open `/api/auth/organization/list-members?organizationId=<organization id>` and take the membership `id` (not `userId`) of that person.

2. Run:

   ```js theme={null}
   await fetch("/api/auth/organization/update-member-role", {
     method: "POST",
     headers: { "Content-Type": "application/json" },
     body: JSON.stringify({ organizationId: "<organization id>", memberId: "<membership id>", role: "owner" }),
   }).then((response) => response.json());
   ```

3. Repeat step 4 for that organization.

If no one from the client belongs to it, for example a test organization of your own, delete it while you're still owner: call `/api/auth/organization/delete` with `{ "organizationId": "<organization id>" }`. With `MULTI_ORG=false`, the kit allows this only for a global `admin` or `superadmin`. Deleting cascades to its memberships and invitations.

> [!NOTE]
>
> Between steps 5 and 6, UI elements that read the role from the cookie cache may show admin menus for up to 5 minutes. Data stays protected: `getOrgContext()` reads the session from the database, bypassing the cookie cache.

## Related

> [card] [Superadmin and service mode](https://docs.gatojs.com/concepts/superadmin-and-service-mode.md)
>
> What service accounts can do and how service mode works.

> [card] [Auth and roles](https://docs.gatojs.com/concepts/auth-and-roles.md)
>
> Global roles, organization roles and sign-up policy.

> [card] [Deploy on Railway](https://docs.gatojs.com/deployment/railway.md)
>
> The deploy steps that come before handover.

> [card] [Known limitations](https://docs.gatojs.com/known-limitations.md)
>
> Impersonation and service accounts after support.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.