feat(backend,waiter,terminal,manager): sign-in: ask before signing a waiter's other phone out, usernames on paired screens
- auth.single_session_waiter gains 'ask', the new default (Alembic 0051 moves venues on 'off'
to it): a second phone gets auth.session_elsewhere {device, since}; Continue retries with
replace=true and the first phone is signed out at once (live session.ended, reason
"replaced") and says why on its sign-in page. Only phones count (terminal sessions are
their own client). refuse_new / end_old unchanged.
- auth.session_ended now carries the reason; the waiter app keeps it until the next sign-in.
- The waiter app sends queued offline ops on every (re)connection, so an order queued on a
phone that was signed out by another goes out after the next sign-in there (e2e checked).
- auth.terminal_require_username (venue) + Device.require_username (per device, wins):
GET /api/devices/me/users lists nobody, the screen asks for a username then PIN/password,
a picked user_id is refused (auth.username_required), "tap" falls back to PIN.
- Manager: Sign-in settings (ask, the new switch), the device dialog's usernames choice.
Plan platform-v2 6F.7.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,7 +2,11 @@
|
||||
|
||||
Sessions
|
||||
- start(db, user, client, request) creates a row and applies the client's single-session
|
||||
rule (setting auth.single_session_<client>: off | refuse_new | end_old).
|
||||
rule (setting auth.single_session_<client>: off | refuse_new | end_old | ask).
|
||||
`ask` (waiters, the default since 6F.7): a login while signed in elsewhere is refused with
|
||||
auth.session_elsewhere (where, since when) until the app retries with replace=True; then
|
||||
the old sessions end with reason "replaced" (their sockets get session.ended at once).
|
||||
Only the waiter's phones count: a terminal session (the counter) is its own client.
|
||||
- validate(sid) is called on every authenticated request (routers.deps.decode_token).
|
||||
Results are cached for CACHE_TTL_S; end()/end_for_user() drop the cache entry at once,
|
||||
so an ended session is refused on its very next request.
|
||||
@@ -61,15 +65,32 @@ def _setting(db, key: str, default: str) -> str:
|
||||
|
||||
|
||||
def single_session_rule(db, client: str) -> str:
|
||||
return _setting(db, f"auth.single_session_{client}", "off") if client in ("manager", "waiter") else "off"
|
||||
if client == "waiter":
|
||||
return _setting(db, "auth.single_session_waiter", "ask") # 6F.7: ask is the default
|
||||
return _setting(db, "auth.single_session_manager", "off") if client == "manager" else "off"
|
||||
|
||||
|
||||
# ── lifecycle ────────────────────────────────────────────────────────────────
|
||||
|
||||
def start(db, user, client: str, request=None, *, device=None, expires_at=None):
|
||||
def device_label(user_agent: str | None) -> str:
|
||||
"""A short, language-neutral name for where a session runs ("Android · Chrome")."""
|
||||
ua = user_agent or ""
|
||||
system = next((name for key, name in (("iPhone", "iPhone"), ("iPad", "iPad"), ("Android", "Android"),
|
||||
("Windows", "Windows"), ("Macintosh", "Mac"), ("Linux", "Linux"))
|
||||
if key in ua), "")
|
||||
browser = next((name for key, name in (("EdgA", "Edge"), ("Edg/", "Edge"), ("Firefox", "Firefox"), ("FxiOS", "Firefox"),
|
||||
("CriOS", "Chrome"), ("Chrome", "Chrome"), ("Safari", "Safari"))
|
||||
if key in ua), "")
|
||||
if "; wv)" in ua:
|
||||
browser = "app"
|
||||
return " · ".join(x for x in (system, browser) if x)
|
||||
|
||||
|
||||
def start(db, user, client: str, request=None, *, device=None, expires_at=None, replace: bool = False):
|
||||
"""Create a session for user (None user_id for the support account). Commits.
|
||||
`device`: the paired device of a terminal login; its previous sessions are replaced
|
||||
(a shared screen shows one user at a time)."""
|
||||
(a shared screen shows one user at a time). `replace`: the person said yes to signing
|
||||
out their other session (rule `ask`)."""
|
||||
from models.session import LoginSession
|
||||
if client not in CLIENTS:
|
||||
raise ValueError(f"unknown client {client!r}")
|
||||
@@ -80,6 +101,11 @@ def start(db, user, client: str, request=None, *, device=None, expires_at=None):
|
||||
LoginSession.ended_at.is_(None)).all()
|
||||
if active and rule == "refuse_new":
|
||||
raise XeniaError("auth.session_exists", client=client)
|
||||
if active and rule == "ask" and not replace:
|
||||
last = max(active, key=lambda s: s.created_at)
|
||||
from core.timeutil import iso_z
|
||||
raise XeniaError("auth.session_elsewhere", client=client, since=iso_z(last.created_at),
|
||||
device=device_label(last.user_agent) or "?", ip=last.ip or "")
|
||||
for s in active:
|
||||
_end_row(s, "replaced")
|
||||
if device is not None:
|
||||
@@ -186,7 +212,8 @@ def validate(sid: str | None, db=None) -> Active:
|
||||
if row is None or row.ended_at is not None:
|
||||
with _lock:
|
||||
_cache.pop(sid, None)
|
||||
raise XeniaError("auth.session_ended")
|
||||
# why it ended (replaced, logout, idle...): the app can say "you signed in elsewhere"
|
||||
raise XeniaError("auth.session_ended", reason=(row.end_reason if row is not None else None) or "unknown")
|
||||
info = Active(row.user_id, row.device_id, row.expires_at)
|
||||
stale = utcnow() - row.last_seen_at > timedelta(seconds=LAST_SEEN_EVERY_S)
|
||||
finally:
|
||||
@@ -233,3 +260,12 @@ def waiter_login_mode(db) -> str:
|
||||
|
||||
def waiter_requires_username(db) -> bool:
|
||||
return _setting(db, "auth.waiter_require_username", "false") == "true"
|
||||
|
||||
|
||||
def terminal_requires_username(db, device=None) -> bool:
|
||||
"""Paired screens (6F.7): type a username instead of picking a name. The device's own
|
||||
choice (Device.require_username) wins over the venue's auth.terminal_require_username."""
|
||||
own = getattr(device, "require_username", None)
|
||||
if own is not None:
|
||||
return bool(own)
|
||||
return _setting(db, "auth.terminal_require_username", "false") == "true"
|
||||
|
||||
Reference in New Issue
Block a user