This commit is contained in:
Daniel 2024-08-15 08:52:14 -04:00
parent c21a0f2021
commit b45899558c
8 changed files with 100 additions and 61 deletions

View file

@ -42,4 +42,5 @@
<data android:mimeType="text/plain"/>
</intent>
</queries>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>

View file

@ -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<bool> 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<void> 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);
}

View file

@ -0,0 +1,9 @@
class Storer {
Future<String?> getKey(String key) async {
return null;
}
void setKey(String key, String value) {
return;
}
}

View file

@ -0,0 +1,12 @@
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
class Storer {
final storage = const FlutterSecureStorage();
Future<String?> getKey(String key) async {
return await storage.read(key: 'token');
}
void setKey(String key, String value) async {
await storage.write(key: 'token', value: value);
}
}

View file

@ -0,0 +1,11 @@
import 'dart:html';
class Storer {
Future<String?> getKey(String key) async {
return window.localStorage[key];
}
void setKey(String key, String value) {
window.localStorage[key] = value;
}
}

View file

@ -12,10 +12,18 @@ class Login extends StatefulWidget {
class _LoginState extends State<Login> {
final _formKey = GlobalKey<FormState>();
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<Login> {
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<Login> {
horizontal: 8, vertical: 16.0),
child: Center(
child: ElevatedButton(
onPressed: () {
onPressed: () async {
if (_formKey.currentState!.validate()) {
Provider.of<Stillbox>(context, listen: false)
final result = await Provider.of<Stillbox>(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.')),

View file

@ -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

View file

@ -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: