fluffychat/lib/views/login.dart

258 lines
8.6 KiB
Dart
Raw Normal View History

2020-05-16 06:43:27 +00:00
import 'dart:async';
2020-01-01 18:10:13 +00:00
import 'dart:math';
2020-11-24 13:27:07 +00:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2020-01-01 18:10:13 +00:00
import 'package:famedlysdk/famedlysdk.dart';
2020-12-25 08:58:34 +00:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2020-01-01 18:10:13 +00:00
import 'package:fluffychat/components/matrix.dart';
2020-11-24 13:27:07 +00:00
import 'package:flushbar/flushbar_helper.dart';
2020-01-01 18:10:13 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-02-07 16:18:38 +00:00
import '../utils/platform_infos.dart';
2020-01-01 18:10:13 +00:00
2020-01-09 21:52:27 +00:00
class Login extends StatefulWidget {
2020-01-01 18:10:13 +00:00
@override
2020-01-09 21:52:27 +00:00
_LoginState createState() => _LoginState();
2020-01-01 18:10:13 +00:00
}
2020-01-09 21:52:27 +00:00
class _LoginState extends State<Login> {
2020-01-01 18:10:13 +00:00
final TextEditingController usernameController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
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
void login(BuildContext context) async {
2020-05-13 13:58:59 +00:00
var matrix = Matrix.of(context);
2020-01-01 18:10:13 +00:00
if (usernameController.text.isEmpty) {
2020-05-07 05:52:40 +00:00
setState(() => usernameError = L10n.of(context).pleaseEnterYourUsername);
2020-01-01 18:10:13 +00:00
} else {
setState(() => usernameError = null);
}
if (passwordController.text.isEmpty) {
2020-05-07 05:52:40 +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);
2020-01-01 18:10:13 +00:00
try {
2020-01-09 21:52:27 +00:00
await matrix.client.login(
2020-08-16 10:54:43 +00:00
user: usernameController.text,
password: passwordController.text,
2021-02-07 16:18:38 +00:00
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
}
2021-01-16 11:46:38 +00:00
if (mounted) setState(() => loading = false);
2020-01-01 18:10:13 +00:00
}
2020-05-16 06:43:27 +00: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 10:54:43 +00:00
newDomain != Matrix.of(context).client.homeserver.toString()) {
2020-12-25 08:58:34 +00:00
await showFutureLoadingDialog(
context: context,
future: () => Matrix.of(context).client.checkHomeserver(newDomain),
);
2020-05-16 06:43:27 +00:00
setState(() => usernameError = null);
}
} catch (e) {
setState(() => usernameError = e.toString());
}
}
2020-11-24 13:27:07 +00:00
void _passwordForgotten(BuildContext context) async {
final input = await showTextInputDialog(
context: context,
title: L10n.of(context).enterAnEmailAddress,
2021-02-23 10:03:54 +00:00
okLabel: L10n.of(context).ok,
cancelLabel: L10n.of(context).cancel,
2021-02-24 11:17:23 +00:00
useRootNavigator: false,
2020-11-24 13:27:07 +00:00
textFields: [
DialogTextField(
hintText: L10n.of(context).enterAnEmailAddress,
keyboardType: TextInputType.emailAddress,
),
],
);
if (input == null) return;
final clientSecret = DateTime.now().millisecondsSinceEpoch.toString();
2020-12-25 08:58:34 +00:00
final response = await showFutureLoadingDialog(
context: context,
future: () => Matrix.of(context).client.resetPasswordUsingEmail(
2020-11-24 13:27:07 +00:00
input.single,
clientSecret,
sendAttempt++,
),
);
2020-12-25 08:58:34 +00:00
if (response.error != null) return;
2020-11-24 13:27:07 +00: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 11:17:23 +00:00
useRootNavigator: false,
2020-11-24 13:27:07 +00:00
);
if (ok == null) return;
final password = await showTextInputDialog(
context: context,
title: L10n.of(context).chooseAStrongPassword,
2021-02-23 10:03:54 +00:00
okLabel: L10n.of(context).ok,
cancelLabel: L10n.of(context).cancel,
2021-02-24 11:17:23 +00:00
useRootNavigator: false,
2020-11-24 13:27:07 +00:00
textFields: [
DialogTextField(
hintText: '******',
obscureText: true,
minLines: 1,
maxLines: 1,
2020-11-24 13:27:07 +00:00
),
],
);
if (password == null) return;
2020-12-25 08:58:34 +00:00
final success = await showFutureLoadingDialog(
context: context,
future: () => Matrix.of(context).client.changePassword(
2020-12-11 09:27:38 +00:00
password.single,
auth: AuthenticationThreePidCreds(
type: AuthenticationTypes.emailIdentity,
threepidCreds: [
ThreepidCreds(
sid: (response as RequestTokenResponse).sid,
clientSecret: clientSecret,
),
],
),
),
2020-11-24 13:27:07 +00:00
);
2020-12-25 08:58:34 +00:00
if (success.error == null) {
2020-11-24 13:27:07 +00:00
FlushbarHelper.createSuccess(
message: L10n.of(context).passwordHasBeenChanged);
}
}
static int sendAttempt = 0;
2020-01-01 18:10:13 +00:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
2021-01-16 13:24:52 +00:00
leading: loading ? Container() : BackButton(),
2020-04-12 08:35:45 +00:00
elevation: 0,
title: Text(
2020-05-07 05:52:40 +00:00
L10n.of(context).logInTo(Matrix.of(context)
2020-04-12 08:35:45 +00:00
.client
.homeserver
2020-06-10 08:07:01 +00:00
.toString()
2020-04-12 08:35:45 +00:00
.replaceFirst('https://', '')),
2020-01-01 18:10:13 +00:00
),
),
2020-04-12 07:19:22 +00:00
body: Builder(builder: (context) {
return ListView(
padding: EdgeInsets.symmetric(
horizontal:
max((MediaQuery.of(context).size.width - 600) / 2, 0)),
children: <Widget>[
2021-01-24 16:26:59 +00:00
Padding(
padding: const EdgeInsets.all(12.0),
child: TextField(
2020-04-12 07:19:22 +00:00
readOnly: loading,
autocorrect: false,
2020-04-12 08:35:45 +00:00
autofocus: true,
2020-05-16 06:43:27 +00:00
onChanged: (t) => _checkWellKnownWithCoolDown(t, context),
2020-04-12 07:19:22 +00:00
controller: usernameController,
2021-02-01 20:39:34 +00:00
autofillHints: loading ? null : [AutofillHints.username],
2020-04-12 07:19:22 +00:00
decoration: InputDecoration(
2021-01-24 16:26:59 +00:00
prefixIcon: Icon(Icons.account_box_outlined),
2020-04-12 07:19:22 +00:00
hintText:
2020-05-13 13:58:59 +00:00
'@${L10n.of(context).username.toLowerCase()}:domain',
2020-04-12 07:19:22 +00:00
errorText: usernameError,
2020-05-07 05:52:40 +00:00
labelText: L10n.of(context).username),
2020-04-12 07:19:22 +00:00
),
2020-01-09 21:52:27 +00:00
),
2021-01-24 16:26:59 +00:00
Padding(
padding: const EdgeInsets.all(12.0),
child: TextField(
2020-04-12 07:19:22 +00:00
readOnly: loading,
autocorrect: false,
2021-02-01 20:39:34 +00:00
autofillHints: loading ? null : [AutofillHints.password],
2020-04-12 07:19:22 +00:00
controller: passwordController,
obscureText: !showPassword,
onSubmitted: (t) => login(context),
decoration: InputDecoration(
2021-01-24 16:26:59 +00:00
prefixIcon: Icon(Icons.lock_outlined),
2020-05-13 13:58:59 +00:00
hintText: '****',
2020-04-12 07:19:22 +00:00
errorText: passwordError,
suffixIcon: IconButton(
2021-02-20 06:40:42 +00:00
tooltip: L10n.of(context).showPassword,
2020-04-12 07:19:22 +00:00
icon: Icon(showPassword
2020-12-06 09:31:35 +00:00
? Icons.visibility_off_outlined
: Icons.visibility_outlined),
2020-04-12 07:19:22 +00:00
onPressed: () =>
setState(() => showPassword = !showPassword),
2020-01-09 21:52:27 +00:00
),
2020-05-07 05:52:40 +00:00
labelText: L10n.of(context).password),
2020-04-12 07:19:22 +00:00
),
2020-01-01 18:10:13 +00:00
),
2021-01-24 16:26:59 +00:00
SizedBox(height: 12),
2020-04-12 08:35:45 +00:00
Hero(
tag: 'loginButton',
2021-03-04 11:28:06 +00:00
child: Padding(
2020-04-12 08:35:45 +00:00
padding: EdgeInsets.symmetric(horizontal: 12),
2021-03-04 11:28:06 +00:00
child: ElevatedButton(
onPressed: loading ? null : () => login(context),
2020-04-12 08:35:45 +00:00
child: loading
2021-01-16 14:13:29 +00:00
? LinearProgressIndicator()
2020-04-12 08:35:45 +00:00
: Text(
2020-05-07 05:52:40 +00:00
L10n.of(context).login.toUpperCase(),
2020-04-12 08:35:45 +00:00
style: TextStyle(color: Colors.white, fontSize: 16),
),
2020-04-12 07:19:22 +00:00
),
),
),
2020-11-24 13:27:07 +00:00
Center(
2021-02-27 06:53:34 +00:00
child: TextButton(
2021-03-04 11:28:06 +00:00
onPressed: () => _passwordForgotten(context),
2020-11-24 13:27:07 +00:00
child: Text(
L10n.of(context).passwordForgotten,
style: TextStyle(
color: Colors.blue,
decoration: TextDecoration.underline,
),
),
),
),
2020-04-12 07:19:22 +00:00
],
);
}),
2020-01-01 18:10:13 +00:00
);
}
}