refactor: Update SDK and enable login with email and phone

This commit is contained in:
Christian Pauly 2021-03-09 19:39:25 +01:00
commit 39421fae32
8 changed files with 47 additions and 12 deletions

View file

@ -42,8 +42,28 @@ class _LoginState extends State<Login> {
setState(() => loading = true);
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);
}
await matrix.client.login(
user: usernameController.text,
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) {
@ -255,3 +275,12 @@ class _LoginState extends State<Login> {
);
}
}
extension on String {
static final RegExp _emailRegex = RegExp(
r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+");
static final RegExp _phoneRegex =
RegExp(r'^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$');
bool get isEmail => _emailRegex.hasMatch(this);
bool get isPhoneNumber => _phoneRegex.hasMatch(this);
}