Преглед на файлове

Updates #721 Restore sessions when logging in anonymously

together with keepalive.
JC Brand преди 8 години
родител
ревизия
9bf00241de
променени са 4 файла, в които са добавени 33 реда и са изтрити 36 реда
  1. 3 0
      docs/CHANGES.md
  2. 1 1
      package.json
  3. 1 3
      spec/converse.js
  4. 28 32
      src/converse-core.js

+ 3 - 0
docs/CHANGES.md

@@ -1,5 +1,8 @@
 # Changelog
 
+## 2.0.2 (Unreleased)
+- #721 keepalive not working with anonymous authentication [jcbrand]
+
 ## 2.0.1 (2016-11-07)
 - #203 New configuration setting [muc_domain](https://conversejs.org/docs/html/configuration.html#muc_domain) [jcbrand]
 - #705 White content after submitting password on chatrooms [jcbrand]

+ 1 - 1
package.json

@@ -57,7 +57,7 @@
     "po2json": "^0.4.4",
     "requirejs": "2.3.2",
     "requirejs-undertemplate": "~0.0.4",
-    "strophe.js": "1.2.9",
+    "strophe.js": "https://github.com/strophe/strophejs#b82171f81db469050bf55fa5394b3283c9b3d3bd",
     "strophejs-plugins": "0.0.7",
     "text": "requirejs/text#2.0.15",
     "underscore": "~1.8.3"

+ 1 - 3
spec/converse.js

@@ -46,13 +46,11 @@
                 }));
 
                 it("needs jid, rid and sid values when not using keepalive", mock.initConverse(function (converse) {
-                    var authentication = converse.authentication;
                     var jid = converse.jid;
                     delete converse.jid;
                     converse.authentication = "prebind";
                     expect(converse.logIn.bind(converse)).toThrow(
-                        new Error("attemptPreboundSession: If you use prebind and not keepalive, then you MUST supply JID, RID and SID values"));
-                    converse.authentication= authentication;
+                        new Error("attemptPreboundSession: If you use prebind and not keepalive, then you MUST supply JID, RID and SID values or a prebind_url."));
                     converse.bosh_service_url = undefined;
                     converse.jid = jid;
                 }));

+ 28 - 32
src/converse-core.js

@@ -1757,31 +1757,30 @@
         };
 
         this.startNewBOSHSession = function () {
+            var that = this;
             $.ajax({
                 url:  this.prebind_url,
                 type: 'GET',
                 dataType: "json",
                 success: function (response) {
-                    this.connection.attach(
+                    that.connection.attach(
                             response.jid,
                             response.sid,
                             response.rid,
-                            this.onConnectStatusChanged
+                            that.onConnectStatusChanged
                     );
-                }.bind(this),
+                },
                 error: function (response) {
-                    delete this.connection;
-                    this.emit('noResumeableSession');
-                }.bind(this)
+                    delete that.connection;
+                    that.emit('noResumeableSession');
+                }
             });
         };
 
         this.attemptPreboundSession = function (tokens) {
             /* Handle session resumption or initialization when prebind is being used.
              */
-            if (this.jid && this.sid && this.rid) {
-                return this.connection.attach(this.jid, this.sid, this.rid, this.onConnectStatusChanged);
-            } else if (this.keepalive) {
+            if (this.keepalive) {
                 if (!this.jid) {
                     throw new Error("attemptPreboundSession: when using 'keepalive' with 'prebind, "+
                                     "you must supply the JID of the current user.");
@@ -1792,18 +1791,16 @@
                     this.log("Could not restore session for jid: "+this.jid+" Error message: "+e.message);
                     this.clearSession(); // If there's a roster, we want to clear it (see #555)
                 }
-            } else {
-                throw new Error("attemptPreboundSession: If you use prebind and not keepalive, "+
-                    "then you MUST supply JID, RID and SID values");
             }
-            // We haven't been able to attach yet. Let's see if there
-            // is a prebind_url, otherwise there's nothing with which
-            // we can attach.
-            if (this.prebind_url) {
-                this.startNewBOSHSession();
+
+            // No keepalive, or session resumption has failed.
+            if (this.jid && this.sid && this.rid) {
+                return this.connection.attach(this.jid, this.sid, this.rid, this.onConnectStatusChanged);
+            } else if (this.prebind_url) {
+                return this.startNewBOSHSession();
             } else {
-                delete this.connection;
-                this.emit('noResumeableSession');
+                throw new Error("attemptPreboundSession: If you use prebind and not keepalive, "+
+                    "then you MUST supply JID, RID and SID values or a prebind_url.");
             }
         };
 
@@ -1844,7 +1841,7 @@
             }
         };
 
-        this.attemptNonPreboundSession = function () {
+        this.attemptNonPreboundSession = function (credentials) {
             /* Handle session resumption or initialization when prebind is not being used.
              *
              * Two potential options exist and are handled in this method:
@@ -1860,7 +1857,11 @@
                 }
             }
             if (this.auto_login) {
-                if (this.credentials_url) {
+                if (credentials) {
+                    // When credentials are passed in, they override prebinding
+                    // or credentials fetching via HTTP
+                    this.autoLogin(credentials);
+                } else if (this.credentials_url) {
                     this.fetchLoginCredentials().done(this.autoLogin.bind(this));
                 } else if (!this.jid) {
                     throw new Error(
@@ -1870,24 +1871,19 @@
                         "username and password can be fetched (via credentials_url)."
                     );
                 } else {
+                    // Probably ANONYMOUS login
                     this.autoLogin();
                 }
             }
         };
 
         this.logIn = function (credentials) {
-            if (credentials || this.authentication === converse.ANONYMOUS) {
-                // When credentials are passed in, they override prebinding
-                // or credentials fetching via HTTP
-                this.autoLogin(credentials);
+            // We now try to resume or automatically set up a new session.
+            // Otherwise the user will be shown a login form.
+            if (this.authentication === converse.PREBIND) {
+                this.attemptPreboundSession();
             } else {
-                // We now try to resume or automatically set up a new session.
-                // Otherwise the user will be shown a login form.
-                if (this.authentication === converse.PREBIND) {
-                    this.attemptPreboundSession();
-                } else {
-                    this.attemptNonPreboundSession();
-                }
+                this.attemptNonPreboundSession(credentials);
             }
         };