handle authentication failure in auth screen
All checks were successful
BlockStorage/repertory/pipeline/head This commit looks good

This commit is contained in:
2025-08-15 12:40:27 -05:00
parent 14998bf952
commit 224bf37e68
2 changed files with 43 additions and 21 deletions

View File

@@ -17,7 +17,8 @@ class Auth with ChangeNotifier {
bool get authenticated => _authenticated; bool get authenticated => _authenticated;
SecureKey get key => _key; SecureKey get key => _key;
Future<void> authenticate(String user, String password) async { Future<bool> authenticate(String user, String password) async {
try {
final sodium = constants.sodium; final sodium = constants.sodium;
final keyHash = sodium.crypto.genericHash( final keyHash = sodium.crypto.genericHash(
@@ -25,11 +26,28 @@ class Auth with ChangeNotifier {
message: Uint8List.fromList(password.toCharArray()), message: Uint8List.fromList(password.toCharArray()),
); );
_authenticated = true;
_key = SecureKey.fromList(sodium, keyHash); _key = SecureKey.fromList(sodium, keyHash);
_user = user; _user = user;
final auth = await createAuth();
final response = await http.put(
Uri.parse(
Uri.encodeFull('${getBaseUri()}/api/v1/locations?auth=$auth'),
),
);
_authenticated = (response.statusCode == 200);
if (_authenticated) {
notifyListeners(); notifyListeners();
return _authenticated;
}
} catch (e) {
debugPrint('$e');
}
logoff();
return false;
} }
Future<String> createAuth() async { Future<String> createAuth() async {

View File

@@ -57,22 +57,26 @@ class _AuthScreenState extends State<AuthScreen> {
} }
setState(() => _enabled = false); setState(() => _enabled = false);
await auth.authenticate( var authenticated = await auth.authenticate(
_userController.text.trim(), _userController.text.trim(),
_passwordController.text, _passwordController.text,
); );
setState(() => _enabled = true); setState(() => _enabled = true);
// if (!mounted) return; if (authenticated) {
// return;
// if (!(ok == true || auth.authenticated)) { }
// ScaffoldMessenger.of(context).showSnackBar(
// const SnackBar( if (!context.mounted) {
// content: Text('Invalid username or password'), return;
// behavior: SnackBarBehavior.floating, }
// ),
// ); ScaffoldMessenger.of(context).showSnackBar(
// } const SnackBar(
content: Text('Invalid username or password'),
behavior: SnackBarBehavior.floating,
),
);
} }
void navigateHomePostFrame() { void navigateHomePostFrame() {