mobile: modo "conectar a mi servidor" (emparejamiento por codigo, sin passkeys)
La app movil puede ahora sincronizar de verdad contra un servidor openGym propio en vez de guardar todo solo en el telefono. Como las passkeys no funcionan desde el WebView de la app (su origen nunca coincide con el hostname real), el emparejamiento se hace con un codigo de un solo uso generado desde una sesion ya logueada en el navegador (Ajustes > "Emparejar la app movil"), canjeado por un bearer token con la misma firma que ya usa la cookie de sesion. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ec84f6db06
commit
781e2441ba
22 files changed
+662
-29
No files matched your search
@@ -0,0 +1,59 @@
|
||||
// Mobile build only, first launch: the choice useStore.boot() couldn't make on its own — keep
|
||||
// everything on this device, or connect to a self-hosted openGym server instead. See
|
||||
// lib/remote.js for the pairing flow this hands off to.
|
||||
import { useState, useRef, useEffect } from 'react'
|
||||
import { useStore } from '../store/useStore.js'
|
||||
import { useUI } from '../store/useUI.js'
|
||||
import { t } from '../lib/i18n.js'
|
||||
import Icon from '../components/Icon.jsx'
|
||||
import { Button } from '../components/ui.jsx'
|
||||
|
||||
export function ConnectSheet({ close }) {
|
||||
const { connectToServer } = useStore()
|
||||
const [url, setUrl] = useState('')
|
||||
const [code, setCode] = useState('')
|
||||
const [busy, setBusy] = useState(false)
|
||||
const ref = useRef(null)
|
||||
useEffect(() => { setTimeout(() => ref.current?.focus(), 250) }, [])
|
||||
const go = async () => {
|
||||
if (!url.trim() || !code.trim()) { useUI.getState().toast(t('Enter your server address and the code')); return }
|
||||
setBusy(true)
|
||||
try { await connectToServer(url.trim(), code.trim()); close(); useUI.getState().toast(t('Connected')) }
|
||||
catch (e) { useUI.getState().toast(e.message || t('Could not connect')) }
|
||||
finally { setBusy(false) }
|
||||
}
|
||||
return <>
|
||||
<h3>{t('Connect to my server')}</h3>
|
||||
<div className="muted small" style={{ marginBottom: 14 }}>
|
||||
{t('Open Settings → “Pair the mobile app” on the openGym site you’re already signed into, then enter its address and the code shown there.')}
|
||||
</div>
|
||||
<input ref={ref} className="input" placeholder={t('Server address (e.g. gym.example.com)')} value={url}
|
||||
onChange={e => setUrl(e.target.value)} autoCapitalize="none" autoCorrect="off" inputMode="url" />
|
||||
<div style={{ height: 10 }} />
|
||||
<input className="input" placeholder={t('Pairing code')} maxLength={8} value={code}
|
||||
onChange={e => setCode(e.target.value.toUpperCase())} style={{ letterSpacing: '.14em', fontWeight: 600, textAlign: 'center' }} />
|
||||
<div style={{ height: 12 }} />
|
||||
<Button variant="primary" onClick={go} disabled={busy}>{busy ? t('Connecting…') : t('Connect')}</Button>
|
||||
</>
|
||||
}
|
||||
|
||||
export default function MobileOnboarding() {
|
||||
const { chooseLocalMode } = useStore()
|
||||
const head = <>
|
||||
<div style={{ fontSize: 54, display: 'flex', justifyContent: 'center', color: 'var(--acc)' }}><Icon name="dumbbell" /></div>
|
||||
<h1 style={{ fontSize: 34, fontWeight: 700, letterSpacing: '-.028em', margin: '10px 0 4px' }}>openGym</h1>
|
||||
</>
|
||||
const wrap = { display: 'flex', flexDirection: 'column', justifyContent: 'center', minHeight: '78vh', textAlign: 'center' }
|
||||
return (
|
||||
<div className="narrow" style={wrap}>
|
||||
{head}
|
||||
<div className="muted" style={{ marginBottom: 34 }}>{t('How do you want to use openGym?')}</div>
|
||||
<Button variant="primary" icon="lock" onClick={() => chooseLocalMode()}>{t('Use on this device')}</Button>
|
||||
<div style={{ height: 10 }} />
|
||||
<Button icon="rocket" onClick={() => useUI.getState().openSheet(close => <ConnectSheet close={close} />)}>{t('Connect to my server')}</Button>
|
||||
<div className="dim small" style={{ marginTop: 26, lineHeight: 1.5 }}>
|
||||
{t('Local keeps everything on this phone. Connecting syncs to your own openGym server instead — you can switch later in Settings.')}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import { wakeLockSupported } from '../lib/wakelock.js'
|
||||
import { t, LANGS, INSTR_LANGS } from '../lib/i18n.js'
|
||||
import { DEMO, REPO } from '../lib/demo.js'
|
||||
import { MOBILE, shareExport, syncReminder } from '../lib/mobile.js'
|
||||
import { ConnectSheet } from './MobileOnboarding.jsx'
|
||||
import { loadStarterPlan, confirmSheet, importFromApp } from '../sheets.jsx'
|
||||
import Icon from '../components/Icon.jsx'
|
||||
import { Section, Row, SelectRow, Switch, Segmented, Button, TextField } from '../components/ui.jsx'
|
||||
@@ -18,7 +19,7 @@ export default function Settings() {
|
||||
const nav = useNavigate()
|
||||
const S = useStore(s => s.S)
|
||||
const user = useStore(s => s.user)
|
||||
const { update, replaceState, setUser, pullState, pushState, signOut, signOutAll, resetDemo } = useStore()
|
||||
const { update, replaceState, setUser, pullState, pushState, signOut, signOutAll, resetDemo, disconnectServer } = useStore()
|
||||
const toast = useUI(s => s.toast)
|
||||
const fileRef = useRef(null)
|
||||
const importRef = useRef(null)
|
||||
@@ -73,12 +74,21 @@ export default function Settings() {
|
||||
</div>
|
||||
|
||||
{/* ---------- account (demo and mobile builds have nothing to sign in to) ---------- */}
|
||||
<Section title={MOBILE ? t('Your data') : DEMO ? t('Demo') : t('Account')}>
|
||||
{MOBILE ? <>
|
||||
<Section title={MOBILE ? (user ? t('Your server') : t('Your data')) : DEMO ? t('Demo') : t('Account')}>
|
||||
{MOBILE ? (user ? <>
|
||||
<Row icon="personCircle" iconTint="var(--grey)" title={user.name} subtitle={t('Synced with your openGym server.')} />
|
||||
{user.admin && <Row icon="wrench" iconTint="var(--indigo)" title={t('Admin dashboard')} accessory="chevron" onClick={() => nav('/admin')} />}
|
||||
<Row icon="signOut" iconTint="var(--red)" title={t('Disconnect')} danger onClick={() => confirmSheet({
|
||||
title: t('Disconnect from your server?'),
|
||||
message: t('Your data is synced to your server first, then this device switches back to local-only.'),
|
||||
confirmText: t('Disconnect'), danger: true,
|
||||
onConfirm: async () => { await disconnectServer(); nav('/home'); toast(t('Disconnected — back to local-only')) },
|
||||
})} />
|
||||
</> : <>
|
||||
<Row icon="lock" iconTint="var(--acc)" title={t('All data stays on this phone')} subtitle={t('No account, no cloud — back it up anytime with Export below.')} />
|
||||
<Row icon="rocket" iconTint="var(--indigo)" title={t('Self-host openGym')} subtitle={t('Passkey sign-in, sync across your devices, your own data.')} accessory="chevron"
|
||||
onClick={() => window.open(REPO, '_blank', 'noopener')} />
|
||||
</> : DEMO ? <>
|
||||
<Row icon="link" iconTint="var(--indigo)" title={t('Connect to my server')} subtitle={t('Sync this device to your own self-hosted openGym instead.')} accessory="chevron"
|
||||
onClick={() => useUI.getState().openSheet(close => <ConnectSheet close={close} />)} />
|
||||
</>) : DEMO ? <>
|
||||
<Row icon="sparkles" iconTint="var(--acc)" title={t('You’re in the demo')} subtitle={t('Example data, stored only in this browser — change anything you like.')} />
|
||||
<Row icon="reset" iconTint="var(--blue)" title={t('Reset demo data')} accessory="chevron"
|
||||
onClick={() => confirmSheet({ title: t('Reset demo data?'), message: t('Puts the example plan, workouts and weigh-ins back the way they started.'), confirmText: t('Reset'), onConfirm: () => { resetDemo(); nav('/home'); toast(t('Demo data reset')) } })} />
|
||||
@@ -87,6 +97,8 @@ export default function Settings() {
|
||||
</> : user ? <>
|
||||
<Row icon="personCircle" iconTint="var(--grey)" title={user.name} subtitle={t('Signed in with passkey — data syncs to this profile.')} />
|
||||
{user.admin && <Row icon="wrench" iconTint="var(--indigo)" title={t('Admin dashboard')} accessory="chevron" onClick={() => nav('/admin')} />}
|
||||
<Row icon="link" iconTint="var(--blue)" title={t('Pair the mobile app')} subtitle={t('Connect the openGym app on your phone to this account.')} accessory="chevron"
|
||||
onClick={() => useUI.getState().openSheet(close => <PairSheet close={close} />)} />
|
||||
<Row icon="signOut" iconTint="var(--red)" title={t('Sign out')} danger onClick={() => confirmSheet({ title: t('Sign out?'), message: t('Your data is synced to your profile first, then cleared from this device.'), confirmText: t('Sign out'), danger: true, onConfirm: () => { signOut(); nav('/home') } })} />
|
||||
<Row icon="shield" iconTint="var(--red)" title={t('Sign out everywhere')} subtitle={t('Ends this profile’s sessions on all your devices.')} danger onClick={signOutEverywhere} />
|
||||
</> : webauthnOK() ? <>
|
||||
@@ -342,6 +354,28 @@ function PushCard({ S, update, toast }) {
|
||||
</>
|
||||
}
|
||||
|
||||
// Lets the mobile app's "connect to my server" mode (lib/remote.js) authenticate without a
|
||||
// WebAuthn ceremony of its own — the code is minted here, from an already signed-in session,
|
||||
// and redeemed by the app for a bearer token. See /api/pair/create in api/server.js.
|
||||
function PairSheet({ close }) {
|
||||
const [code, setCode] = useState(null)
|
||||
const [err, setErr] = useState(null)
|
||||
useEffect(() => { api('/api/pair/create', { method: 'POST', body: '{}' }).then(r => setCode(r.code)).catch(e => setErr(e.message || t('Could not generate a code'))) }, [])
|
||||
return <>
|
||||
<h3>{t('Pair the mobile app')}</h3>
|
||||
<div className="muted small" style={{ marginBottom: 14 }}>
|
||||
{t('On the openGym app, choose “Connect to my server”, then enter this address and the code below. It expires in 5 minutes.')}
|
||||
</div>
|
||||
{err ? <div className="dim small">{err}</div> : (
|
||||
<div className="card" style={{ textAlign: 'center', fontSize: 30, fontWeight: 700, letterSpacing: '.16em', padding: '18px 0' }}>
|
||||
{code || '········'}
|
||||
</div>
|
||||
)}
|
||||
<div style={{ height: 12 }} />
|
||||
<Button onClick={close}>{t('Done')}</Button>
|
||||
</>
|
||||
}
|
||||
|
||||
// The same registration as the sign-in screen's, reached from Settings instead. It asks for
|
||||
// the invite code on the same terms: an invite-only instance rejects a registration without
|
||||
// one, so a form that cannot collect it is a form that cannot succeed.
|
||||
|
||||
Reference in new issue
Block a user