Files
companion/lib/data/sources/local/credential_store.dart
T
2026-05-07 19:51:17 +02:00

56 lines
1.8 KiB
Dart

import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import '../../../domain/models/server_config.dart';
typedef Credentials = ({String username, String password});
class CredentialStore {
static const _usernameKey = 'localiserd_username';
static const _passwordKey = 'localiserd_password';
static const _hostKey = 'localiserd_host';
static const _portKey = 'localiserd_port';
final _storage = const FlutterSecureStorage();
Future<void> save(Credentials credentials) => Future.wait([
_storage.write(key: _usernameKey, value: credentials.username),
_storage.write(key: _passwordKey, value: credentials.password),
]).then((_) {});
Future<Credentials?> load() async {
final results = await Future.wait([
_storage.read(key: _usernameKey),
_storage.read(key: _passwordKey),
]);
final username = results[0];
final password = results[1];
if (username == null || password == null) return null;
return (username: username, password: password);
}
Future<void> saveServer(ServerConfig config) => Future.wait([
_storage.write(key: _hostKey, value: config.host),
_storage.write(key: _portKey, value: config.port.toString()),
]).then((_) {});
Future<ServerConfig?> loadServer() async {
final results = await Future.wait([
_storage.read(key: _hostKey),
_storage.read(key: _portKey),
]);
final host = results[0];
final portStr = results[1];
if (host == null || portStr == null) return null;
final port = int.tryParse(portStr);
if (port == null) return null;
return ServerConfig(host: host, port: port);
}
Future<void> clear() => Future.wait([
_storage.delete(key: _usernameKey),
_storage.delete(key: _passwordKey),
_storage.delete(key: _hostKey),
_storage.delete(key: _portKey),
]).then((_) {});
}