Device pairing
How a staff member pairs a phone to the Hub, how the phone's session is checked, and how a phone sends a student photo, a staff profile photo, or library scans.
A staff member pairs their phone by scanning a QR code in the Hub. The phone then acts for them: taking a student's photo, or scanning books at the library desk. Pairing state lives in Redis, not a table, and tokens are stored only as SHA-256 hashes.
Pairing
Mint a code (POST /device-pairings, staff, with Feature.DEVICE_PAIRING or Feature.LIBRARY). A 32-byte token valid for 5 minutes, once. A new code replaces any unused one. The QR is {Hub}/device#t={token}; the token is in the hash, so it's never sent as a query string.
Preview (POST /device/pairings/preview). Shows the staff member and school without using the token.
Exchange (POST /device/pairings/exchange). Consumes the token atomically (two taps can't both win) and opens a 30-minute session. Any previous phone is unpaired and told so.
An unknown, expired or used code is 410 This link has expired or was already used.
The phone then sends its session as Authorization: Bearer. DeviceSessionGuard refuses a missing one (Device session required.) or an ended one (Device session ended.), and pins the request to the session's school.
| Route | Who | Does |
|---|---|---|
GET /device-pairings/current (getDevicePairing) | Staff | Paired, and connected (someone on the channel) |
DELETE /device-pairings/current (removeDevicePairing) | Staff | Unpairs and tells the phone. Always answers "Phone unpaired" |
GET /device/state | Phone | The current request |
GET /device/realtime-tokens | Phone | Tokens for its one paired-device channel |
The phone routes are public, limited per IP: preview 60 a minute, exchange 30, photo upload 120, state and tokens unlimited. Limits count in Redis across API copies (from API 52aac0dc6).
Requests to the phone
Each pairing holds one request at a time; a new one replaces the old.
| Request | Started by | Lasts | Phone sends |
|---|---|---|---|
student-photo | POST /students/:studentId/capture-requests, with update on student | 3 minutes | One PNG, JPEG or WebP up to 5 MB |
staff-photo | POST /staff/:staffId/capture-requests (createStaffCaptureRequest), staff, Feature.DEVICE_PAIRING, update on staff | 3 minutes, from the operation text | The same photo upload |
library-desk, library-capture | The library desk | A 12-hour workday | Scans, shelves, book photos. See Library |
library-cover | A book's cover button | 3 minutes | A cover photo |
A request extends the session to at least its own length. No paired phone gives Pair a phone first.
Saving a student photo. The phone has no permissions of its own: the upload re-checks that the staff member is still active and can still update that student (This phone can no longer upload photos.), and that the request is still current (410 This request has expired. Ask for a new one.). Success sets the profile image and audits it via: 'paired-device'.
Saving a staff profile photo. POST /staff/:staffId/capture-requests asks the caller's paired phone to photograph that member of staff, themselves included. No phone is 409 Pair a phone first. A missing staff profile is Staff profile not found. Cancel is DELETE /staff/:staffId/capture-requests/:requestId (cancelStaffCaptureRequest) and answers Request cancelled. The phone still uploads on POST /device/commands/:commandId/photo. When the stored command is staff-photo, that upload uses staff-photo.command.ts. It checks again that the owner is active and can still update that staff member, with the same This phone can no longer upload photos. Success calls uploadProfileImage and audits change: 'profile-image', via: 'paired-device'. Read at API main d0fbb403. The command test was not run. No photo was uploaded.
In the Hub
The laptop. The Device tab on a staff profile (shown when device pairing or library is on) mints a code once status has loaded and no phone is paired, and re-mints when it expires. It shows "Connected" or "Paired, not connected".
On Hub main 4000c140, the same tree as staging 0836a1e4, a staff photo shows Take with phone when device pairing is on. The button stays off until a phone is paired. Until then the hint is "Pair a phone from the Device tab on your staff profile first." A connected phone toasts Sent to your phone, "Take the photo on the phone." A paired phone that is not connected toasts Open the capture page on your phone, "The request waits for 3 minutes." A 409 toasts Could not reach your phone and uses that same hint, and the cached pairing status is set to not paired. Any other failure uses the same title and the error message. profile-editable-avatar.tsx was read. Its test was not run. No screen was opened.
The phone opens /device:
| Situation | Shows |
|---|---|
| No token and no session | "This link has expired" |
| Session no longer valid | "This phone is no longer paired." |
| In-app browser (Facebook, Instagram, WhatsApp, Line) | A hint to open in Safari or Chrome |
| Storage blocked | "This browser is blocking storage. Open the link in Safari or Chrome." |
| Waiting | "Waiting for a request", polling every 20 seconds and keeping the screen awake |
| An unknown request type | "Reload this page to update" |
A phone only obeys unpaired if it's still the phone that opened that socket, so a replacement pairing isn't kicked.