---
url: /saas-starter/v0.9.0/guide/how-to/sign-in-from-a-mobile-app.md
---
# Sign in from a mobile or desktop app

::: tip One page for every option
This page covers every choice of `--Persistence`, `--Auth`, `--IncludeTests` and `--Aspire`.
A label says which options a part is for; the copy in your app's `docs/` folder has only yours.
`ApiHost` stands for your app's name.
:::

The API that serves the React client also serves your other apps (iOS, Android, desktop, a
command-line tool) with the same accounts. The difference is only where each kind of client keeps
its **refresh token**:

| Client | Access token | Refresh token |
|---|---|---|
| Browser (the React client) | In the page's memory, sent as `Authorization: Bearer` | In an HttpOnly cookie the page can't read. It asks for this with the `X-Refresh-Token-Cookie: true` header |
| Any other app | The same | In the response body. Keep it in the platform's secure storage: Keychain on iOS and macOS, Keystore (for example through `EncryptedSharedPreferences`) on Android, the Credential Manager on Windows |

An app just leaves the header out. Nothing needs turning on, and no CORS rule is needed: CORS
only applies in browsers.

## Signing in

```http
POST /api/auth/login
Content-Type: application/json

{ "email": "ada@example.com", "password": "..." }
```

```json
{ "accessToken": "eyJ...", "refreshToken": "q3Jx...", "expiresAtUtc": "2026-11-28T10:15:00Z" }
```

Send the access token with every request (`Authorization: Bearer eyJ...`). It expires after
`Jwt:AccessTokenLifetimeMinutes` (15 by default). Keep the refresh token in secure storage, never
in plain preferences or files.

* **Two-factor authentication:** for an account with it on, login answers 403 with
  `"reason": "TwoFactorRequired"` and a `twoFactorTicket`. Ask for the code from the authenticator
  app (or a recovery code), and send both within five minutes:
  `POST /api/auth/two-factor` with `{ "ticket": "...", "code": "123456" }`, which returns the
  tokens. A wrong code answers 401, and counts towards the lockout.
* **"A new sign-in to your account":** signing in sets a `known_device` cookie, and a sign-in
  without it is emailed to the user as a new device. Keep the cookies the API sets (a cookie
  container on the HTTP client) so the user isn't emailed each time the app signs in.

## Refreshing

When a request gets a 401, or shortly before `expiresAtUtc`, exchange the refresh token for new
tokens:

```http
POST /api/auth/refresh
Content-Type: application/json

{ "refreshToken": "q3Jx..." }
```

The response has the same shape as the login's. **Each refresh replaces the refresh token**:
store the new one, replacing the old, before doing anything else. A 401 here means the session has
ended (signed out, the password changed, the account locked or deleted): delete the stored tokens
and show the sign-in screen.

Refresh tokens last `Jwt:RefreshTokenLifetimeDays` (14 by default) and each refresh starts the
period again, so an app that's opened at least that often stays signed in.

### When a refresh's response is lost

On a bad connection, the request can reach the API while its response never comes back. The API
has then retired the old token, but the app never got the new one. Simply **retry with the same
token**: for `Jwt:RefreshTokenReuseGraceSeconds` (30 by default) after it was replaced, the API
accepts it again and returns new tokens.

After that, reusing a replaced token is taken as a sign it was stolen: **every session of the
user ends**, on all their devices. So never keep using an old token on purpose, and make sure two
parts of the app don't refresh at the same time with the same token: share one refresh between
them, as `refreshTokens` does in `src/Client/src/lib/api/client.ts`.

## Signing out

```http
POST /api/auth/logout
Content-Type: application/json

{ "refreshToken": "q3Jx..." }
```

Then delete the stored tokens. The access token keeps working until it expires (at most a few
minutes), as with the browser.

::: info With organisations

## Organisations

The access token is for one organisation at a time (its `org` claim). To work in another of the
user's organisations:

```http
POST /api/organisations/switch
Authorization: Bearer eyJ...
Content-Type: application/json

{ "organisationId": "0193...", "refreshToken": "q3Jx..." }
```

It returns new tokens, like a refresh. A refresh keeps the session's organisation, or moves to
another of the user's if they're no longer a member.
:::

## Other ways to sign in

* **Passkeys** work from apps through the platform's passkey APIs (`ASAuthorization` on Apple
  platforms, Credential Manager on Android), against the same `/api/auth/passkey/*` endpoints the
  browser uses. The app's identifier must be associated with your domain (Apple's
  `apple-app-site-association`, Android's `assetlinks.json`), because passkeys belong to the
  site's domain.
* **Google and GitHub** sign-in ends by redirecting to the web client's `/external-login` page,
  which suits browsers. For an app, open that flow in the system browser
  (`ASWebAuthenticationSession`, Custom Tabs) and add a redirect to the app's own URL scheme or
  universal link in `ExternalLoginEndpoints.cs`. That isn't included.

## Third-party apps

All of this is for your own apps. If other companies' apps will sign your users in ("Sign in with
YourApp", or API access on a user's behalf), you need an OAuth 2.0 / OpenID Connect server, such
as OpenIddict, rather than handing out these tokens.
