feat(waiter): sign-in v2 on the phones (platform-v2 step 4.2)

- Login follows auth.waiter_login_mode / auth.waiter_require_username from the
  bootstrap: name + PIN, name + password, typed username, or a "paired screens
  only" message for tap mode. POST /api/auth/login {client: "waiter"}; a miss
  shows the translated error without a reload.
- POST /api/auth/login-by-id removed; bootstrap login carries waiter_mode and
  waiter_require_username.
- Sign-out ends the session on the server (POST /api/auth/logout), so
  single_session_waiter = refuse_new no longer strands a waiter.
- Lock on the phone: menu "Lock", automatic lock / sign-out after inactivity
  (security.auto_lock*, auto_logout*), unlock with the PIN in the same session,
  lock kept across app restarts (restore() no longer clears it).
- e2e: new waiter.mjs (22 checks) in run-all; android.mjs signs in for real and
  locks/unlocks on the emulator; TLS live check waits for the socket; the
  rediscovery helper re-attaches to a stale WebView handle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 15:25:00 +02:00
co-authored by Claude Opus 5.5
parent 9f5498332a
commit 8299184e3a
14 changed files with 664 additions and 106 deletions
+165
View File
@@ -0,0 +1,165 @@
// Lock and idle sign-out on the phone (plan step 4.2), following the venue's settings
// security.auto_lock(_seconds) and security.auto_logout(_seconds), as the manager does.
//
// Locked: the screen is covered and the same waiter unlocks with their own PIN (or password,
// when the venue signs waiters in with passwords): POST /api/auth/unlock with the session's
// own token, so the session (and its token) simply continues. Five wrong tries end the
// session on the server (auth.session_ended) and the phone goes back to sign-in.
// "Sign out" is always offered, so a different waiter can take the phone.
//
// The last touch is kept per venue, so a phone left closed past the limits is locked (or
// signed out) when it is opened again.
import { useEffect, useRef, useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { useAccess } from '@xenia/access'
import { isError } from '@xenia/api'
import PinPad from './PinPad'
import useAuthStore from '../store/authStore'
import client from '../api/client'
import { apiError } from '../api/errors'
import { getVenueSettings } from '../api/venueSettings'
import { storageKey } from '../config/server'
import { idleAction, idleLimits } from '../logic/idle'
const ACTIVITY_EVENTS = ['pointerdown', 'keydown', 'touchstart', 'scroll']
const SAVE_EVERY_MS = 5_000
const CHECK_EVERY_MS = 1_000
function lastActiveKey() { return storageKey('lastActive') }
/** Watches for inactivity while someone is signed in; renders nothing. */
export function IdleGuard() {
const navigate = useNavigate()
const lastRef = useRef(null)
const limitsRef = useRef({ lockMs: Infinity, logoutMs: Infinity })
useEffect(() => {
let alive = true
getVenueSettings().then(r => { if (alive) limitsRef.current = idleLimits(r.data) }).catch(() => {})
const refresh = setInterval(() => {
getVenueSettings().then(r => { if (alive) limitsRef.current = idleLimits(r.data) }).catch(() => {})
}, 60_000)
const saved = parseInt(localStorage.getItem(lastActiveKey()) || '', 10)
lastRef.current = Number.isFinite(saved) ? saved : Date.now()
let lastSaved = 0
function onActivity() {
const now = Date.now()
lastRef.current = now
if (now - lastSaved > SAVE_EVERY_MS) {
lastSaved = now
try { localStorage.setItem(lastActiveKey(), String(now)) } catch { /* storage full */ }
}
}
ACTIVITY_EVENTS.forEach(ev => window.addEventListener(ev, onActivity, { passive: true }))
const tick = setInterval(() => {
const { token, locked, lock, logout } = useAuthStore.getState()
if (!token) return
const action = idleAction(Date.now() - lastRef.current, limitsRef.current, locked)
if (action === 'logout') {
logout()
navigate('/login', { replace: true })
} else if (action === 'lock') {
lock()
}
}, CHECK_EVERY_MS)
return () => {
alive = false
clearInterval(refresh)
clearInterval(tick)
ACTIVITY_EVENTS.forEach(ev => window.removeEventListener(ev, onActivity))
}
}, [navigate])
return null
}
/** Mark the phone as just used (after unlocking), so the guard doesn't lock it again at once. */
function touchActivity() {
try { localStorage.setItem(lastActiveKey(), String(Date.now())) } catch { /* storage full */ }
window.dispatchEvent(new Event('keydown'))
}
export default function LockScreen() {
const { user, savedUsername, unlock, logout } = useAuthStore()
const { login: modes } = useAccess()
const usePassword = modes?.waiter_mode === 'password'
const navigate = useNavigate()
const [error, setError] = useState('')
const [busy, setBusy] = useState(false)
const [password, setPassword] = useState('')
const [attempt, setAttempt] = useState(0)
function signOut() {
logout()
navigate('/login', { replace: true })
}
async function tryUnlock(secret) {
setBusy(true)
setError('')
try {
const { data } = await client.post('/api/auth/unlock', secret,
{ xenia: { ignoreUnauthorized: true, noOverride: true } })
touchActivity()
unlock(data.user)
} catch (err) {
if (isError(err, 'auth.session_ended')) { signOut(); return }
setError(apiError(err, usePassword ? 'Λανθασμένος κωδικός' : 'Λανθασμένο PIN'))
setPassword('')
setAttempt(a => a + 1)
} finally {
setBusy(false)
}
}
return (
<div
data-testid="lock-screen"
style={{
position: 'fixed', inset: 0, zIndex: 9999,
background: 'var(--bg)', display: 'flex', alignItems: 'center', justifyContent: 'center',
padding: 16,
}}
>
<div className="login-box" style={{ textAlign: 'center' }}>
<div style={{ fontSize: 40 }}>🔒</div>
<p style={{ fontSize: 18, fontWeight: 700, color: 'var(--text)', margin: '6px 0 2px' }}>Κλειδωμένο</p>
<p style={{ fontSize: 14, color: 'var(--muted)', marginBottom: 12 }}>{user?.full_name || user?.username || savedUsername}</p>
{usePassword ? (
<form onSubmit={e => { e.preventDefault(); if (password) tryUnlock({ password }) }} style={{ display: 'grid', gap: 10 }}>
<input
type="password"
value={password}
onChange={e => setPassword(e.target.value)}
placeholder="Κωδικός"
autoComplete="current-password"
data-testid="unlock-password"
style={{
width: '100%', boxSizing: 'border-box', padding: '12px 14px', borderRadius: 10,
border: '1px solid var(--border)', background: 'var(--bg2)', color: 'var(--text)', fontSize: 16,
}}
/>
<button type="submit" className="btn btn--primary btn--lg" disabled={busy || !password}>Ξεκλείδωμα</button>
</form>
) : (
<PinPad key={attempt} onSubmit={pin => tryUnlock({ pin })} loading={busy} />
)}
{error && <p className="error-msg" data-testid="unlock-error">{error}</p>}
<button
onClick={signOut}
data-testid="lock-sign-out"
style={{ marginTop: 16, background: 'none', border: 'none', color: 'var(--muted)', fontSize: 13, cursor: 'pointer' }}
>
Αποσύνδεση (άλλος σερβιτόρος)
</button>
</div>
</div>
)
}
+7 -1
View File
@@ -26,7 +26,7 @@ export default function UserMenu() {
const [busy, setBusy] = useState(false)
const ref = useRef(null)
const navigate = useNavigate()
const { user, logout } = useAuthStore()
const { user, logout, lock } = useAuthStore()
const { dark, toggle } = useThemeStore()
const {
shift, selfEndAllowed,
@@ -187,6 +187,12 @@ export default function UserMenu() {
<div className="user-menu-divider" />
{/* ── Lock (plan step 4.2): the same waiter unlocks with their PIN ── */}
<button className="user-menu-item" data-testid="menu-lock" onClick={() => { setOpen(false); lock() }}>
<span className="user-menu-item__icon">🔒</span>
<span>Κλείδωμα</span>
</button>
<button className="user-menu-item user-menu-item--danger" onClick={handleLogout}>
<span className="user-menu-item__icon">⏏</span>
<span>Αποσύνδεση</span>