# Linked apps (sign in once across your apps)
Source: https://sovaria.in/docs/linked-apps

Link two or more of your organization's projects so people move between the apps already signed in, while each app keeps its own accounts, roles, policies and tokens.

A store, a driver app and an admin panel are often separate projects. With **Organization › Linked apps** an owner or admin links them: someone signed in to the store opens the admin panel without signing in again. Nothing is shared between the apps' browsers (no shared cookies, no shared storage). Each app gets its own session for that person, signed with its own key, and a token for one app is refused on every other app.

## How it works

Your app calls `sovaria.auth.openLinkedApp(otherProjectId, address)`. The TypeScript SDK (0.14.0 or later, keeping the session in the browser) does the rest on both sides, in one browser tab:

- **The other app asks.** Its page makes a secret (a PKCE verifier) that stays in that tab's session storage and asks Sovaria to come back signed in. Sovaria checks the two apps are in the same group of your organization, that the return address is one of that app's own, and that the starting address is one of the first app's own.
- **Your app answers.** Back on the first app, the signed-in person's own session turns the request into a one-time code (single use, 60 seconds, only for that app and that address) and the browser returns with it in the URL fragment, which browsers don't send to servers or in Referer headers.
- **The other app signs in** with the code and the secret only its tab holds. A link someone else started (forwarded, or planted in an email) can never sign anyone in, because that tab has no secret for it.

The person's account in that app: the one already linked to them, else the account with the same verified email, else a new one when **Create accounts on first visit** is on **and** that app's own "Allow new users to sign up" switch is on. Their roles in each app are that app's own: linking grants nothing. Guest (anonymous) sessions can't open linked apps, and links work between production environments only. When the person is already signed in to the other app, it keeps that session and doesn't sign in again.

Store app:
```ts
// A button in the store: open the admin panel, already signed in.
await sovaria.auth.openLinkedApp('<admin project id>', 'https://admin.example.com/orders')
```

Admin app:
```ts
// Nothing to add: the SDK finishes on load. To wait for it, or show a stale link:
const result = await sovaria.auth.linkedHandoffResult()
if (result?.error) showSignIn(result.error.message)
```

Store app, signed out:
```ts
// Someone arrived from the admin app but must sign in first. Call this after sign-in,
// or they stay on the store (the request waits in this tab for 5 minutes):
await sovaria.auth.continueLinkedApp()
```

Addresses: an app's project address, its auth Site URL and redirect URLs, or exact addresses you list for it in Linked apps (https; http only for localhost; never a match that only a wildcard host allows). Addresses you list there are also added to that project's allowed origins, so the browser may call its API, and taken out again when you remove them or the app leaves the group (origins your team added stay). Only list addresses the app is served from: an address on a shared domain can be someone else's, and none of the app's redirect URLs may be an open redirect (a page that forwards to any address it is given). A hash route on the address (`#/orders`) is kept.

## Isolation between apps

Every app on `*.sovarias.in` is its own site for sign-in. The SDK keeps each session in that app's own browser storage. Sovaria never sets a cookie for the whole domain, every cookie answered on an app's host loses any `Domain` attribute (however it is spelled), and Sovaria's own cookie names are refused. A site that writes a cookie for the shared domain is refused at publish with `SHARED_COOKIE_DOMAIN`, naming the file and line.

## API

| Call | Who | What |
|---|---|---|
| `GET /api/v1/org/{org_id}/linked-apps` | Organization owners and admins | The groups of linked apps |
| `POST /api/v1/org/{org_id}/linked-apps` | Owners and admins | `{ name, project_ids, auto_create_users }`: link two or more projects (a project is in one group at most) |
| `PATCH /api/v1/org/{org_id}/linked-apps/{id}` | Owners and admins | `name`, `auto_create_users`, `add_projects`, `remove_projects`, `origins: [{ project_id, allowed_origins }]`; a group keeps at least two apps |
| `POST /api/v1/auth/linked/start` | The other app (anon key), on its own host | `{ source_project_id, source_url, return_url, code_challenge }` → `{ request_id, url }` (5 minutes, single use) |
| `POST /api/v1/auth/linked/handoff` | The signed-in app user, on the first app's host | `{ request_id }` → `{ url }` back to the other app with the code |
| `POST /api/v1/auth/linked/exchange` | The other app (anon key), on its own host | `{ code, code_verifier }` → a session for that app |

| Error code | Meaning |
|---|---|
| `LINKED_NOT_LINKED` | The apps aren't (or are no longer) in the same group |
| `LINKED_NOT_ALLOWED` | From `start`: the apps aren't linked, or an address isn't one of its app's (one answer for both, so nobody can probe which apps are linked) |
| `LINKED_REDIRECT_NOT_ALLOWED` | From `handoff`: the return address is no longer one of the other app's addresses |
| `LINKED_REQUEST_INVALID` / `LINKED_HANDOFF_INVALID` | Expired, already used, made for another app or address, or the verifier doesn't match |
| `LINKED_PKCE_INVALID` | `code_challenge` isn't a base64url S256 challenge |
| `LINKED_PRODUCTION_ONLY` | A staging or branch environment took part |
| `LINKED_ANONYMOUS` | A guest session tried to open a linked app |
| `LINKED_APP_USER_ONLY` | Not an app user's own session (a console member, an AI assistant, support access) |
| `LINKED_ACCOUNT_DISABLED` | The person's account in that app is turned off |
| `SIGNUP_DISABLED` | No account in that app, and it doesn't take new sign-ups |
| `LINKED_PROJECT_IN_POOL` / `LINKED_PROJECT_NOT_IN_ORG` / `LINKED_TOO_FEW` / `LINKED_APP_NOT_IN_POOL` | Group changes refused |

AI assistants connected over MCP can't create links or change linked apps. Removing an app from a group stops new links into it; people keep their accounts.
