Mobile API Best Practices · Mobile Auth, Security & Flaky Networks
Device Registry, Sessions & Remote Logout
Track devices, bind push tokens and sign out lost phones.
Model each signed-in installation as a device session: an ID, platform, app version, last seen, push token, and the refresh-token family it owns. This enables "signed-in devices" UI, remote logout (revoke the family + delete the push token), logout everywhere after a password change, per-device rate limits and targeted push. On sign-out, the app must call the server to revoke tokens and unregister its push token — otherwise the next user on a shared device can receive the previous user's notifications.
Device lifecycle
| Event | Server action | Client action |
|---|---|---|
| Sign in | Create device session + token family | Store tokens in Keychain/Keystore; register push token |
| Push token rotated | Update device record | Send new token on change |
| User signs out | Revoke family; unlink push token | Wipe tokens + local user data |
| Lost phone (remote logout) | Revoke that device's family | Next refresh fails → local wipe |
| Password reset | Revoke all families (optionally except current) | Re-authenticate |
| App uninstalled | Push provider reports token invalid → mark inactive | — |
CREATE TABLE device_sessions (
id uuid PRIMARY KEY,
user_id bigint NOT NULL REFERENCES users(id),
platform text NOT NULL CHECK (platform IN ('ios','android')),
app_version text NOT NULL,
push_token text UNIQUE, -- one owner per token
token_family uuid NOT NULL,
last_seen_at timestamptz NOT NULL DEFAULT now(),
revoked_at timestamptz
);
@router.delete("/v1/me/devices/{device_id}")
async def remote_logout(device_id: UUID, user=Depends(current_user)):
device = await devices.get_owned(user.id, device_id) # authorization: own devices only
await tokens.revoke_family(device.token_family)
await devices.update(device.id, revoked_at=utcnow(), push_token=None)
return Response(status_code=204)
Common pitfalls
- Push tokens not unlinked on logout (notifications leak to the next user).
- No way to revoke a single device.
- Local data left on the device after sign-out.
Key points
- One session record per installation
- Logout revokes tokens and push registration
- Logout-everywhere after credential changes
Further reading: OWASP Session Management Cheat Sheet
Practice questions
Why must the push token be unlinked when a user signs out?
Answer
Otherwise the server keeps sending that user's notifications to the device, which may now be used by someone else.
How does remote logout reach a phone that's offline?
Answer
The server revokes the device's refresh-token family; the next time the app tries to refresh, it fails and the app wipes local credentials and data.
Open in the app to mark lessons complete, take notes and track your progress.