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