fluffychat/lib/pages/sign_up/signup.dart

113 lines
3.3 KiB
Dart
Raw Normal View History

2021-10-26 18:50:34 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-10-30 14:06:10 +02:00
import 'package:vrouter/vrouter.dart';
2021-10-26 18:50:34 +02:00
2021-11-09 21:32:16 +01:00
import 'package:fluffychat/pages/sign_up/signup_view.dart';
2021-09-10 10:30:54 +02:00
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/widgets/matrix.dart';
2021-11-09 21:32:16 +01:00
import '../../utils/localized_exception_extension.dart';
2021-09-10 10:30:54 +02:00
class SignupPage extends StatefulWidget {
2022-01-29 12:35:03 +01:00
const SignupPage({Key? key}) : super(key: key);
2021-09-10 10:30:54 +02:00
@override
SignupPageController createState() => SignupPageController();
}
class SignupPageController extends State<SignupPage> {
final TextEditingController usernameController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
2021-10-30 14:06:10 +02:00
final TextEditingController passwordController2 = TextEditingController();
final TextEditingController emailController = TextEditingController();
2022-01-29 12:35:03 +01:00
String? error;
2021-09-10 10:30:54 +02:00
bool loading = false;
2021-10-30 14:06:10 +02:00
bool showPassword = false;
2021-09-10 10:30:54 +02:00
void toggleShowPassword() => setState(() => showPassword = !showPassword);
2022-01-29 12:35:03 +01:00
String? get domain => VRouter.of(context).queryParameters['domain'];
2021-10-30 14:06:10 +02:00
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
2022-01-29 12:35:03 +01:00
String? usernameTextFieldValidator(String? value) {
2021-10-30 14:06:10 +02:00
usernameController.text =
usernameController.text.trim().toLowerCase().replaceAll(' ', '_');
2022-01-29 12:35:03 +01:00
if (value!.isEmpty) {
return L10n.of(context)!.pleaseChooseAUsername;
2021-10-30 14:06:10 +02:00
}
return null;
}
2021-09-10 10:30:54 +02:00
2022-01-29 12:35:03 +01:00
String? password1TextFieldValidator(String? value) {
2021-10-30 14:06:10 +02:00
const minLength = 8;
2022-01-29 12:35:03 +01:00
if (value!.isEmpty) {
return L10n.of(context)!.chooseAStrongPassword;
2021-10-30 14:06:10 +02:00
}
if (value.length < minLength) {
2022-01-29 12:35:03 +01:00
return L10n.of(context)!.pleaseChooseAtLeastChars(minLength.toString());
2021-09-10 10:30:54 +02:00
}
2021-10-30 14:06:10 +02:00
return null;
}
2022-01-29 12:35:03 +01:00
String? password2TextFieldValidator(String? value) {
if (value!.isEmpty) {
return L10n.of(context)!.chooseAStrongPassword;
2021-09-10 10:30:54 +02:00
}
2021-10-30 14:06:10 +02:00
if (value != passwordController.text) {
2022-01-29 12:35:03 +01:00
return L10n.of(context)!.passwordsDoNotMatch;
2021-10-30 14:06:10 +02:00
}
return null;
}
2022-01-29 12:35:03 +01:00
String? emailTextFieldValidator(String? value) {
if (value!.isNotEmpty && !value.contains('@')) {
return L10n.of(context)!.pleaseEnterValidEmail;
2021-10-30 14:06:10 +02:00
}
return null;
}
void signup([_]) async {
setState(() {
error = null;
});
2022-01-29 12:35:03 +01:00
if (!formKey.currentState!.validate()) return;
2021-09-10 10:30:54 +02:00
2021-10-30 14:06:10 +02:00
setState(() {
loading = true;
});
2021-09-10 10:30:54 +02:00
try {
final client = Matrix.of(context).getLoginClient();
2021-10-30 14:06:10 +02:00
final email = emailController.text;
if (email.isNotEmpty) {
Matrix.of(context).currentClientSecret =
DateTime.now().millisecondsSinceEpoch.toString();
Matrix.of(context).currentThreepidCreds =
await client.requestTokenToRegisterEmail(
Matrix.of(context).currentClientSecret,
email,
0,
);
}
2021-09-10 10:30:54 +02:00
await client.uiaRequestBackground(
(auth) => client.register(
username: usernameController.text,
password: passwordController.text,
initialDeviceDisplayName: PlatformInfos.clientName,
auth: auth,
),
);
} catch (e) {
2022-01-29 12:35:03 +01:00
error = (e).toLocalizedString(context);
2021-09-10 10:30:54 +02:00
} finally {
2021-11-04 17:10:16 +01:00
if (mounted) {
setState(() => loading = false);
}
2021-09-10 10:30:54 +02:00
}
}
@override
Widget build(BuildContext context) => SignupPageView(this);
}