2021-06-06 17:07:19 +02:00
|
|
|
import 'dart:async';
|
|
|
|
|
2020-04-12 10:35:45 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
|
|
|
|
import 'package:matrix/matrix.dart';
|
|
|
|
import 'package:vrouter/vrouter.dart';
|
2021-06-06 17:07:19 +02:00
|
|
|
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:fluffychat/config/app_config.dart';
|
2021-11-09 21:32:16 +01:00
|
|
|
import 'package:fluffychat/pages/homeserver_picker/homeserver_picker_view.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:fluffychat/widgets/matrix.dart';
|
2021-11-09 21:32:16 +01:00
|
|
|
import '../../utils/localized_exception_extension.dart';
|
2020-04-12 10:35:45 +02:00
|
|
|
|
2021-01-17 15:33:31 +01:00
|
|
|
class HomeserverPicker extends StatefulWidget {
|
2022-01-29 12:35:03 +01:00
|
|
|
const HomeserverPicker({Key? key}) : super(key: key);
|
2021-10-14 18:09:30 +02:00
|
|
|
|
2021-01-17 15:33:31 +01:00
|
|
|
@override
|
2021-04-09 16:28:26 +02:00
|
|
|
HomeserverPickerController createState() => HomeserverPickerController();
|
2021-01-17 15:33:31 +01:00
|
|
|
}
|
2020-04-12 10:35:45 +02:00
|
|
|
|
2021-04-09 16:28:26 +02:00
|
|
|
class HomeserverPickerController extends State<HomeserverPicker> {
|
|
|
|
bool isLoading = false;
|
|
|
|
final TextEditingController homeserverController =
|
2021-01-22 21:39:37 +01:00
|
|
|
TextEditingController(text: AppConfig.defaultHomeserver);
|
2022-01-29 12:35:03 +01:00
|
|
|
String? error;
|
2021-11-16 10:23:29 +01:00
|
|
|
|
2021-04-09 16:28:26 +02:00
|
|
|
/// Starts an analysis of the given homeserver. It uses the current domain and
|
|
|
|
/// makes sure that it is prefixed with https. Then it searches for the
|
|
|
|
/// well-known information and forwards to the login page depending on the
|
2021-06-11 10:08:04 +02:00
|
|
|
/// login type.
|
2021-10-16 09:59:38 +02:00
|
|
|
Future<void> checkHomeserverAction() async {
|
2021-11-16 10:23:29 +01:00
|
|
|
setState(() {
|
2022-04-15 11:42:59 +02:00
|
|
|
error = null;
|
2021-11-16 10:23:29 +01:00
|
|
|
isLoading = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
2022-04-15 11:42:59 +02:00
|
|
|
homeserverController.text =
|
|
|
|
homeserverController.text.trim().toLowerCase().replaceAll(' ', '-');
|
|
|
|
var homeserver = Uri.parse(homeserverController.text);
|
|
|
|
if (homeserver.scheme.isEmpty) {
|
|
|
|
homeserver = Uri.https(homeserverController.text, '');
|
|
|
|
}
|
|
|
|
final matrix = Matrix.of(context);
|
|
|
|
matrix.loginHomeserverSummary =
|
|
|
|
await matrix.getLoginClient().checkHomeserver(homeserver);
|
|
|
|
final ssoSupported = matrix.loginHomeserverSummary!.loginFlows
|
|
|
|
.any((flow) => flow.type == 'm.login.sso');
|
2021-11-15 12:14:00 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
await Matrix.of(context).getLoginClient().register();
|
2022-04-15 11:42:59 +02:00
|
|
|
matrix.loginRegistrationSupported = true;
|
2021-11-15 12:14:00 +01:00
|
|
|
} on MatrixException catch (e) {
|
2022-04-15 11:42:59 +02:00
|
|
|
matrix.loginRegistrationSupported = e.requireAdditionalAuthentication;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ssoSupported && matrix.loginRegistrationSupported == false) {
|
|
|
|
// Server does not support SSO or registration. We can skip to login page:
|
|
|
|
VRouter.of(context).to('login');
|
|
|
|
} else {
|
|
|
|
VRouter.of(context).to('connect');
|
2021-11-15 12:14:00 +01:00
|
|
|
}
|
2021-01-17 15:33:31 +01:00
|
|
|
} catch (e) {
|
2022-01-29 12:35:03 +01:00
|
|
|
setState(() => error = (e).toLocalizedString(context));
|
2021-01-17 15:33:31 +01:00
|
|
|
} finally {
|
|
|
|
if (mounted) {
|
2021-04-09 16:28:26 +02:00
|
|
|
setState(() => isLoading = false);
|
2021-01-17 15:33:31 +01:00
|
|
|
}
|
2020-04-12 10:35:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2021-05-23 13:28:55 +02:00
|
|
|
Widget build(BuildContext context) {
|
|
|
|
Matrix.of(context).navigatorContext = context;
|
|
|
|
return HomeserverPickerView(this);
|
|
|
|
}
|
2020-04-12 10:35:45 +02:00
|
|
|
}
|