import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../core/session/server_session.dart'; import '../../ui/adaptive/host.dart'; /// Signs the phone in at the server's web login with the same password the /// browser uses, so the app can reach a server behind it (over Tailscale, for /// example). A server without a login needs no sign-in. class SettingsSignInSection extends StatefulWidget { const SettingsSignInSection({ super.key, required this.endpoint, required this.onChanged, }); /// The server's websocket address; its `/login` sits beside it. final String endpoint; /// Reconnects once the session changed. final VoidCallback onChanged; @override State createState() => _SettingsSignInSectionState(); } class _SettingsSignInSectionState extends State { final _password = TextEditingController(); bool _busy = false; String? _message; @override void dispose() { _password.dispose(); super.dispose(); } Future _signIn() async { final endpoint = Uri.tryParse(widget.endpoint.trim()); if (endpoint == null) { setState(() => _message = 'Set the server address first.'); return; } setState(() { _busy = true; _message = null; }); final error = await ServerSession.signIn(endpoint, _password.text); if (!mounted) return; setState(() { _busy = false; _message = error ?? 'Signed in.'; if (error == null) _password.clear(); }); if (error == null) widget.onChanged(); } Future _signOut() async { await ServerSession.signOut(); if (!mounted) return; setState(() => _message = 'Signed out.'); widget.onChanged(); } @override Widget build(BuildContext context) { final cupertino = usesCupertinoHost(context); final signedIn = ServerSession.cookie != null; final field = cupertino ? CupertinoTextField( key: const Key('settings-sign-in-password'), controller: _password, obscureText: true, placeholder: 'Web password', onSubmitted: (_) => _signIn(), ) : TextField( key: const Key('settings-sign-in-password'), controller: _password, obscureText: true, decoration: const InputDecoration(labelText: 'Web password'), onSubmitted: (_) => _signIn(), ); final signIn = cupertino ? CupertinoButton.filled( key: const Key('settings-sign-in'), onPressed: _busy ? null : _signIn, child: const Text('Sign in'), ) : FilledButton( key: const Key('settings-sign-in'), onPressed: _busy ? null : _signIn, child: const Text('Sign in'), ); final signOut = cupertino ? CupertinoButton( key: const Key('settings-sign-out'), onPressed: _signOut, child: const Text('Sign out'), ) : TextButton( key: const Key('settings-sign-out'), onPressed: _signOut, child: const Text('Sign out'), ); return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Text( signedIn ? 'Signed in to the server\'s web login.' : 'Only needed when the server sits behind the web login.', ), const SizedBox(height: 8), field, const SizedBox(height: 8), Wrap(spacing: 8, children: [signIn, if (signedIn) signOut]), if (_message != null) Padding( padding: const EdgeInsets.only(top: 4), child: Semantics(liveRegion: true, child: Text(_message!)), ), ], ); } }