2023-12-28 18:53:21 +13:00
|
|
|
import 'package:flutter/material.dart';
|
2023-12-29 19:31:35 +13:00
|
|
|
import 'package:matrix/matrix.dart';
|
2023-12-28 18:53:21 +13:00
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2023-12-29 10:40:09 +13:00
|
|
|
import 'package:zenith/rooms.dart';
|
2023-12-28 18:53:21 +13:00
|
|
|
import 'package:zenith/zenith_client_provider.dart';
|
|
|
|
|
|
|
|
class LoginPage extends StatefulWidget {
|
|
|
|
const LoginPage({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<LoginPage> createState() => _LoginPageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _LoginPageState extends State<LoginPage> {
|
|
|
|
final serverController = TextEditingController();
|
|
|
|
final usernameController = TextEditingController();
|
|
|
|
final passwordController = TextEditingController();
|
2023-12-29 14:50:00 +13:00
|
|
|
bool loggingIn = false;
|
|
|
|
String failedMessage = '';
|
2023-12-28 18:53:21 +13:00
|
|
|
|
2023-12-29 19:31:35 +13:00
|
|
|
void login() async {
|
2023-12-29 14:50:00 +13:00
|
|
|
setState(() {
|
|
|
|
loggingIn = true;
|
|
|
|
});
|
|
|
|
|
2023-12-30 11:16:38 +13:00
|
|
|
final provider = Provider.of<ZenithClientProvider>(context, listen: false);
|
|
|
|
final client = Client("zenith");
|
|
|
|
|
2023-12-29 14:50:00 +13:00
|
|
|
try {
|
2023-12-29 19:31:35 +13:00
|
|
|
await client.checkHomeserver(Uri.parse(serverController.text));
|
2023-12-30 11:16:38 +13:00
|
|
|
await client.login(
|
2023-12-29 19:31:35 +13:00
|
|
|
LoginType.mLoginPassword,
|
|
|
|
password: passwordController.text,
|
|
|
|
identifier: AuthenticationUserIdentifier(user: usernameController.text),
|
2023-12-30 11:16:38 +13:00
|
|
|
refreshToken: true,
|
2023-12-29 19:31:35 +13:00
|
|
|
);
|
2023-12-29 14:50:00 +13:00
|
|
|
} catch (error) {
|
2023-12-29 19:31:35 +13:00
|
|
|
print(error);
|
2023-12-29 14:50:00 +13:00
|
|
|
setState(() {
|
|
|
|
failedMessage = error.toString();
|
2023-12-29 15:12:10 +13:00
|
|
|
loggingIn = false;
|
2023-12-29 14:50:00 +13:00
|
|
|
});
|
|
|
|
|
|
|
|
Future.delayed(const Duration(seconds: 10), () {
|
|
|
|
setState(() {
|
|
|
|
failedMessage = '';
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
} finally {
|
|
|
|
setState(() {
|
|
|
|
loggingIn = false;
|
|
|
|
});
|
|
|
|
}
|
2023-12-28 18:53:21 +13:00
|
|
|
|
2023-12-30 11:16:38 +13:00
|
|
|
provider.setClient(client);
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
prefs.setString("homeserver", serverController.text);
|
|
|
|
prefs.setString("username", usernameController.text);
|
|
|
|
prefs.setString("password", passwordController.text);
|
|
|
|
|
2023-12-28 18:53:21 +13:00
|
|
|
if (!mounted) return;
|
|
|
|
Navigator.pushAndRemoveUntil(
|
|
|
|
context,
|
2023-12-29 10:40:09 +13:00
|
|
|
MaterialPageRoute(builder: (context) => const RoomsPage()),
|
2023-12-28 18:53:21 +13:00
|
|
|
(route) => false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-12-29 14:50:00 +13:00
|
|
|
return Consumer<ZenithClientProvider>(
|
|
|
|
builder: (context, provider, child) => Scaffold(
|
2023-12-29 15:12:10 +13:00
|
|
|
appBar: AppBar(
|
|
|
|
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
|
|
|
title: const Text("Login"),
|
2023-12-28 18:53:21 +13:00
|
|
|
),
|
2023-12-29 15:12:10 +13:00
|
|
|
body: Padding(
|
|
|
|
padding: const EdgeInsets.all(16.0),
|
|
|
|
child: Center(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
TextFormField(
|
|
|
|
controller: serverController,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
labelText: 'Server', hintText: 'https://matrix.org'),
|
|
|
|
),
|
|
|
|
TextFormField(
|
|
|
|
controller: usernameController,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
labelText: 'Username', hintText: 'john'),
|
|
|
|
),
|
|
|
|
TextFormField(
|
|
|
|
controller: passwordController,
|
|
|
|
decoration: const InputDecoration(labelText: 'Password'),
|
|
|
|
obscureText: true,
|
2023-12-29 19:31:35 +13:00
|
|
|
onFieldSubmitted: (value) => login(),
|
2023-12-29 15:12:10 +13:00
|
|
|
),
|
|
|
|
Text(failedMessage,
|
|
|
|
style: Theme.of(context).textTheme.headlineSmall)
|
|
|
|
],
|
2023-12-29 14:50:00 +13:00
|
|
|
),
|
2023-12-29 15:12:10 +13:00
|
|
|
),
|
|
|
|
),
|
|
|
|
floatingActionButton: FloatingActionButton(
|
2023-12-29 19:31:35 +13:00
|
|
|
onPressed: loggingIn ? null : login,
|
2023-12-29 15:12:10 +13:00
|
|
|
tooltip: 'Log in',
|
|
|
|
child: loggingIn
|
|
|
|
? const CircularProgressIndicator()
|
|
|
|
: const Icon(Icons.login),
|
|
|
|
)),
|
2023-12-28 18:53:21 +13:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|