diff --git a/web/repertory/lib/models/auth.dart b/web/repertory/lib/models/auth.dart index 6d383caf..94ebf939 100644 --- a/web/repertory/lib/models/auth.dart +++ b/web/repertory/lib/models/auth.dart @@ -17,19 +17,37 @@ class Auth with ChangeNotifier { bool get authenticated => _authenticated; SecureKey get key => _key; - Future authenticate(String user, String password) async { - final sodium = constants.sodium; + Future authenticate(String user, String password) async { + try { + final sodium = constants.sodium; - final keyHash = sodium.crypto.genericHash( - outLen: sodium.crypto.aeadXChaCha20Poly1305IETF.keyBytes, - message: Uint8List.fromList(password.toCharArray()), - ); + final keyHash = sodium.crypto.genericHash( + outLen: sodium.crypto.aeadXChaCha20Poly1305IETF.keyBytes, + message: Uint8List.fromList(password.toCharArray()), + ); - _authenticated = true; - _key = SecureKey.fromList(sodium, keyHash); - _user = user; + _key = SecureKey.fromList(sodium, keyHash); + _user = user; - notifyListeners(); + 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(); + + return _authenticated; + } + } catch (e) { + debugPrint('$e'); + } + + logoff(); + return false; } Future createAuth() async { diff --git a/web/repertory/lib/screens/auth_screen.dart b/web/repertory/lib/screens/auth_screen.dart index eac69998..61730c48 100644 --- a/web/repertory/lib/screens/auth_screen.dart +++ b/web/repertory/lib/screens/auth_screen.dart @@ -57,22 +57,26 @@ class _AuthScreenState extends State { } setState(() => _enabled = false); - await auth.authenticate( + var authenticated = await auth.authenticate( _userController.text.trim(), _passwordController.text, ); setState(() => _enabled = true); - // if (!mounted) return; - // - // if (!(ok == true || auth.authenticated)) { - // ScaffoldMessenger.of(context).showSnackBar( - // const SnackBar( - // content: Text('Invalid username or password'), - // behavior: SnackBarBehavior.floating, - // ), - // ); - // } + if (authenticated) { + return; + } + + if (!context.mounted) { + return; + } + + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar( + content: Text('Invalid username or password'), + behavior: SnackBarBehavior.floating, + ), + ); } void navigateHomePostFrame() {