2020-05-16 08:43:27 +02:00
|
|
|
import 'dart:async';
|
2020-01-01 19:10:13 +01:00
|
|
|
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2021-05-23 13:11:55 +02:00
|
|
|
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
|
|
|
import 'package:email_validator/email_validator.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2020-12-25 09:58:34 +01:00
|
|
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:matrix/matrix.dart';
|
2021-04-03 13:09:20 +02:00
|
|
|
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:fluffychat/widgets/matrix.dart';
|
2021-11-09 21:32:16 +01:00
|
|
|
import '../../config/app_config.dart';
|
|
|
|
import '../../config/setting_keys.dart';
|
|
|
|
import '../../utils/platform_infos.dart';
|
|
|
|
import 'login_view.dart';
|
2021-05-22 09:08:23 +02:00
|
|
|
|
2020-01-09 22:52:27 +01:00
|
|
|
class Login extends StatefulWidget {
|
2022-01-29 12:35:03 +01:00
|
|
|
const Login({Key? key}) : super(key: key);
|
2021-10-14 18:09:30 +02:00
|
|
|
|
2020-01-01 19:10:13 +01:00
|
|
|
@override
|
2021-05-22 09:08:23 +02:00
|
|
|
LoginController createState() => LoginController();
|
2020-01-01 19:10:13 +01:00
|
|
|
}
|
|
|
|
|
2021-05-22 09:08:23 +02:00
|
|
|
class LoginController extends State<Login> {
|
2020-01-01 19:10:13 +01:00
|
|
|
final TextEditingController usernameController = TextEditingController();
|
|
|
|
final TextEditingController passwordController = TextEditingController();
|
2022-01-29 12:35:03 +01:00
|
|
|
String? usernameError;
|
|
|
|
String? passwordError;
|
2020-01-04 09:16:29 +01:00
|
|
|
bool loading = false;
|
2020-01-09 22:52:27 +01:00
|
|
|
bool showPassword = false;
|
2020-01-01 19:10:13 +01:00
|
|
|
|
2021-05-22 09:08:23 +02:00
|
|
|
void toggleShowPassword() => setState(() => showPassword = !showPassword);
|
|
|
|
|
|
|
|
void login([_]) async {
|
2021-04-14 10:37:15 +02:00
|
|
|
final matrix = Matrix.of(context);
|
2020-01-01 19:10:13 +01:00
|
|
|
if (usernameController.text.isEmpty) {
|
2022-01-29 12:35:03 +01:00
|
|
|
setState(() => usernameError = L10n.of(context)!.pleaseEnterYourUsername);
|
2020-01-01 19:10:13 +01:00
|
|
|
} else {
|
|
|
|
setState(() => usernameError = null);
|
|
|
|
}
|
|
|
|
if (passwordController.text.isEmpty) {
|
2022-01-29 12:35:03 +01:00
|
|
|
setState(() => passwordError = L10n.of(context)!.pleaseEnterYourPassword);
|
2020-01-01 19:10:13 +01:00
|
|
|
} else {
|
|
|
|
setState(() => passwordError = null);
|
|
|
|
}
|
|
|
|
|
2020-01-02 22:31:39 +01:00
|
|
|
if (usernameController.text.isEmpty || passwordController.text.isEmpty) {
|
2020-01-01 19:10:13 +01:00
|
|
|
return;
|
2020-01-02 22:31:39 +01:00
|
|
|
}
|
2020-01-01 19:10:13 +01:00
|
|
|
|
2020-04-12 10:35:45 +02:00
|
|
|
setState(() => loading = true);
|
2020-01-01 19:10:13 +01:00
|
|
|
try {
|
2021-03-09 19:39:25 +01:00
|
|
|
final username = usernameController.text;
|
|
|
|
AuthenticationIdentifier identifier;
|
|
|
|
if (username.isEmail) {
|
|
|
|
identifier = AuthenticationThirdPartyIdentifier(
|
|
|
|
medium: 'email',
|
|
|
|
address: username,
|
|
|
|
);
|
|
|
|
} else if (username.isPhoneNumber) {
|
|
|
|
identifier = AuthenticationThirdPartyIdentifier(
|
|
|
|
medium: 'msisdn',
|
|
|
|
address: username,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
identifier = AuthenticationUserIdentifier(user: username);
|
|
|
|
}
|
2021-09-19 13:48:23 +02:00
|
|
|
await matrix.getLoginClient().login(LoginType.mLoginPassword,
|
2021-03-09 19:39:25 +01:00
|
|
|
identifier: identifier,
|
|
|
|
// To stay compatible with older server versions
|
|
|
|
// ignore: deprecated_member_use
|
|
|
|
user: identifier.type == AuthenticationIdentifierTypes.userId
|
|
|
|
? username
|
|
|
|
: null,
|
2020-08-16 12:54:43 +02:00
|
|
|
password: passwordController.text,
|
2021-02-07 17:18:38 +01:00
|
|
|
initialDeviceDisplayName: PlatformInfos.clientName);
|
2020-01-01 19:10:13 +01:00
|
|
|
} on MatrixException catch (exception) {
|
|
|
|
setState(() => passwordError = exception.errorMessage);
|
2020-01-04 09:16:29 +01:00
|
|
|
return setState(() => loading = false);
|
2020-01-01 19:10:13 +01:00
|
|
|
} catch (exception) {
|
|
|
|
setState(() => passwordError = exception.toString());
|
2020-01-04 09:16:29 +01:00
|
|
|
return setState(() => loading = false);
|
2020-01-01 19:10:13 +01:00
|
|
|
}
|
2020-11-24 15:49:27 +01:00
|
|
|
|
2021-01-16 12:46:38 +01:00
|
|
|
if (mounted) setState(() => loading = false);
|
2020-01-01 19:10:13 +01:00
|
|
|
}
|
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
Timer? _coolDown;
|
2020-05-16 08:43:27 +02:00
|
|
|
|
2021-05-22 09:08:23 +02:00
|
|
|
void checkWellKnownWithCoolDown(String userId) async {
|
2020-05-16 08:43:27 +02:00
|
|
|
_coolDown?.cancel();
|
|
|
|
_coolDown = Timer(
|
2021-10-14 18:09:30 +02:00
|
|
|
const Duration(seconds: 1),
|
2021-05-22 09:08:23 +02:00
|
|
|
() => _checkWellKnown(userId),
|
2020-05-16 08:43:27 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-22 09:08:23 +02:00
|
|
|
void _checkWellKnown(String userId) async {
|
2020-05-16 08:43:27 +02:00
|
|
|
setState(() => usernameError = null);
|
|
|
|
if (!userId.isValidMatrixId) return;
|
|
|
|
try {
|
2021-09-19 13:48:23 +02:00
|
|
|
final oldHomeserver = Matrix.of(context).getLoginClient().homeserver;
|
2022-01-29 12:35:03 +01:00
|
|
|
var newDomain = Uri.https(userId.domain!, '');
|
2021-09-19 13:48:23 +02:00
|
|
|
Matrix.of(context).getLoginClient().homeserver = newDomain;
|
2022-01-29 12:35:03 +01:00
|
|
|
DiscoveryInformation? wellKnownInformation;
|
2021-08-18 17:50:06 +02:00
|
|
|
try {
|
2021-09-19 13:48:23 +02:00
|
|
|
wellKnownInformation =
|
|
|
|
await Matrix.of(context).getLoginClient().getWellknown();
|
2022-01-29 12:35:03 +01:00
|
|
|
if (wellKnownInformation.mHomeserver.baseUrl.toString().isNotEmpty) {
|
2021-08-18 17:50:06 +02:00
|
|
|
newDomain = wellKnownInformation.mHomeserver.baseUrl;
|
2021-07-11 12:41:20 +02:00
|
|
|
}
|
2021-08-18 17:50:06 +02:00
|
|
|
} catch (_) {
|
|
|
|
// do nothing, newDomain is already set to a reasonable fallback
|
|
|
|
}
|
|
|
|
if (newDomain != oldHomeserver) {
|
2020-12-25 09:58:34 +01:00
|
|
|
await showFutureLoadingDialog(
|
|
|
|
context: context,
|
2021-08-08 08:46:19 +02:00
|
|
|
// do nothing if we error, we'll handle it below
|
|
|
|
future: () => Matrix.of(context)
|
2021-09-20 09:02:04 +02:00
|
|
|
.getLoginClient()
|
2021-08-08 08:46:19 +02:00
|
|
|
.checkHomeserver(newDomain)
|
2022-02-15 12:47:22 +01:00
|
|
|
.catchError((e) {}),
|
2020-12-25 09:58:34 +01:00
|
|
|
);
|
2021-09-19 13:48:23 +02:00
|
|
|
if (Matrix.of(context).getLoginClient().homeserver == null) {
|
|
|
|
Matrix.of(context).getLoginClient().homeserver = oldHomeserver;
|
2021-08-08 08:46:19 +02:00
|
|
|
// okay, the server we checked does not appear to be a matrix server
|
|
|
|
Logs().v(
|
|
|
|
'$newDomain is not running a homeserver, asking to use $oldHomeserver');
|
|
|
|
final dialogResult = await showOkCancelAlertDialog(
|
|
|
|
context: context,
|
|
|
|
useRootNavigator: false,
|
2022-01-29 12:35:03 +01:00
|
|
|
message:
|
|
|
|
L10n.of(context)!.noMatrixServer(newDomain, oldHomeserver!),
|
|
|
|
okLabel: L10n.of(context)!.ok,
|
|
|
|
cancelLabel: L10n.of(context)!.cancel,
|
2021-08-08 08:46:19 +02:00
|
|
|
);
|
|
|
|
if (dialogResult == OkCancelResult.ok) {
|
|
|
|
setState(() => usernameError = null);
|
|
|
|
} else {
|
|
|
|
Navigator.of(context, rootNavigator: false).pop();
|
2021-08-18 17:50:06 +02:00
|
|
|
return;
|
2021-08-08 08:46:19 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-18 17:50:06 +02:00
|
|
|
var jitsi = wellKnownInformation?.additionalProperties
|
2022-01-29 12:35:03 +01:00
|
|
|
.tryGet<Map<String, dynamic>>('im.vector.riot.jitsi')
|
2021-08-18 17:50:06 +02:00
|
|
|
?.tryGet<String>('preferredDomain');
|
|
|
|
if (jitsi != null) {
|
|
|
|
if (!jitsi.endsWith('/')) {
|
|
|
|
jitsi += '/';
|
|
|
|
}
|
|
|
|
Logs().v('Found custom jitsi instance $jitsi');
|
|
|
|
await Matrix.of(context)
|
|
|
|
.store
|
|
|
|
.setItem(SettingKeys.jitsiInstance, jitsi);
|
|
|
|
AppConfig.jitsiInstance = jitsi;
|
|
|
|
}
|
2020-05-16 08:43:27 +02:00
|
|
|
setState(() => usernameError = null);
|
2021-12-04 19:54:07 +01:00
|
|
|
} else {
|
|
|
|
setState(() =>
|
|
|
|
Matrix.of(context).getLoginClient().homeserver = oldHomeserver);
|
2020-05-16 08:43:27 +02:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
setState(() => usernameError = e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 09:08:23 +02:00
|
|
|
void passwordForgotten() async {
|
2020-11-24 14:27:07 +01:00
|
|
|
final input = await showTextInputDialog(
|
2021-05-23 15:02:36 +02:00
|
|
|
useRootNavigator: false,
|
2020-11-24 14:27:07 +01:00
|
|
|
context: context,
|
2022-01-29 12:35:03 +01:00
|
|
|
title: L10n.of(context)!.enterAnEmailAddress,
|
|
|
|
okLabel: L10n.of(context)!.ok,
|
|
|
|
cancelLabel: L10n.of(context)!.cancel,
|
2020-11-24 14:27:07 +01:00
|
|
|
textFields: [
|
|
|
|
DialogTextField(
|
2022-01-29 12:35:03 +01:00
|
|
|
hintText: L10n.of(context)!.enterAnEmailAddress,
|
2020-11-24 14:27:07 +01:00
|
|
|
keyboardType: TextInputType.emailAddress,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
if (input == null) return;
|
2021-09-19 13:08:33 +02:00
|
|
|
final clientSecret =
|
|
|
|
Matrix.of(context).client.generateUniqueTransactionId();
|
2020-12-25 09:58:34 +01:00
|
|
|
final response = await showFutureLoadingDialog(
|
|
|
|
context: context,
|
2021-09-19 13:48:23 +02:00
|
|
|
future: () =>
|
|
|
|
Matrix.of(context).getLoginClient().requestTokenToResetPasswordEmail(
|
|
|
|
clientSecret,
|
|
|
|
input.single,
|
|
|
|
sendAttempt++,
|
|
|
|
),
|
2020-11-24 14:27:07 +01:00
|
|
|
);
|
2020-12-25 09:58:34 +01:00
|
|
|
if (response.error != null) return;
|
2020-11-24 14:27:07 +01:00
|
|
|
final ok = await showOkAlertDialog(
|
2021-05-23 15:02:36 +02:00
|
|
|
useRootNavigator: false,
|
2020-11-24 14:27:07 +01:00
|
|
|
context: context,
|
2022-01-29 12:35:03 +01:00
|
|
|
title: L10n.of(context)!.weSentYouAnEmail,
|
|
|
|
message: L10n.of(context)!.pleaseClickOnLink,
|
|
|
|
okLabel: L10n.of(context)!.iHaveClickedOnLink,
|
2020-11-24 14:27:07 +01:00
|
|
|
);
|
2022-01-29 12:35:03 +01:00
|
|
|
if (ok != OkCancelResult.ok) return;
|
2020-11-24 14:27:07 +01:00
|
|
|
final password = await showTextInputDialog(
|
2021-05-23 15:02:36 +02:00
|
|
|
useRootNavigator: false,
|
2020-11-24 14:27:07 +01:00
|
|
|
context: context,
|
2022-01-29 12:35:03 +01:00
|
|
|
title: L10n.of(context)!.chooseAStrongPassword,
|
|
|
|
okLabel: L10n.of(context)!.ok,
|
|
|
|
cancelLabel: L10n.of(context)!.cancel,
|
2020-11-24 14:27:07 +01:00
|
|
|
textFields: [
|
2021-10-14 18:09:30 +02:00
|
|
|
const DialogTextField(
|
2020-11-24 14:27:07 +01:00
|
|
|
hintText: '******',
|
|
|
|
obscureText: true,
|
2020-12-10 15:06:02 +01:00
|
|
|
minLines: 1,
|
|
|
|
maxLines: 1,
|
2020-11-24 14:27:07 +01:00
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
if (password == null) return;
|
2020-12-25 09:58:34 +01:00
|
|
|
final success = await showFutureLoadingDialog(
|
|
|
|
context: context,
|
2021-09-19 13:48:23 +02:00
|
|
|
future: () => Matrix.of(context).getLoginClient().changePassword(
|
2020-12-11 10:27:38 +01:00
|
|
|
password.single,
|
|
|
|
auth: AuthenticationThreePidCreds(
|
|
|
|
type: AuthenticationTypes.emailIdentity,
|
2021-11-04 16:09:12 +01:00
|
|
|
threepidCreds: ThreepidCreds(
|
2022-01-29 12:35:03 +01:00
|
|
|
sid: response.result!.sid,
|
2021-11-04 16:09:12 +01:00
|
|
|
clientSecret: clientSecret,
|
|
|
|
),
|
2020-12-11 10:27:38 +01:00
|
|
|
),
|
|
|
|
),
|
2020-11-24 14:27:07 +01:00
|
|
|
);
|
2020-12-25 09:58:34 +01:00
|
|
|
if (success.error == null) {
|
2021-05-23 13:11:55 +02:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
2022-01-29 12:35:03 +01:00
|
|
|
SnackBar(content: Text(L10n.of(context)!.passwordHasBeenChanged)));
|
2020-11-24 14:27:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sendAttempt = 0;
|
|
|
|
|
2020-01-01 19:10:13 +01:00
|
|
|
@override
|
2021-05-22 09:08:23 +02:00
|
|
|
Widget build(BuildContext context) => LoginView(this);
|
2020-01-01 19:10:13 +01:00
|
|
|
}
|
2021-03-09 19:39:25 +01:00
|
|
|
|
|
|
|
extension on String {
|
|
|
|
static final RegExp _phoneRegex =
|
|
|
|
RegExp(r'^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$');
|
2021-03-10 11:11:30 +01:00
|
|
|
bool get isEmail => EmailValidator.validate(this);
|
2021-03-09 19:39:25 +01:00
|
|
|
bool get isPhoneNumber => _phoneRegex.hasMatch(this);
|
|
|
|
}
|