Accept mxid on login (#187)
authorAjay Bura <ajbura@gmail.com>
Sat, 9 Jul 2022 08:28:57 +0000 (13:58 +0530)
committerAjay Bura <ajbura@gmail.com>
Sat, 9 Jul 2022 08:28:57 +0000 (13:58 +0530)
config.json
src/app/templates/auth/Auth.jsx
src/util/matrixUtil.js

index c647dbb486f75566146281ba9fed21fd0e6c2a73..2aa67ddcd0a5b6b60bea6ab78519e64914265b82 100644 (file)
@@ -1,7 +1,6 @@
 {
-  "defaultHomeserver": 4,
+  "defaultHomeserver": 3,
   "homeserverList": [
-    "converser.eu",
     "envs.net",
     "halogen.city",
     "kde.org",
index 0c27c00cdadccf6cb99b16b296db320fa5b446a2..7c2117360504e7c58123b1c843380fe91c00b82d 100644 (file)
@@ -56,11 +56,8 @@ function Homeserver({ onChange }) {
   const setupHsConfig = async (servername) => {
     setProcess({ isLoading: true, message: 'Looking for homeserver...' });
     let baseUrl = null;
-    try {
-      baseUrl = await getBaseUrl(servername);
-    } catch (e) {
-      baseUrl = e.message;
-    }
+    baseUrl = await getBaseUrl(servername);
+
     if (searchingHs !== servername) return;
     setProcess({ isLoading: true, message: `Connecting to ${baseUrl}...` });
     const tempClient = auth.createTemporaryClient(baseUrl);
@@ -175,31 +172,38 @@ function Login({ loginFlow, baseUrl }) {
 
   const validator = (values) => {
     const errors = {};
-    if (typeIndex === 0 && values.username.length > 0 && values.username.indexOf(':') > -1) {
-      errors.username = 'Username must contain local-part only';
-    }
     if (typeIndex === 1 && values.email.length > 0 && !isValidInput(values.email, EMAIL_REGEX)) {
       errors.email = BAD_EMAIL_ERROR;
     }
     return errors;
   };
-  const submitter = (values, actions) => auth.login(
-    baseUrl,
-    typeIndex === 0 ? normalizeUsername(values.username) : undefined,
-    typeIndex === 1 ? values.email : undefined,
-    values.password,
-  ).then(() => {
-    actions.setSubmitting(true);
-    window.location.reload();
-  }).catch((error) => {
-    let msg = error.message;
-    if (msg === 'Unknown message') msg = 'Please check your credentials';
-    actions.setErrors({
-      password: msg === 'Invalid password' ? msg : undefined,
-      other: msg !== 'Invalid password' ? msg : undefined,
+  const submitter = async (values, actions) => {
+    let userBaseUrl = baseUrl;
+    let { username } = values;
+    const mxIdMatch = username.match(/^@(.+):(.+\..+)$/);
+    if (typeIndex === 0 && mxIdMatch) {
+      [, username, userBaseUrl] = mxIdMatch;
+      userBaseUrl = await getBaseUrl(userBaseUrl);
+    }
+
+    return auth.login(
+      userBaseUrl,
+      typeIndex === 0 ? normalizeUsername(username) : undefined,
+      typeIndex === 1 ? values.email : undefined,
+      values.password,
+    ).then(() => {
+      actions.setSubmitting(true);
+      window.location.reload();
+    }).catch((error) => {
+      let msg = error.message;
+      if (msg === 'Unknown message') msg = 'Please check your credentials';
+      actions.setErrors({
+        password: msg === 'Invalid password' ? msg : undefined,
+        other: msg !== 'Invalid password' ? msg : undefined,
+      });
+      actions.setSubmitting(false);
     });
-    actions.setSubmitting(false);
-  });
+  };
 
   return (
     <>
index fb97d8509770ee17d697eeb2424a247507ef3225..ef016edab4be662a03009a23776013db72f96aa2 100644 (file)
@@ -20,7 +20,7 @@ export async function getBaseUrl(servername) {
     if (baseUrl === undefined) throw new Error();
     return baseUrl;
   } catch (e) {
-    throw new Error(`${protocol}${servername}`);
+    return `${protocol}${servername}`;
   }
 }
 
@@ -204,11 +204,10 @@ export async function hasDevices(userId) {
   const mx = initMatrix.matrixClient;
   try {
     const usersDeviceMap = await mx.downloadKeys([userId, mx.getUserId()]);
-    return Object.values(usersDeviceMap).every((userDevices) =>
-      Object.keys(userDevices).length > 0,
-    );
+    return Object.values(usersDeviceMap)
+      .every((userDevices) => (Object.keys(userDevices).length > 0));
   } catch (e) {
     console.error("Error determining if it's possible to encrypt to all users: ", e);
     return false;
   }
-}
\ No newline at end of file
+}