Support new local connect auth

This commit is contained in:
2025-11-30 14:51:12 +08:00
parent e759d5f46c
commit 4a7ff96a8b
3 changed files with 241 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'web_auth_server.dart';
class WebAuthServerState {
final bool isRunning;
final int? port;
final Object? error;
WebAuthServerState({this.isRunning = false, this.port, this.error});
WebAuthServerState copyWith({
bool? isRunning,
int? port,
Object? error,
bool clearError = false,
}) {
return WebAuthServerState(
isRunning: isRunning ?? this.isRunning,
port: port ?? this.port,
error: clearError ? null : error ?? this.error,
);
}
}
class WebAuthServerNotifier extends StateNotifier<WebAuthServerState> {
final WebAuthServer _server;
WebAuthServerNotifier(this._server) : super(WebAuthServerState());
Future<void> start() async {
try {
final port = await _server.start();
state = state.copyWith(isRunning: true, port: port, clearError: true);
} catch (e) {
state = state.copyWith(isRunning: false, error: e);
}
}
void stop() {
_server.stop();
state = state.copyWith(isRunning: false, port: null);
}
}
final webAuthServerProvider = Provider<WebAuthServer>((ref) {
return WebAuthServer(ref);
});
final webAuthServerStateProvider =
StateNotifierProvider<WebAuthServerNotifier, WebAuthServerState>((ref) {
final server = ref.watch(webAuthServerProvider);
return WebAuthServerNotifier(server);
});