2020-05-16 08:43:27 +02:00
|
|
|
import 'dart:async';
|
2020-01-01 19:10:13 +01:00
|
|
|
|
2020-11-24 14:27:07 +01:00
|
|
|
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
2021-04-03 13:09:20 +02:00
|
|
|
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
|
2020-01-01 19:10:13 +01:00
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
2021-05-22 08:53:52 +02:00
|
|
|
import 'package:fluffychat/widgets/layouts/one_page_card.dart';
|
2020-12-25 09:58:34 +01:00
|
|
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
2021-05-22 08:53:52 +02:00
|
|
|
import 'package:fluffychat/widgets/matrix.dart';
|
2021-04-03 13:09:20 +02:00
|
|
|
|
2020-01-01 19:10:13 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2020-10-03 13:11:07 +02:00
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2021-04-15 13:03:14 +02:00
|
|
|
import '../../utils/platform_infos.dart';
|
2021-03-10 11:11:30 +01:00
|
|
|
import 'package:email_validator/email_validator.dart';
|
2020-01-01 19:10:13 +01:00
|
|
|
|
2020-01-09 22:52:27 +01:00
|
|
|
class Login extends StatefulWidget {
|
2020-01-01 19:10:13 +01:00
|
|
|
@override
|
2020-01-09 22:52:27 +01:00
|
|
|
_LoginState createState() => _LoginState();
|
2020-01-01 19:10:13 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 22:52:27 +01:00
|
|
|
class _LoginState extends State<Login> {
|
2020-01-01 19:10:13 +01:00
|
|
|
final TextEditingController usernameController = TextEditingController();
|
|
|
|
final TextEditingController passwordController = TextEditingController();
|
|
|
|
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
|
|
|
|
|
|
|
void login(BuildContext context) 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) {
|
2020-05-07 07:52:40 +02:00
|
|
|
setState(() => usernameError = L10n.of(context).pleaseEnterYourUsername);
|
2020-01-01 19:10:13 +01:00
|
|
|
} else {
|
|
|
|
setState(() => usernameError = null);
|
|
|
|
}
|
|
|
|
if (passwordController.text.isEmpty) {
|
2020-05-07 07:52:40 +02: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);
|
|
|
|
}
|
2020-01-09 22:52:27 +01:00
|
|
|
await matrix.client.login(
|
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
|
|
|
}
|
|
|
|
|
2020-05-16 08:43:27 +02:00
|
|
|
Timer _coolDown;
|
|
|
|
|
|
|
|
void _checkWellKnownWithCoolDown(String userId, BuildContext context) async {
|
|
|
|
_coolDown?.cancel();
|
|
|
|
_coolDown = Timer(
|
|
|
|
Duration(seconds: 1),
|
|
|
|
() => _checkWellKnown(userId, context),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _checkWellKnown(String userId, BuildContext context) async {
|
|
|
|
setState(() => usernameError = null);
|
|
|
|
if (!userId.isValidMatrixId) return;
|
|
|
|
try {
|
|
|
|
final wellKnownInformations = await Matrix.of(context)
|
|
|
|
.client
|
|
|
|
.getWellKnownInformationsByUserId(userId);
|
|
|
|
final newDomain = wellKnownInformations.mHomeserver?.baseUrl;
|
|
|
|
if ((newDomain?.isNotEmpty ?? false) &&
|
2020-08-16 12:54:43 +02:00
|
|
|
newDomain != Matrix.of(context).client.homeserver.toString()) {
|
2020-12-25 09:58:34 +01:00
|
|
|
await showFutureLoadingDialog(
|
|
|
|
context: context,
|
|
|
|
future: () => Matrix.of(context).client.checkHomeserver(newDomain),
|
|
|
|
);
|
2020-05-16 08:43:27 +02:00
|
|
|
setState(() => usernameError = null);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
setState(() => usernameError = e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 14:27:07 +01:00
|
|
|
void _passwordForgotten(BuildContext context) async {
|
|
|
|
final input = await showTextInputDialog(
|
|
|
|
context: context,
|
|
|
|
title: L10n.of(context).enterAnEmailAddress,
|
2021-02-23 11:03:54 +01:00
|
|
|
okLabel: L10n.of(context).ok,
|
|
|
|
cancelLabel: L10n.of(context).cancel,
|
2021-02-24 12:17:23 +01:00
|
|
|
useRootNavigator: false,
|
2020-11-24 14:27:07 +01:00
|
|
|
textFields: [
|
|
|
|
DialogTextField(
|
|
|
|
hintText: L10n.of(context).enterAnEmailAddress,
|
|
|
|
keyboardType: TextInputType.emailAddress,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
if (input == null) return;
|
|
|
|
final clientSecret = DateTime.now().millisecondsSinceEpoch.toString();
|
2020-12-25 09:58:34 +01:00
|
|
|
final response = await showFutureLoadingDialog(
|
|
|
|
context: context,
|
|
|
|
future: () => Matrix.of(context).client.resetPasswordUsingEmail(
|
2020-11-24 14:27:07 +01:00
|
|
|
input.single,
|
|
|
|
clientSecret,
|
|
|
|
sendAttempt++,
|
|
|
|
),
|
|
|
|
);
|
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(
|
|
|
|
context: context,
|
|
|
|
title: L10n.of(context).weSentYouAnEmail,
|
|
|
|
message: L10n.of(context).pleaseClickOnLink,
|
|
|
|
okLabel: L10n.of(context).iHaveClickedOnLink,
|
2021-02-24 12:17:23 +01:00
|
|
|
useRootNavigator: false,
|
2020-11-24 14:27:07 +01:00
|
|
|
);
|
|
|
|
if (ok == null) return;
|
|
|
|
final password = await showTextInputDialog(
|
|
|
|
context: context,
|
|
|
|
title: L10n.of(context).chooseAStrongPassword,
|
2021-02-23 11:03:54 +01:00
|
|
|
okLabel: L10n.of(context).ok,
|
|
|
|
cancelLabel: L10n.of(context).cancel,
|
2021-02-24 12:17:23 +01:00
|
|
|
useRootNavigator: false,
|
2020-11-24 14:27:07 +01:00
|
|
|
textFields: [
|
|
|
|
DialogTextField(
|
|
|
|
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,
|
|
|
|
future: () => Matrix.of(context).client.changePassword(
|
2020-12-11 10:27:38 +01:00
|
|
|
password.single,
|
|
|
|
auth: AuthenticationThreePidCreds(
|
|
|
|
type: AuthenticationTypes.emailIdentity,
|
|
|
|
threepidCreds: [
|
|
|
|
ThreepidCreds(
|
|
|
|
sid: (response as RequestTokenResponse).sid,
|
|
|
|
clientSecret: clientSecret,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2020-11-24 14:27:07 +01:00
|
|
|
);
|
2020-12-25 09:58:34 +01:00
|
|
|
if (success.error == null) {
|
2021-04-03 13:09:20 +02:00
|
|
|
AdaptivePageLayout.of(context).showSnackBar(
|
|
|
|
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
|
|
|
|
Widget build(BuildContext context) {
|
2021-04-09 14:56:23 +02:00
|
|
|
return OnePageCard(
|
|
|
|
child: Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
leading: loading ? Container() : BackButton(),
|
|
|
|
elevation: 0,
|
|
|
|
title: Text(
|
|
|
|
L10n.of(context).logInTo(Matrix.of(context)
|
|
|
|
.client
|
|
|
|
.homeserver
|
|
|
|
.toString()
|
|
|
|
.replaceFirst('https://', '')),
|
|
|
|
),
|
2020-01-01 19:10:13 +01:00
|
|
|
),
|
2021-04-09 14:56:23 +02:00
|
|
|
body: Builder(builder: (context) {
|
|
|
|
return ListView(
|
|
|
|
children: <Widget>[
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(12.0),
|
|
|
|
child: TextField(
|
|
|
|
readOnly: loading,
|
|
|
|
autocorrect: false,
|
|
|
|
autofocus: true,
|
|
|
|
onChanged: (t) => _checkWellKnownWithCoolDown(t, context),
|
|
|
|
controller: usernameController,
|
|
|
|
autofillHints: loading ? null : [AutofillHints.username],
|
|
|
|
decoration: InputDecoration(
|
|
|
|
prefixIcon: Icon(Icons.account_box_outlined),
|
|
|
|
hintText:
|
|
|
|
'@${L10n.of(context).username.toLowerCase()}:domain',
|
|
|
|
errorText: usernameError,
|
|
|
|
labelText: L10n.of(context).username),
|
|
|
|
),
|
2020-04-12 09:19:22 +02:00
|
|
|
),
|
2021-04-09 14:56:23 +02:00
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(12.0),
|
|
|
|
child: TextField(
|
|
|
|
readOnly: loading,
|
|
|
|
autocorrect: false,
|
|
|
|
autofillHints: loading ? null : [AutofillHints.password],
|
|
|
|
controller: passwordController,
|
|
|
|
obscureText: !showPassword,
|
|
|
|
onSubmitted: (t) => login(context),
|
|
|
|
decoration: InputDecoration(
|
|
|
|
prefixIcon: Icon(Icons.lock_outlined),
|
|
|
|
hintText: '****',
|
|
|
|
errorText: passwordError,
|
|
|
|
suffixIcon: IconButton(
|
|
|
|
tooltip: L10n.of(context).showPassword,
|
|
|
|
icon: Icon(showPassword
|
|
|
|
? Icons.visibility_off_outlined
|
|
|
|
: Icons.visibility_outlined),
|
|
|
|
onPressed: () =>
|
|
|
|
setState(() => showPassword = !showPassword),
|
|
|
|
),
|
|
|
|
labelText: L10n.of(context).password),
|
|
|
|
),
|
2020-04-12 09:19:22 +02:00
|
|
|
),
|
2021-04-09 14:56:23 +02:00
|
|
|
SizedBox(height: 12),
|
|
|
|
Hero(
|
|
|
|
tag: 'loginButton',
|
|
|
|
child: Padding(
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 12),
|
|
|
|
child: ElevatedButton(
|
|
|
|
onPressed: loading ? null : () => login(context),
|
|
|
|
child: loading
|
|
|
|
? LinearProgressIndicator()
|
|
|
|
: Text(
|
|
|
|
L10n.of(context).login.toUpperCase(),
|
|
|
|
style: TextStyle(color: Colors.white, fontSize: 16),
|
|
|
|
),
|
|
|
|
),
|
2020-04-12 09:19:22 +02:00
|
|
|
),
|
|
|
|
),
|
2021-04-09 14:56:23 +02:00
|
|
|
Center(
|
|
|
|
child: TextButton(
|
|
|
|
onPressed: () => _passwordForgotten(context),
|
|
|
|
child: Text(
|
|
|
|
L10n.of(context).passwordForgotten,
|
|
|
|
style: TextStyle(
|
|
|
|
color: Colors.blue,
|
|
|
|
decoration: TextDecoration.underline,
|
|
|
|
),
|
2020-11-24 14:27:07 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2021-04-09 14:56:23 +02:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
),
|
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);
|
|
|
|
}
|