feat(app): phone onboarding (server, then password) kept across launches; a known server opens straight into the app; a refused session asks for the password
This commit is contained in:
@@ -61,8 +61,23 @@ class ServerSession {
|
||||
}
|
||||
|
||||
static Future<void> signOut() => _save(null);
|
||||
|
||||
/// Whether [endpoint]'s server accepts the kept session.
|
||||
static Future<SessionState> check(Uri endpoint) async {
|
||||
final login = loginUriFor(endpoint);
|
||||
if (login == null) return SessionState.unreachable;
|
||||
final status = await impl.probeLogin(login, headers);
|
||||
if (status == null) return SessionState.unreachable;
|
||||
// The web login sends a signed-in visitor on; it shows its form otherwise.
|
||||
if (status >= 300 && status < 400) return SessionState.signedIn;
|
||||
if (status == 200) return SessionState.signedOut;
|
||||
return SessionState.unreachable;
|
||||
}
|
||||
}
|
||||
|
||||
/// What a server says about this device's session.
|
||||
enum SessionState { signedIn, signedOut, unreachable }
|
||||
|
||||
/// The web login page for a websocket endpoint: same host and port, http(s).
|
||||
Uri? loginUriFor(Uri endpoint) {
|
||||
final scheme = switch (endpoint.scheme) {
|
||||
|
||||
@@ -40,3 +40,22 @@ Future<({String? cookie, String? error})> postLogin(
|
||||
client.close(force: true);
|
||||
}
|
||||
}
|
||||
|
||||
/// The status the web login answers with this session: a redirect away from
|
||||
/// the login page when signed in, the page itself when not; null when the
|
||||
/// server cannot be reached.
|
||||
Future<int?> probeLogin(Uri login, Map<String, String> headers) async {
|
||||
final client = HttpClient()..connectionTimeout = const Duration(seconds: 8);
|
||||
try {
|
||||
final request = await client.getUrl(login);
|
||||
request.followRedirects = false;
|
||||
headers.forEach(request.headers.set);
|
||||
final response = await request.close().timeout(const Duration(seconds: 10));
|
||||
await response.drain<void>();
|
||||
return response.statusCode;
|
||||
} on Exception {
|
||||
return null;
|
||||
} finally {
|
||||
client.close(force: true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,3 +3,6 @@ Future<({String? cookie, String? error})> postLogin(
|
||||
Uri login,
|
||||
String password,
|
||||
) async => (cookie: null, error: 'Sign in through the browser page.');
|
||||
|
||||
/// A browser page is already behind its own login.
|
||||
Future<int?> probeLogin(Uri login, Map<String, String> headers) async => 302;
|
||||
|
||||
Reference in New Issue
Block a user