# Sign users in to your own native app

> Let your own mobile or desktop app sign users in with its own screens, without opening a browser.

Your organization's own mobile or desktop app can show its own sign-in screens and walk users through the realm's
sign-in steps: username and password, then a one-time code if the realm asks for one. At the end it gets a code and
exchanges it for tokens, as any app does. No browser opens.

**Before you start:**

- **Who can do it:** a realm administrator, in a group that holds `RealmOwner`
  ([Delegated administration](/platform/realms/delegated-administration/)). `RealmViewer` can read the settings but
  not change them.
- **Where:** today this is done through the API; [Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`.
- **First:** be sure the app is truly your organization's own.

<Aside type="caution" title="Only for your own app">
  The app sees the user's password. For anyone else's app, use the browser sign-in:
  [Sign users in to a web app](../sign-in-web-app/) or [Sign users in to a single-page app](../single-page-app/). A native app can use the browser sign-in
  too, with a loopback or private-use return address.
</Aside>

## Set it up

<Steps>

1. Allow first-party sign-in in the realm:

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "firstPartyAppsAllowed": true }'
   ```

2. Register the app and mark it as yours with `firstParty`:

   ```sh
   curl 'https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client?id=field-app' \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{
       "status": "active",
       "clientName": "Field App",
       "tokenEndpointAuthMethod": "none",
       "firstParty": true,
       "grantTypes": ["authorization_code", "refresh_token"],
       "scopes": ["openid", "profile", "offline_access", "field:read"],
       "defaultScopes": ["openid", "field:read"],
       "allowedResources": ["https://field.example.com/api"],
       "defaultAudience": "https://field.example.com/api"
     }'
   ```

3. Optionally, offer a stronger sign-in by name: map an `acr` value to a sign-in chain, as in
   [Step up for sensitive actions](../../stronger-sign-in/step-up-for-sensitive-actions/).

</Steps>

## How the app signs a user in

1. The app starts at `POST https://foobar.cvyuh.com/oauth2/authorize-challenge` with what a browser sign-in carries:
   scopes, the API it wants, and its PKCE challenge.
2. The realm answers `insufficient_authorization` with the next step to show, and an `auth_session` to send back with
   the answers.
3. The app repeats until the realm answers with a `code`. A failed sign-in is `access_denied`.
4. The app redeems the code at the token endpoint.

The user sees the app's own screens, one for each step the realm asks for: a two-step sign-in takes two screens.
There's no consent page, because the app and the realm are one party. The exception is offline access, which the user
must have allowed earlier through the browser sign-in.

## Good to know

- **Each `auth_session` is answered once**, by this app only, within ten minutes.
- **A sign-in begun with a [DPoP](../../../applications/client-security/dpop/) proof** must use the same key throughout.
- **The session is real:** signing out ends it.
- **An agent's client is always refused**, whatever it is marked.

## Check it worked

Sign in through the app as a test user. A wrong password should be refused, and a right one should end in a `code`.
Each step is on the user's trail as with a browser sign-in (`LOGIN_STARTED`, `MODULE_COMPLETED`, then `LOGIN` or
`LOGIN_FAILED`), and a wrong password counts toward [Lockout](../../../authentication/lockout/). Find them in
**Audit → Authentication** in the dashboard.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| `unauthorized_client`, "may not sign a person in without a browser" | `firstPartyAppsAllowed` is off, or the client isn't `firstParty` | Do steps 1 and 2 |
| `unauthorized_client` for an agent's client | An agent never signs a person in this way | Use [Let an agent act for a user](../../agents/agent-acts-for-a-person/) |
| `unauthorized_client`, "may not use the authorization code grant" | The realm or the client lacks `authorization_code` | Add it to the client's `grantTypes` |
| An old `auth_session` is refused | It was answered already, or ten minutes passed | Start the sign-in again |
| No refresh token | Offline access must first be allowed through the browser sign-in | Have the user allow it once in the browser |

## Undo it

Set the client's `firstParty` to `false`, or the realm's `firstPartyAppsAllowed` to `false` to stop it for every app.

## Related

- [First-party app sign-in](../../../applications/single-sign-on/first-party-apps/): the capability, its settings and errors.
- [Authorization code with PKCE](../../../applications/single-sign-on/authorization-code/): the code the app redeems.
- [Step up for sensitive actions](../../stronger-sign-in/step-up-for-sensitive-actions/): a stronger sign-in by name.
