From b45899558c075bbdaccafa17becd8d3c02e6d2c7 Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Thu, 15 Aug 2024 08:52:14 -0400 Subject: [PATCH] fix --- android/app/src/main/AndroidManifest.xml | 1 + lib/controller/stillbox.dart | 25 ++++--- lib/controller/storage_none.dart | 9 +++ lib/controller/storage_secure.dart | 12 +++ lib/controller/storage_web.dart | 11 +++ lib/views/login.dart | 94 ++++++++++++------------ pubspec.lock | 6 +- pubspec.yaml | 3 + 8 files changed, 100 insertions(+), 61 deletions(-) create mode 100644 lib/controller/storage_none.dart create mode 100644 lib/controller/storage_secure.dart create mode 100644 lib/controller/storage_web.dart diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 122cf31..edf20eb 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -42,4 +42,5 @@ + diff --git a/lib/controller/stillbox.dart b/lib/controller/stillbox.dart index 2b4aa6c..0f08e75 100644 --- a/lib/controller/stillbox.dart +++ b/lib/controller/stillbox.dart @@ -1,14 +1,16 @@ -import 'package:flutter/material.dart'; +import 'package:flutter/foundation.dart'; import 'package:web_socket_channel/io.dart'; import 'package:http/http.dart' as http; -import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import '../pb/stillbox.pb.dart'; import 'play.dart'; +import 'storage_none.dart' + if (dart.library.io) 'storage_secure.dart' + if (dart.library.html) 'storage_web.dart'; class BadAuthException implements Exception {} class Stillbox extends ChangeNotifier { - final storage = const FlutterSecureStorage(); + final storage = Storer(); Player player = Player(); late IOWebSocketChannel channel; bool connected = false; @@ -62,10 +64,8 @@ class Stillbox extends ChangeNotifier { } Future doLogin(String uri, String username, String password) async { - if (baseUri == null) { - baseUri = Uri.parse(uri); - setUris(); - } + baseUri = Uri.parse(uri); + setUris(); String baseUriString = baseUri.toString(); // trim trailing slash since gordio router really dislikes it if (baseUriString.endsWith('/')) { @@ -81,8 +81,10 @@ class Stillbox extends ChangeNotifier { ); if (response.statusCode == 200) { updateCookie(response); - await storage.write(key: 'token', value: headers['cookie']); - await storage.write(key: 'baseURL', value: uri); + storage.setKey('baseURL', uri); + if (!kIsWeb) { + storage.setKey('token', headers['cookie']!); + } await connect(); return true; } @@ -104,8 +106,9 @@ class Stillbox extends ChangeNotifier { } Future getBearer() async { - String? storedToken = await storage.read(key: 'token'); - String? storedUri = await storage.read(key: 'baseURL'); + String? storedToken = await storage.getKey('token'); + late String? storedUri; + storedUri = await storage.getKey('baseURL'); if (storedToken == null || storedUri == null) { throw (BadAuthException); } diff --git a/lib/controller/storage_none.dart b/lib/controller/storage_none.dart new file mode 100644 index 0000000..ec3d078 --- /dev/null +++ b/lib/controller/storage_none.dart @@ -0,0 +1,9 @@ +class Storer { + Future getKey(String key) async { + return null; + } + + void setKey(String key, String value) { + return; + } +} diff --git a/lib/controller/storage_secure.dart b/lib/controller/storage_secure.dart new file mode 100644 index 0000000..06acd2e --- /dev/null +++ b/lib/controller/storage_secure.dart @@ -0,0 +1,12 @@ +import 'package:flutter_secure_storage/flutter_secure_storage.dart'; + +class Storer { + final storage = const FlutterSecureStorage(); + Future getKey(String key) async { + return await storage.read(key: 'token'); + } + + void setKey(String key, String value) async { + await storage.write(key: 'token', value: value); + } +} diff --git a/lib/controller/storage_web.dart b/lib/controller/storage_web.dart new file mode 100644 index 0000000..a0ef055 --- /dev/null +++ b/lib/controller/storage_web.dart @@ -0,0 +1,11 @@ +import 'dart:html'; + +class Storer { + Future getKey(String key) async { + return window.localStorage[key]; + } + + void setKey(String key, String value) { + window.localStorage[key] = value; + } +} diff --git a/lib/views/login.dart b/lib/views/login.dart index 114b9aa..7495cea 100644 --- a/lib/views/login.dart +++ b/lib/views/login.dart @@ -12,10 +12,18 @@ class Login extends StatefulWidget { class _LoginState extends State { final _formKey = GlobalKey(); - TextEditingController uriController = TextEditingController(); + late TextEditingController uriController; TextEditingController userController = TextEditingController(); TextEditingController passwordController = TextEditingController(); + _LoginState() { + if (Uri.base.scheme == 'http' || Uri.base.scheme == 'https') { + uriController = TextEditingController(text: Uri.base.toString()); + } else { + uriController = TextEditingController(); + } + } + @override Widget build(BuildContext context) { return Scaffold( @@ -29,29 +37,24 @@ class _LoginState extends State { padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 16), child: Column(children: [ Builder(builder: (context) { - if (!(Uri.base.scheme == 'http' || - Uri.base.scheme == 'https')) { - return Padding( - padding: const EdgeInsets.symmetric( - horizontal: 8, vertical: 10), - child: TextFormField( - controller: uriController, - decoration: const InputDecoration( - border: OutlineInputBorder(), - labelText: "Server URL"), - validator: (value) { - if (value != null) { - return Uri.parse(value).isAbsolute - ? null - : 'Please enter a valid URL.'; - } else { - return 'Please enter a valid URL.'; - } - }, - )); - } - - return Container(); + return Padding( + padding: const EdgeInsets.symmetric( + horizontal: 8, vertical: 10), + child: TextFormField( + controller: uriController, + decoration: const InputDecoration( + border: OutlineInputBorder(), + labelText: "Server URL"), + validator: (value) { + if (value != null) { + return Uri.parse(value).isAbsolute + ? null + : 'Please enter a valid URL.'; + } else { + return 'Please enter a valid URL.'; + } + }, + )); }), Padding( padding: @@ -87,33 +90,30 @@ class _LoginState extends State { horizontal: 8, vertical: 16.0), child: Center( child: ElevatedButton( - onPressed: () { + onPressed: () async { if (_formKey.currentState!.validate()) { - Provider.of(context, listen: false) + final result = await Provider.of(context, + listen: false) .doLogin( uriController.text, userController.text, - passwordController.text) - .then((result) { - if (result == true) { - if (context.mounted) { - Navigator.pushReplacement( - context, - PageRouteBuilder( - pageBuilder: (context, animation, - secondaryAnimation) => - const CallsHome(), - transitionsBuilder: (context, animation, - secondaryAnimation, child) { - return child; - }, - transitionDuration: - const Duration(milliseconds: 0), - ), - ); - } - } - }); + passwordController.text); + if (context.mounted && result == true) { + Navigator.pushReplacement( + context, + PageRouteBuilder( + pageBuilder: (context, animation, + secondaryAnimation) => + const CallsHome(), + transitionsBuilder: (context, animation, + secondaryAnimation, child) { + return child; + }, + transitionDuration: + const Duration(milliseconds: 0), + ), + ); + } } else { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Please login.')), diff --git a/pubspec.lock b/pubspec.lock index 3a58e1d..24d194f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -329,7 +329,7 @@ packages: source: hosted version: "1.1.4" media_kit_libs_linux: - dependency: transitive + dependency: "direct main" description: name: media_kit_libs_linux sha256: e186891c31daa6bedab4d74dcdb4e8adfccc7d786bfed6ad81fe24a3b3010310 @@ -345,7 +345,7 @@ packages: source: hosted version: "1.1.4" media_kit_libs_windows_audio: - dependency: transitive + dependency: "direct main" description: name: media_kit_libs_windows_audio sha256: c2fd558cc87b9d89a801141fcdffe02e338a3b21a41a18fbd63d5b221a1b8e53 @@ -353,7 +353,7 @@ packages: source: hosted version: "1.0.9" media_kit_native_event_loop: - dependency: transitive + dependency: "direct main" description: name: media_kit_native_event_loop sha256: a605cf185499d14d58935b8784955a92a4bf0ff4e19a23de3d17a9106303930e diff --git a/pubspec.yaml b/pubspec.yaml index b34927a..fdb44b4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -46,6 +46,9 @@ dependencies: just_audio_media_kit: ^2.0.5 media_kit_libs_audio: ^1.0.4 patch_package: ^0.0.8 + media_kit_libs_linux: ^1.1.3 + media_kit_native_event_loop: ^1.0.8 + media_kit_libs_windows_audio: ^1.0.9 dev_dependencies: flutter_test: