Zenith/lib/login.dart

121 lines
3.8 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:matrix/matrix.dart';
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';
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 = '';
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 {
await client.checkHomeserver(Uri.parse(serverController.text));
2023-12-30 11:16:38 +13:00
await client.login(
LoginType.mLoginPassword,
password: passwordController.text,
identifier: AuthenticationUserIdentifier(user: usernameController.text),
2023-12-30 11:16:38 +13:00
refreshToken: true,
);
2023-12-29 14:50:00 +13:00
} catch (error) {
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-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);
if (!mounted) return;
Navigator.pushAndRemoveUntil(
context,
2023-12-29 10:40:09 +13:00
MaterialPageRoute(builder: (context) => const RoomsPage()),
(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-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,
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(
onPressed: loggingIn ? null : login,
2023-12-29 15:12:10 +13:00
tooltip: 'Log in',
child: loggingIn
? const CircularProgressIndicator()
: const Icon(Icons.login),
)),
);
}
}