# Pairing a device

> Signing in a new device with a QR code or a short code, approving it on a device that is already signed in, signing in through the browser, revoking devices and what to do when one is lost.

Source: https://docs.posternmail.app/apps/pairing/

This page explains the two ways to sign in a new device, what you approve and why, the limits
that protect your account, and how to sign a device out again.

| Way | For | You need |
|---|---|---|
| **Pairing**: scan a QR code or type a short code | Phones, and browsers on another computer | A device that is already signed in, next to you |
| **Sign in with your Hub address** through the browser | Phones and the desktop app | Your Hub's address and your password, passkey or single sign-on |

Pairing is the fastest way to add a phone. Nothing is signed in until you approve the request
on the device that showed the code.

## Show a pairing code

On a device that is already signed in:

| Where | How |
|---|---|
| Web app or desktop app | **Settings → Devices → Connect a device**. Also in the command palette (`⌘K` or `Ctrl+K`) as **Connect a device…**. |
| iOS or Android app | **Settings → Devices → Connect a device** |

The **Connect a device** dialog shows:

- a **QR code** for the phone app to scan;
- the **Code**, for example `K7QF-2M9X`, and how long it still works ("Works for another
  4:59");
- **Copy link**, a link such as `https://mail.example.com/pair#K7QF-2M9X` that signs in a
  browser on another computer;
- **New code**, which makes a fresh one;
- **No app yet?**, links to the download page.

| Property | Value |
|---|---|
| Valid for | 5 minutes from when it is shown |
| Use | Once. After a device has used it, it is gone. |
| Format | 8 characters, shown as two groups of four with a dash. Letters and digits without `0`, `1`, `I` and `O`, so they can't be confused. Upper or lower case and the dash don't matter when you type it. |
| QR code content | `postern://pair?hub=<your Hub's address>&code=<code>`, plus the Cloudflare Access token if your Hub has one (see [below](https://docs.posternmail.app/apps/pairing/#hubs-behind-cloudflare-access)) |

The Hub's address in the QR code and the link is `POSTERN_PUBLIC_URL` when it is set, and
otherwise the address the signed-in browser used. If phones reach your Hub by another address
than your computer does, set `POSTERN_PUBLIC_URL`. See [Configuration](https://docs.posternmail.app/configuration/).

**Connect a device** is greyed out while your Hub can't be reached: "Connecting a device needs
your Hub, which can't be reached right now".

## Connect a phone with the QR code

1. On the phone, open Postern and tap **Scan QR code**. Allow the camera when asked. (The
   phone's camera app can also scan it; it opens Postern.)
2. The phone asks **Connect to this Hub?** and shows the Hub's address. Continue only if it is
   your Hub. For a plain HTTP address it adds "Not encrypted (HTTP)". Tap **Continue**.
3. The phone shows **Approve on your other device** with the code.
4. The signed-in device shows a request. Check that the name and the code match the phone in
   your hand, then approve it (see [Approve the request](https://docs.posternmail.app/apps/pairing/#approve-the-request)).
5. The phone says **Approved**, then "Setting up your mailboxes… The first time can take up to
   half a minute."

> **Check:** the phone shows your inbox, and **Settings → Devices** on the other device lists
> the phone.

> **If it did not work:** see [Messages](https://docs.posternmail.app/apps/pairing/#messages) below. A QR code from somewhere else gives
> "That QR code isn't from Postern."

### Type the code instead

No camera, or the camera can't read it: tap **Scan QR code**, then **Enter the code instead**.
Enter your Hub's address and the code shown next to the QR code, then **Continue**. You can
also paste the whole link from **Copy link** into **Hub address**.

## Sign in a browser with a code

To sign in the web app on another computer:

- Open the link from **Copy link** in that computer's browser. The page uses the code by
  itself.
- Or open your Hub's sign-in page, click **Sign in with a code from another device**, enter the
  code and click **Continue**.

The page then says **Approve on your other device** and "Waiting for approval…". After you
approve, the browser is signed in. **Use a different code** starts over.

## Approve the request

Every signed-in device that is open gets the request.

**Web app and desktop app:** a dialog **Sign in on** *device name*? shows the device's name,
its kind (iOS, Android, Desktop app, Web browser) and the code with "Same code on the device?".
**Deny** comes first and has the focus, so a stray key press never approves a device. Click
**Approve**.

When you approve a phone or the desktop app from a browser, the Hub wants proof that it is
really you, because that device will receive your mailbox sign-ins:

- If your account has a password, the dialog asks for **Your Hub password, to confirm it's
  you**, unless you signed in on this browser within the last 10 minutes.
- If your account has no password (passkey or single sign-on only), sign out of the web app,
  sign in again and approve within 10 minutes. The Hub says: "For your security, please
  confirm it's you: sign out, sign in again and retry within 10 minutes."

**iOS and Android app:** an alert **Connect a new device?** says which device wants to connect.
Tap **Approve** or **Don't allow**. With App Lock on, the alert waits until you unlock the
app.

The request must be answered within 5 minutes. **Deny** ends it, and the new device says "The
request was declined on your other device."

## Sign in with your Hub address

Phones (**Enter Hub address**) and the desktop app sign in through your Hub's own pages:

1. The app opens your Hub's sign-in page: in a secure browser sheet on iOS, a Custom Tab on
   Android, and a sign-in window in the desktop app.
2. Sign in with any method your Hub offers: passkey, password and two-step code, or single
   sign-on.
3. The page asks **Sign in on** *device*? and shows the device. Click **Allow and open the
   app**. If your last sign-in on that browser was more than 10 minutes ago, it asks for **Your
   password** first, or, for accounts without a password, to **Sign in again**.
4. The browser hands a one-time code back to the app, which exchanges it for its own session.
   The code is valid for 2 minutes and protected with PKCE, so only the app that started the
   sign-in can use it. The app never sees your password.

**Cancel** on that page returns to the app without signing in ("The sign-in was canceled. Try
again when you're ready.").

## Hubs behind Cloudflare Access

If Cloudflare Access protects your Hub, the apps need its service token on every request. Set
the token on the Hub (`POSTERN_ACCESS_CLIENT_ID` and `POSTERN_ACCESS_CLIENT_SECRET`). The Hub
then hands it to apps that have already passed Access and signed in:

- in the pairing QR code and link (shown only to a signed-in user);
- in the browser sign-in's hand-back to the app;
- in the sign-in result.

A phone that scans the QR code from the web app needs no extra step. A phone that signs in with
**Enter Hub address** first confirms your email address with Cloudflare in the browser sheet.
The token is a second lock in front of the Hub, never a replacement for signing in. Treat a
pairing QR code like a key while it is valid. See [Cloudflare](https://docs.posternmail.app/reach/cloudflare/).

## Messages

| Message | Where | Meaning |
|---|---|---|
| This pairing code is invalid or has expired | New device | The code was mistyped, used before, or is older than 5 minutes. Get a **New code**. |
| This code is incomplete. Scan the QR code again. | Phone | The scanned link has no Hub address or code. |
| The request ran out of time before it was approved. Try again. | Phone | Nobody approved within 5 minutes. |
| That code has expired | Browser | Same, for a browser. |
| The request was declined on your other device. / The sign-in was denied | New device | Someone chose **Deny** or **Don't allow**. |
| This code has expired / This code has run out. | Device showing the code | 5 minutes passed. Click **New code**. |
| Too many attempts. Please wait a moment and try again. | Anywhere | A rate limit (below). Wait, then try again. |
| That password isn't right. | Approving in the web app | The confirmation password was wrong. |
| Sign in on this device with your password, passkey or single sign-on to use your mailbox credentials here. | New device | It was approved by a session that may not hand out mailbox sign-ins. Sign it in with **Enter Hub address** instead. |

## Rate limits

The Hub limits these requests per client IP address or per account. The limits are built in
and can't be changed. Each starts with a burst, then allows one more try per interval.

| What | Per | Burst | Then one more every |
|---|---|---|---|
| Using a pairing code (scan or type) | IP address | 10 | 30 seconds |
| Exchanging the browser sign-in for an app session | IP address | 30 | 5 seconds |
| Sign-in attempts | IP address | 20 | 15 seconds |
| Wrong passwords or codes, including when you confirm an approval | Account | 5 | 1 minute |

A limited request gets HTTP 429 with a `Retry-After` header and the message "Too many
attempts. Please wait a moment and try again." Showing a code and waiting for the approval (the
new device asks every 2 seconds) are not limited.

Behind a reverse proxy, the Hub only sees each client's own address when it trusts the proxy
(`POSTERN_TRUST_PROXY`). Otherwise every client shares the proxy's address, and so shares these
limits. See [Configuration](https://docs.posternmail.app/configuration/).

## Sign a device out

**Settings → Devices** lists every device signed in to your account, this one first, then by when they were last seen: its name,
platform, "active now" or when it was last seen, its last IP address and whether notifications
are on. The device you are using is marked **This device**.

- **Rename** gives a device a clearer name (up to 80 characters).
- **Sign out…** asks "Sign out *name*?" ("Its session ends now, and the mail saved on it is
  removed the next time it connects."). **Sign out device** confirms.

What happens:

1. The Hub ends the device's sessions at once, deletes its push registration and closes its
   live connection. It can no longer reach your account, settings or mailbox sign-ins through
   the Hub.
2. The next time the device reaches the Hub, it deletes the mail and the mailbox sign-ins it
   kept. The desktop app then says "You were signed out of …"; the web app says "This device
   was signed out from another device, and the mail saved on it was removed."
3. The Hub's **Settings → Security** log records "Device signed out".

Pairing and sign-ins show up there too, as "Device connected" and "Mailbox sign-ins sent to one
of your devices".

## Lost or stolen device

1. On any other device, open **Settings → Devices** and **Sign out…** the lost one.
2. Check **Settings → Security** for sign-ins you don't recognise.
3. A device in direct mode holds your mailbox sign-ins. Until it reaches your Hub again, it
   keeps its copy of your mail and can still reach your providers. If it may stay offline or
   you think someone else has it, also change those sign-ins at the provider: change the
   password or app password of IMAP mailboxes, and remove Postern's access in your Google or
   Microsoft account security settings. Then reconnect the mailboxes in Postern (**Settings →
   Mailboxes**).

App Lock on the phone (see [iOS](https://docs.posternmail.app/apps/ios/#app-lock)) keeps the app closed to anyone without your
face, fingerprint or passcode in the meantime.
