You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
348 lines
11 KiB
Dart
348 lines
11 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import 'package:matrix/matrix.dart';
|
|
|
|
import 'package:fluffychat/l10n/l10n.dart';
|
|
import 'package:fluffychat/pangea/common/controllers/pangea_controller.dart';
|
|
import 'package:fluffychat/pangea/login/pages/pangea_login_view.dart';
|
|
import 'package:fluffychat/pangea/login/widgets/p_sso_button.dart';
|
|
import 'package:fluffychat/pangea/user/utils/p_login.dart';
|
|
import 'package:fluffychat/utils/localized_exception_extension.dart';
|
|
import 'package:fluffychat/widgets/adaptive_dialogs/show_ok_cancel_alert_dialog.dart';
|
|
import 'package:fluffychat/widgets/adaptive_dialogs/show_text_input_dialog.dart';
|
|
import 'package:fluffychat/widgets/future_loading_dialog.dart';
|
|
import 'package:fluffychat/widgets/matrix.dart';
|
|
|
|
class Login extends StatefulWidget {
|
|
// #Pangea
|
|
// final Client client;
|
|
// const Login({required this.client, super.key});
|
|
|
|
const Login({super.key});
|
|
// Pangea#
|
|
|
|
@override
|
|
LoginController createState() => LoginController();
|
|
}
|
|
|
|
class LoginController extends State<Login> {
|
|
final TextEditingController usernameController = TextEditingController();
|
|
final TextEditingController passwordController = TextEditingController();
|
|
String? usernameError;
|
|
String? passwordError;
|
|
bool loading = false;
|
|
bool showPassword = false;
|
|
|
|
void toggleShowPassword() =>
|
|
setState(() => showPassword = !loading && !showPassword);
|
|
|
|
// #Pangea
|
|
bool loadingSignIn = false;
|
|
bool loadingAppleSSO = false;
|
|
bool loadingGoogleSSO = false;
|
|
|
|
final PangeaController pangeaController = MatrixState.pangeaController;
|
|
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
|
|
|
|
Client? client;
|
|
|
|
bool get enabledSignIn =>
|
|
!loadingSignIn &&
|
|
usernameController.text.isNotEmpty &&
|
|
passwordController.text.isNotEmpty;
|
|
|
|
@override
|
|
void initState() {
|
|
// TODO: implement initState
|
|
super.initState();
|
|
loadingSignIn = true;
|
|
pangeaController.checkHomeServerAction().then((value) {
|
|
setState(() {
|
|
loadingSignIn = false;
|
|
});
|
|
}).catchError((e) {
|
|
final String err = e.toString();
|
|
setState(() {
|
|
loadingSignIn = false;
|
|
passwordError = err.toLocalizedString(context);
|
|
});
|
|
});
|
|
|
|
usernameController.addListener(() => setState(() {}));
|
|
passwordController.addListener(() => setState(() {}));
|
|
|
|
Matrix.of(context).getLoginClient().then((client) {
|
|
if (mounted) setState(() => this.client = client);
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
usernameController.dispose();
|
|
passwordController.dispose();
|
|
loadingSignIn = false;
|
|
usernameError = null;
|
|
passwordError = null;
|
|
super.dispose();
|
|
}
|
|
|
|
void setLoadingSignIn(bool loading) {
|
|
if (mounted) {
|
|
setState(() => loadingSignIn = loading);
|
|
}
|
|
}
|
|
|
|
void setLoadingSSO(bool loading, SSOProvider provider) {
|
|
if (provider == SSOProvider.apple) {
|
|
loadingAppleSSO = loading;
|
|
loadingGoogleSSO = false;
|
|
} else if (provider == SSOProvider.google) {
|
|
loadingGoogleSSO = loading;
|
|
loadingAppleSSO = false;
|
|
}
|
|
if (mounted) setState(() {});
|
|
}
|
|
|
|
void login() async => pLoginAction(controller: this, context: context);
|
|
// void login() async {
|
|
// final matrix = Matrix.of(context);
|
|
// if (usernameController.text.isEmpty) {
|
|
// setState(() => usernameError = L10n.of(context).pleaseEnterYourUsername);
|
|
// } else {
|
|
// setState(() => usernameError = null);
|
|
// }
|
|
// if (passwordController.text.isEmpty) {
|
|
// setState(() => passwordError = L10n.of(context).pleaseEnterYourPassword);
|
|
// } else {
|
|
// setState(() => passwordError = null);
|
|
// }
|
|
|
|
// if (usernameController.text.isEmpty || passwordController.text.isEmpty) {
|
|
// return;
|
|
// }
|
|
|
|
// setState(() => loading = true);
|
|
|
|
// _coolDown?.cancel();
|
|
|
|
// try {
|
|
// 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);
|
|
// }
|
|
// final client = await matrix.getLoginClient();
|
|
// client.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,
|
|
// );
|
|
// } on MatrixException catch (exception) {
|
|
// setState(() => passwordError = exception.errorMessage);
|
|
// return setState(() => loading = false);
|
|
// } catch (exception) {
|
|
// setState(() => passwordError = exception.toString());
|
|
// return setState(() => loading = false);
|
|
// }
|
|
|
|
// if (mounted) setState(() => loading = false);
|
|
// }
|
|
|
|
// Timer? _coolDown;
|
|
|
|
// void checkWellKnownWithCoolDown(String userId) async {
|
|
// _coolDown?.cancel();
|
|
// _coolDown = Timer(
|
|
// const Duration(seconds: 1),
|
|
// () => _checkWellKnown(userId),
|
|
// );
|
|
// }
|
|
|
|
// void _checkWellKnown(String userId) async {
|
|
// if (mounted) setState(() => usernameError = null);
|
|
// if (!userId.isValidMatrixId) return;
|
|
// final oldHomeserver = widget.client.homeserver;
|
|
// try {
|
|
// var newDomain = Uri.https(userId.domain!, '');
|
|
// widget.client.homeserver = newDomain;
|
|
// DiscoveryInformation? wellKnownInformation;
|
|
// try {
|
|
// wellKnownInformation = await widget.client.getWellknown();
|
|
// if (wellKnownInformation.mHomeserver.baseUrl.toString().isNotEmpty) {
|
|
// newDomain = wellKnownInformation.mHomeserver.baseUrl;
|
|
// }
|
|
// } catch (_) {
|
|
// // do nothing, newDomain is already set to a reasonable fallback
|
|
// }
|
|
// if (newDomain != oldHomeserver) {
|
|
// await widget.client.checkHomeserver(newDomain);
|
|
|
|
// if (widget.client.homeserver == null) {
|
|
// widget.client.homeserver = oldHomeserver;
|
|
// // 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,
|
|
// title: L10n.of(context)
|
|
// .noMatrixServer(newDomain.toString(), oldHomeserver.toString()),
|
|
// okLabel: L10n.of(context).ok,
|
|
// cancelLabel: L10n.of(context).cancel,
|
|
// );
|
|
// if (dialogResult == OkCancelResult.ok) {
|
|
// if (mounted) setState(() => usernameError = null);
|
|
// } else {
|
|
// Navigator.of(context, rootNavigator: false).pop();
|
|
// return;
|
|
// }
|
|
// }
|
|
// usernameError = null;
|
|
// if (mounted) setState(() {});
|
|
// } else {
|
|
// widget.client.homeserver = oldHomeserver;
|
|
// if (mounted) {
|
|
// setState(() {});
|
|
// }
|
|
// }
|
|
// } catch (e) {
|
|
// widget.client.homeserver = oldHomeserver;
|
|
// usernameError = e.toLocalizedString(context);
|
|
// if (mounted) setState(() {});
|
|
// }
|
|
// }
|
|
// Pangea#
|
|
|
|
void passwordForgotten() async {
|
|
// #Pangea
|
|
if (client == null) return;
|
|
// Pangea#
|
|
final input = await showTextInputDialog(
|
|
useRootNavigator: false,
|
|
context: context,
|
|
title: L10n.of(context).passwordForgotten,
|
|
message: L10n.of(context).enterAnEmailAddress,
|
|
okLabel: L10n.of(context).ok,
|
|
cancelLabel: L10n.of(context).cancel,
|
|
initialText:
|
|
usernameController.text.isEmail ? usernameController.text : '',
|
|
hintText: L10n.of(context).enterAnEmailAddress,
|
|
keyboardType: TextInputType.emailAddress,
|
|
);
|
|
if (input == null) return;
|
|
final clientSecret = DateTime.now().millisecondsSinceEpoch.toString();
|
|
final response = await showFutureLoadingDialog(
|
|
context: context,
|
|
// #Pangea
|
|
// future: () => widget.client.requestTokenToResetPasswordEmail(
|
|
// clientSecret,
|
|
// input,
|
|
// sendAttempt++,
|
|
// ),
|
|
future: () => client!.requestTokenToResetPasswordEmail(
|
|
clientSecret,
|
|
input,
|
|
sendAttempt++,
|
|
),
|
|
// Pangea#
|
|
);
|
|
if (response.error != null) return;
|
|
final password = await showTextInputDialog(
|
|
useRootNavigator: false,
|
|
context: context,
|
|
title: L10n.of(context).passwordForgotten,
|
|
message: L10n.of(context).chooseAStrongPassword,
|
|
okLabel: L10n.of(context).ok,
|
|
cancelLabel: L10n.of(context).cancel,
|
|
hintText: '******',
|
|
obscureText: true,
|
|
minLines: 1,
|
|
maxLines: 1,
|
|
);
|
|
if (password == null) return;
|
|
final ok = await showOkAlertDialog(
|
|
useRootNavigator: false,
|
|
context: context,
|
|
title: L10n.of(context).weSentYouAnEmail,
|
|
// #Pangea
|
|
// message: L10n.of(context).pleaseClickOnLink,
|
|
message: L10n.of(context).clickOnEmailLink,
|
|
// Pangea#
|
|
okLabel: L10n.of(context).iHaveClickedOnLink,
|
|
);
|
|
if (ok != OkCancelResult.ok) return;
|
|
final data = <String, dynamic>{
|
|
'new_password': password,
|
|
'logout_devices': false,
|
|
"auth": AuthenticationThreePidCreds(
|
|
type: AuthenticationTypes.emailIdentity,
|
|
threepidCreds: ThreepidCreds(
|
|
sid: response.result!.sid,
|
|
clientSecret: clientSecret,
|
|
),
|
|
).toJson(),
|
|
};
|
|
final success = await showFutureLoadingDialog(
|
|
context: context,
|
|
// #Pangea
|
|
// future: () => widget.client.request(
|
|
// RequestType.POST,
|
|
// '/client/v3/account/password',
|
|
// data: data,
|
|
// ),
|
|
future: () => client!.request(
|
|
RequestType.POST,
|
|
'/client/v3/account/password',
|
|
data: data,
|
|
),
|
|
// Pangea#
|
|
);
|
|
if (success.error == null) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text(L10n.of(context).passwordHasBeenChanged)),
|
|
);
|
|
usernameController.text = input;
|
|
passwordController.text = password;
|
|
login();
|
|
}
|
|
}
|
|
|
|
static int sendAttempt = 0;
|
|
|
|
@override
|
|
// #Pangea
|
|
// Widget build(BuildContext context) => LoginView(this);
|
|
Widget build(BuildContext context) => PangeaLoginView(this);
|
|
// Pangea#
|
|
}
|
|
|
|
// #Pangea
|
|
// extension on String {
|
|
extension LoginExtension on String {
|
|
// Pangea#
|
|
static final RegExp _phoneRegex =
|
|
RegExp(r'^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$');
|
|
static final RegExp _emailRegex = RegExp(r'(.+)@(.+)\.(.+)');
|
|
|
|
bool get isEmail => _emailRegex.hasMatch(this);
|
|
|
|
bool get isPhoneNumber => _phoneRegex.hasMatch(this);
|
|
}
|