feat(app): the native app signs in at the web login and carries the session on its socket and downloads

This commit is contained in:
2026-10-23 19:51:43 +01:00
parent d6fbea1665
commit 2fc0a4394b
13 changed files with 335 additions and 1 deletions
@@ -1,6 +1,7 @@
import 'dart:io';
import 'dart:typed_data';
import '../../../core/session/server_session.dart';
import 'artifact_viewer_fetch.dart';
/// Native fetch for an already resolved same-origin artifact.
@@ -12,6 +13,8 @@ Future<ArtifactFetchResult> fetchArtifact(Uri uri) async {
try {
final request = await client.getUrl(uri);
request.followRedirects = false;
// A phone behind the web login carries its kept session.
ServerSession.headers.forEach(request.headers.set);
final response = await request.close();
final bytes = BytesBuilder(copy: false);
await for (final chunk in response) {
@@ -13,6 +13,7 @@ import 'settings_layout.dart';
import 'settings_notifications_section.dart';
import 'settings_schedules_section.dart';
import 'settings_sections.dart';
import 'settings_sign_in_section.dart';
import 'settings_store.dart';
import 'settings_templates_section.dart';
@@ -35,10 +36,14 @@ class SettingsScreen extends StatefulWidget {
this.onOpenTemplates,
this.onOpenSchedules,
this.notifications,
this.showSignIn = false,
});
final transport.PipelinesClient client;
/// Whether to offer the web sign-in (the native app; a browser has its own).
final bool showSignIn;
/// A fixture/preview seed for the URL field. Persisted settings still load;
/// this value takes precedence for the draft and is never written by itself.
final Uri? initialEndpoint;
@@ -140,6 +145,20 @@ class _SettingsScreenState extends State<SettingsScreen> {
serverUrlField: _serverUrlField,
),
),
// A phone signs in at the web login; a browser has its own.
if (widget.showSignIn) ...<Widget>[
const SizedBox(height: settingsSectionGap),
_SettingsSection(
title: 'Sign in',
child: SettingsSignInSection(
endpoint: _controller.serverUrl,
onChanged: () {
final endpoint = widget.client.endpoint;
if (endpoint != null) widget.client.connect(endpoint);
},
),
),
],
const SizedBox(height: settingsSectionGap),
_SettingsSection(
title: 'Workspace',
@@ -0,0 +1,126 @@
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<SettingsSignInSection> createState() => _SettingsSignInSectionState();
}
class _SettingsSignInSectionState extends State<SettingsSignInSection> {
final _password = TextEditingController();
bool _busy = false;
String? _message;
@override
void dispose() {
_password.dispose();
super.dispose();
}
Future<void> _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<void> _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!)),
),
],
);
}
}