Browse Source

Add `login` API method.

JC Brand 9 năm trước cách đây
mục cha
commit
e09328df6a
3 tập tin đã thay đổi với 35 bổ sung8 xóa
  1. 16 0
      docs/source/development.rst
  2. 3 0
      src/converse-api.js
  3. 16 8
      src/converse-core.js

+ 16 - 0
docs/source/development.rst

@@ -400,6 +400,22 @@ The "user" grouping
 
 This grouping collects API functions related to the current logged in user.
 
+login
+~~~~~
+
+Logs the user in. This method can accept a map with the credentials, like this:
+
+.. code-block:: javascript
+
+    converse.user.login({
+        'jid': 'dummy@example.com',
+        'password': 'secret'
+    });
+
+or it can be called without any parameters, in which case converse.js will try
+to log the user in by calling the `prebind_url` or `credentials_url` depending
+on whether prebinding is used or not.
+
 logout
 ~~~~~~
 

+ 3 - 0
src/converse-api.js

@@ -34,6 +34,9 @@
             },
         },
         'user': {
+            'login': function (credentials) {
+                converse.logIn(credentials);
+            },
             'logout': function () {
                 converse.logOut();
             },

+ 16 - 8
src/converse-core.js

@@ -443,7 +443,7 @@
             if (converse.disconnection_cause === Strophe.Status.CONNFAIL ||
                     (converse.disconnection_cause === Strophe.Status.AUTHFAIL &&
                      converse.credentials_url &&
-                     converse.auto_login
+                     !converse.logged_out
                     )
                 ) {
                 converse.reconnect(condition);
@@ -1703,6 +1703,20 @@
             }
         };
 
+        this.logIn = function (credentials) {
+            if (credentials) {
+                this.autoLogin(credentials);
+            } 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.initConnection = function () {
             if (this.connection && this.connection.connected) {
                 this.setUpXMLLogging();
@@ -1719,13 +1733,7 @@
                     throw new Error("initConnection: this browser does not support websockets and bosh_service_url wasn't specified.");
                 }
                 this.setUpXMLLogging();
-                // 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.logIn();
             }
         };