fluffychat/lib/pages/signup.dart
2021-10-30 14:06:10 +02:00

111 lines
3.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:vrouter/vrouter.dart';
import 'package:fluffychat/pages/views/signup_view.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/widgets/matrix.dart';
import '../utils/localized_exception_extension.dart';
class SignupPage extends StatefulWidget {
const SignupPage({Key key}) : super(key: key);
@override
SignupPageController createState() => SignupPageController();
}
class SignupPageController extends State<SignupPage> {
final TextEditingController usernameController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
final TextEditingController passwordController2 = TextEditingController();
final TextEditingController emailController = TextEditingController();
String error;
bool loading = false;
bool showPassword = false;
void toggleShowPassword() => setState(() => showPassword = !showPassword);
String get domain => VRouter.of(context).queryParameters['domain'];
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
String usernameTextFieldValidator(String value) {
usernameController.text =
usernameController.text.trim().toLowerCase().replaceAll(' ', '_');
if (value.isEmpty) {
return L10n.of(context).pleaseChooseAUsername;
}
return null;
}
String password1TextFieldValidator(String value) {
const minLength = 8;
if (value.isEmpty) {
return L10n.of(context).chooseAStrongPassword;
}
if (value.length < minLength) {
return 'Please choose at least $minLength characters.';
}
return null;
}
String password2TextFieldValidator(String value) {
if (value.isEmpty) {
return L10n.of(context).chooseAStrongPassword;
}
if (value != passwordController.text) {
return 'Passwords do not match!';
}
return null;
}
String emailTextFieldValidator(String value) {
if (value.isNotEmpty && !value.contains('@')) {
return 'Please enter a valid email address.';
}
return null;
}
void signup([_]) async {
setState(() {
error = null;
});
if (!formKey.currentState.validate()) return;
setState(() {
loading = true;
});
try {
final client = Matrix.of(context).getLoginClient();
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,
);
}
await client.uiaRequestBackground(
(auth) => client.register(
username: usernameController.text,
password: passwordController.text,
initialDeviceDisplayName: PlatformInfos.clientName,
auth: auth,
),
);
} catch (e) {
error = (e as Object).toLocalizedString(context);
} finally {
setState(() => loading = false);
}
}
@override
Widget build(BuildContext context) => SignupPageView(this);
}