Explorar o código

Ensure current resource is bound when manually calling `connection.bind`

JC Brand %!s(int64=6) %!d(string=hai) anos
pai
achega
bd81b89724
Modificáronse 1 ficheiros con 11 adicións e 12 borrados
  1. 11 12
      src/headless/converse-core.js

+ 11 - 12
src/headless/converse-core.js

@@ -525,10 +525,14 @@ async function initUserSession (jid) {
 }
 
 async function setUserJID (jid) {
+    jid = jid.toLowerCase();
     if (!Strophe.getResourceFromJid(jid)) {
         jid = jid.toLowerCase() + _converse.generateResource();
+        // Set JID on the connection object so that when we call
+        // `connection.bind` the new resource is found by Strophe.js
+        // and sent to the XMPP server.
+        _converse.connection.jid = jid;
     }
-    jid = jid.toLowerCase();
     await initUserSession(jid);
     _converse.jid = jid;
     _converse.bare_jid = Strophe.getBareJidFromJid(jid);
@@ -545,6 +549,7 @@ async function setUserJID (jid) {
      * @event _converse#setUserJID
      */
     _converse.api.trigger('setUserJID');
+    return jid;
 }
 
 
@@ -1461,18 +1466,12 @@ _converse.api = {
                     return;
                 }
             }
-            let credentials;
-            if (jid && password) {
-                credentials = { jid, password };
-            } else if (u.isValidJID(_converse.jid) && _converse.password) {
-                credentials = {
-                    jid: _converse.jid,
-                    password: _converse.password
-                };
-            }
-            if (credentials && credentials.jid) {
-                await setUserJID(jid || _converse.jid);
+            if (jid || _converse.jid) {
+                // Reassign because we might have gained a resource
+                jid = await setUserJID(jid || _converse.jid);
             }
+            password = password || _converse.password;
+            const credentials = (jid && password) ? { jid, password } : null;
             _converse.attemptNonPreboundSession(credentials, reconnecting);
         },