
# Choosing a sign-in shape

Every app picks one of three sign-in shapes with the `[auth] provider` key in `agentcloud.toml`: `"edge"`, `"agent.cloud"` (the same as leaving it out) or `"none"`. After `agc ship`, `agc status` shows an organization app's live mode in `data.sign_in.mode`, and who can open it in `data.audience`.

| Shape | `[auth] provider` | What a visitor sees | Sign-in code in the app |
|---|---|---|---|
| 1. Private tool | `"edge"` | A sign-in before any page; then the app, already signed in | None: read two request headers |
| 2. Public pages, then sign-in | omitted, or `"agent.cloud"` | Public landing pages; sign-in (Google or an email link) where an account is needed | [Two routes](#agent-cloud-sign-in-shape-2), `/auth/sign-in` and `/auth/callback` |
| 3. Fully public | `"none"` | The app; nothing ever asks them to sign in | None |

Use the edge gate for internal tools, with an [audience](/docs/build/the-edge-gate.md#audiences-who-can-open-an-app) such as `agc audience set org`: new apps default to `anyone`, so the gate alone doesn't limit access to your team. Use shape 2 when strangers must see something before signing in, and `none` only when the app holds nothing about who visits.

## Execution contract

- **Before starting:** Read the organization default and decide who should have access. Edge sign-in alone does not restrict access to teammates.
- **Do:** Select the documented `[auth] provider` shape, implement its routes or headers, then test on the mirror before shipping.
- **Success:** The intended sign-in flow works and the owner-set audience admits the intended people.
- **If blocked:** If a target lacks an edge gate, use a supported shape or have its owner configure sign-in.
- **Human handoff:** Keep the organization default unless the person asks to change it. A live provider change needs approval; `agc audience set` works only for an owner's agent.

## Ask your agent

```text
This is an internal tool for my team only. Put the whole app behind Agent Cloud's
sign-in and don't build any login pages. Tell me which sign-in shape you used.
```

The agent sets `[auth] provider = "edge"` and ships. A live app changing mode gets an approval link, "Changes how people sign in". For shape 2:

```text
Let anyone browse the camp list, but make parents sign in before they book.
Use Agent Cloud's sign-in, not your own login, and key users on the id it returns.
```

The agent adds two routes, a users table and a session, and runs `agc check`.

## Steps

```toml
[auth]
provider = "edge"   # shape 1; "agent.cloud" (or no [auth]) is 2; "none" is 3
```

```bash
agc check
agc ship
agc status
```

`agc status` shows `data.sign_in`: the app's `mode`, and for an app in an organization its `org_default` and `is_org_default`.

## How it works

| Rule | |
|---|---|
| How the agent picks | AGENTS.md says most apps have public pages plus sign-in, so the starters wire shape 2. The agent fits the shape to the request and asks only when it's open, such as whether outsiders get in |
| The organization's default | An owner's setting that `agc init` writes into a new `agentcloud.toml` ([New-app defaults](/docs/owners/new-app-defaults.md)). Organizations created after the edge gate shipped on Agent Cloud's own cluster start with `edge`; older ones keep theirs |
| The manifest decides, per release | The app's mode is its live release's: a failed or unapproved ship changes nothing, and removing `[auth]` moves the app back to Agent Cloud sign-in. So a live app's mode change waits for a person ([the reason it shows](/docs/owners/new-app-defaults.md#the-manifest-wins)) |
| `none` | No gate, but still `AGC_AUTH_URL` and `AGC_AUTH_TOKEN`, so a starter keeps booting and can offer optional sign-in |
| Internet-facing targets | On an organization's target whose apps are on the internet, every release is behind the target's gate whatever the manifest says, and a mode change asks no approval |

## Agent Cloud sign-in (shape 2)

The app gets `AGC_AUTH_URL` and `AGC_AUTH_TOKEN` and adds two routes. `GET /auth/sign-in` redirects to `$AGC_AUTH_URL/authorize?state=…`. `GET /auth/callback` checks `state`, then trades the `code` at `POST $AGC_AUTH_URL/token` for `{ user: { id, email, name, method, role } }`, where `role` is `"owner"` or `"member"` for a person in the app's organization and `null` for anyone else. The app never sees a password or a provider secret. Success is `agc check` passing `sign-in checks state`, and a person landing back in the app signed in.

Key users on `user.id` in a `uuid` column: mirrors keep uuids and mask emails. Leave `email` and `name` nullable, since a mirror's test sign-in by id has neither. Store a session by its cookie's sha256, never the value.

The routes in Hono, as the starters do; the rules carry over to any server.

```ts
const mirror = Boolean(process.env.AGENTCLOUD_MIRROR); // http://localhost: no Secure, so no __Host-
const STATE = mirror ? 'auth_state' : '__Host-auth_state';
const opts = { httpOnly: true, secure: !mirror, sameSite: 'Lax', path: '/' } as const; // no Domain, ever

app.get('/auth/sign-in', (c) => {
  const state = randomBytes(24).toString('base64url');
  setCookie(c, STATE, state, { ...opts, maxAge: 600 }); // seconds
  return c.redirect(`${process.env.AGC_AUTH_URL}/authorize?state=${state}`);
});

app.get('/auth/callback', async (c) => {
  // 1. Check state FIRST: trading the code first still uses it up and signs the wrong person in.
  const sent = Buffer.from(c.req.query('state') ?? ''), kept = Buffer.from(getCookie(c, STATE) ?? '');
  deleteCookie(c, STATE, { path: '/', secure: !mirror });
  if (!kept.length || sent.length !== kept.length || !timingSafeEqual(sent, kept)) return c.text('Sign-in expired. Try again.', 400);
  // 2. Trade the code: once, within a minute.
  const r = await fetch(`${process.env.AGC_AUTH_URL}/token`, { method: 'POST',
    headers: { authorization: `Bearer ${process.env.AGC_AUTH_TOKEN}`, 'content-type': 'application/json' },
    body: JSON.stringify({ code: c.req.query('code') ?? '' }) });
  if (!r.ok) return c.text('Sign-in failed. Try again.', 400);
  const { user } = await r.json(); // { id, email, name, method, role }
  // 3. Upsert users by user.id, never email. 4. Your own session: a day, in a __Host-session cookie.
  return startSession(c, user);
});
```

`agc check` drives `/auth/sign-in` (expecting exactly the redirect to `$AGC_AUTH_URL/authorize?state=…`) and `/auth/callback`, returning the code with a wrong `state`. An app that trades it fails `sign-in checks state`, here and in the rehearsal. Without a usable `state`, `/authorize` shows a 400 page: "The app didn't send a state to check the sign-in against, so it can't start."

Agent Cloud signs the person in, records them as one of the app's users and sends a code to `/auth/callback`. The [Sign-in broker API](/docs/reference/sign-in-broker-api.md) has the routes, `method` values, the mirror's test sign-in and limits.

- **`__Host-` cookies outside a mirror**, so a neighbor app can't plant one with your cookie's name ([why](/docs/security/web-security-for-the-console-and-apps.md#same-site-neighbours-and-webhooks)). A mirror's plain `http://localhost` drops `Secure` cookies, so the example skips the prefix there.
- **Sessions a day or less, or recheck**: a removed person keeps access as long as their session, so recheck a longer one daily with `GET $AGC_AUTH_URL/users/<id>` and end it on 404 `auth.no_user`.
- **Match the sign-in page to the app**: `[auth] theme = "light"`, `"dark"` (default) or `"system"`.
- **Sign-in on the app's own pages**: to show your own Google button and email field instead of Agent Cloud's page, the server POSTs `{ state }` to `$AGC_AUTH_URL/stytch/google` (or `{ state, email }` to `/stytch/email`) with the same token and redirects to the `url` it answers; `/auth/callback` stays as above.

## Reference

Who gets in is the app's audience: `agc audience` reads it, and an owner's agent sets it with `agc audience set` ([audiences](/docs/build/the-edge-gate.md#audiences-who-can-open-an-app)). On a target with its own sign-in, `"edge"` defers to the target's identity provider; `"none"` lets anyone in. Key values: [agentcloud.toml reference](/docs/reference/agentcloud-toml-reference.md#what-the-keys-do).

## Errors

Every code, its exit and fix: [Error code catalog](/docs/reference/error-code-catalog.md).

```json
{
  "ok": false,
  "product": "agent.cloud",
  "command": "ship",
  "error": {
    "code": "manifest.bad_auth",
    "message": "agentcloud.toml says [auth] provider = \"okta\"; it takes \"agent.cloud\" (the default), \"edge\" (your target's own sign-in) or \"none\" (public).",
    "fix": "Set provider to one of those, or remove the [auth] table to use agent.cloud sign-in.",
    "retryable": false,
    "docs": "agc help manifest"
  },
  "next": [{ "cmd": "agc help manifest", "why": "what [auth] takes" }]
}
```

The Well-Architected review flags a mismatch as `signin-mode`: medium for an app signing people in itself on a target with its own sign-in, high for `provider = "edge"` on a target that lost its sign-in.

## Agent notes

- Keep the `[auth]` line `agc init` wrote; deleting it is a mode change a person has to approve.
- Don't write a login form, store passwords or add another identity provider.
- Collect no data about people beyond what sign-in and the app's purpose need.
- Never key users on email (masked on mirrors, and it can change), and never log the code, the token or the session id.

## Related

- [The edge gate (shape 1)](/docs/build/the-edge-gate.md), where Agent Cloud keeps the session for you
- [New-app defaults](/docs/owners/new-app-defaults.md)
- [Approvals](/docs/concepts/approvals.md)
- [Same-site neighbours, CSRF and webhooks](/docs/security/web-security-for-the-console-and-apps.md#same-site-neighbours-and-webhooks)
- [Sign-in broker API](/docs/reference/sign-in-broker-api.md)

<!-- feedback:start -->
---
Wrong, outdated or unclear? Run `agc feedback docs "build/choosing-a-sign-in-shape: <what is wrong>" --body <file>` with the sentence quoted in the file. No code or app data is sent.
<!-- feedback:end -->
