Sfoglia il codice sorgente

Allow `nickname` to be provided by the `credentials_url`

JC Brand 6 anni fa
parent
commit
624cf4f435

+ 1 - 0
CHANGES.md

@@ -7,6 +7,7 @@
 - Take roster nickname into consideration when rendering messages and chat headings.
 - Hide the textarea when a user is muted in a groupchat.
 - Don't restore a BOSH session without knowing the JID
+- You can now return a `nickname` value with your [credentials_url](https://conversejs.org/docs/html/configuration.html#credentials-url) endpoint.
 - #1296: `embedded` view mode shows `chatbox-navback` arrow in header
 - #1532: Converse reloads on enter pressed in the filter box
 

+ 21 - 6
dist/converse.js

@@ -63751,7 +63751,7 @@ async function finishInitialization() {
 }
 
 function fetchLoginCredentials() {
-  new es6_promise_dist_es6_promise_auto__WEBPACK_IMPORTED_MODULE_3___default.a((resolve, reject) => {
+  return new es6_promise_dist_es6_promise_auto__WEBPACK_IMPORTED_MODULE_3___default.a((resolve, reject) => {
     const xhr = new XMLHttpRequest();
     xhr.open('GET', _converse.credentials_url, true);
     xhr.setRequestHeader('Accept', "application/json, text/javascript");
@@ -63761,10 +63761,11 @@ function fetchLoginCredentials() {
         const data = JSON.parse(xhr.responseText);
         resolve({
           'jid': data.jid,
-          'password': data.password
+          'password': data.password,
+          'nickname': data.nickname
         });
       } else {
-        xhr.onerror();
+        xhr.onerror({});
       }
     };
 
@@ -63773,7 +63774,7 @@ function fetchLoginCredentials() {
 
       _converse.api.trigger('noResumeableSession', this);
 
-      reject(xhr.responseText);
+      reject(new Error(xhr.responseText));
     };
 
     xhr.send();
@@ -64589,7 +64590,7 @@ _converse.initialize = async function (settings, callback) {
     }
   };
 
-  this.attemptNonPreboundSession = function (credentials, reconnecting) {
+  this.attemptNonPreboundSession = async function (credentials, reconnecting) {
     /* Handle session resumption or initialization when prebind is not being used.
      *
      * Two potential options exist and are handled in this method:
@@ -64606,7 +64607,21 @@ _converse.initialize = async function (settings, callback) {
       this.autoLogin(credentials);
     } else if (this.auto_login) {
       if (this.credentials_url) {
-        fetchLoginCredentials().then(this.autoLogin.bind(this), this.autoLogin.bind(this));
+        let data = {};
+
+        try {
+          data = await fetchLoginCredentials();
+        } catch (e) {
+          _converse.log("Could not fetch login credentials", strophe_js__WEBPACK_IMPORTED_MODULE_0__["Strophe"].LogLevel.ERROR);
+
+          _converse.log(e, strophe_js__WEBPACK_IMPORTED_MODULE_0__["Strophe"].LogLevel.ERROR);
+        } finally {
+          if (_lodash_noconflict__WEBPACK_IMPORTED_MODULE_4___default.a.get(data, 'nickname')) {
+            _converse.nickname = data.nickname;
+          }
+
+          this.autoLogin(data);
+        }
       } else if (!this.jid) {
         throw new Error("attemptNonPreboundSession: If you use auto_login, " + "you also need to give either a jid value (and if " + "applicable a password) or you need to pass in a URL " + "from where the username and password can be fetched " + "(via credentials_url).");
       } else {

+ 8 - 0
docs/source/configuration.rst

@@ -572,8 +572,14 @@ The server behind ``credentials_url`` should return a JSON encoded object::
     {
         "jid": "me@example.com/resource",
         "password": "Ilikecats!"
+        "nickname": "catlover"
     }
 
+The ``nickname`` value is optional. If it's returned, then it's treated
+as equivalent to passing :ref:`nickname` to ``converse.initialize`` and will
+override any ``nickname`` value that might have already been passed in to
+``converse.initialize``.
+
 
 csi_waiting_time
 ----------------
@@ -1062,6 +1068,8 @@ muc_show_join_leave
 Determines whether Converse will show info messages inside a chatroom
 whenever a user joins or leaves it.
 
+.. _`nickname`:
+
 nickname
 --------
 

+ 27 - 18
src/headless/converse-core.js

@@ -484,25 +484,26 @@ async function finishInitialization () {
 }
 
 function fetchLoginCredentials () {
-   new Promise((resolve, reject) => {
+   return new Promise((resolve, reject) => {
       const xhr = new XMLHttpRequest();
       xhr.open('GET', _converse.credentials_url, true);
       xhr.setRequestHeader('Accept', "application/json, text/javascript");
       xhr.onload = function() {
-            if (xhr.status >= 200 && xhr.status < 400) {
-               const data = JSON.parse(xhr.responseText);
-               resolve({
-                  'jid': data.jid,
-                  'password': data.password
-               });
-            } else {
-               xhr.onerror();
-            }
+          if (xhr.status >= 200 && xhr.status < 400) {
+             const data = JSON.parse(xhr.responseText);
+             resolve({
+                'jid': data.jid,
+                'password': data.password,
+                'nickname': data.nickname
+             });
+          } else {
+             xhr.onerror({});
+          }
       };
       xhr.onerror = function () {
-            delete _converse.connection;
-            _converse.api.trigger('noResumeableSession', this);
-            reject(xhr.responseText);
+          delete _converse.connection;
+          _converse.api.trigger('noResumeableSession', this);
+          reject(new Error(xhr.responseText));
       };
       xhr.send();
    });
@@ -1227,7 +1228,7 @@ _converse.initialize = async function (settings, callback) {
         }
     };
 
-    this.attemptNonPreboundSession = function (credentials, reconnecting) {
+    this.attemptNonPreboundSession = async function (credentials, reconnecting) {
         /* Handle session resumption or initialization when prebind is not being used.
          *
          * Two potential options exist and are handled in this method:
@@ -1244,10 +1245,18 @@ _converse.initialize = async function (settings, callback) {
             this.autoLogin(credentials);
         } else if (this.auto_login) {
             if (this.credentials_url) {
-                fetchLoginCredentials().then(
-                    this.autoLogin.bind(this),
-                    this.autoLogin.bind(this)
-                );
+                let data = {};
+                try {
+                    data = await fetchLoginCredentials();
+                } catch (e) {
+                   _converse.log("Could not fetch login credentials", Strophe.LogLevel.ERROR);
+                   _converse.log(e, Strophe.LogLevel.ERROR);
+                } finally {
+                   if (_.get(data, 'nickname')) {
+                      _converse.nickname = data.nickname;
+                   }
+                   this.autoLogin(data);
+                }
             } else if (!this.jid) {
                 throw new Error(
                     "attemptNonPreboundSession: If you use auto_login, "+

+ 21 - 6
src/headless/dist/converse-headless.js

@@ -42145,7 +42145,7 @@ async function finishInitialization() {
 }
 
 function fetchLoginCredentials() {
-  new es6_promise_dist_es6_promise_auto__WEBPACK_IMPORTED_MODULE_3___default.a((resolve, reject) => {
+  return new es6_promise_dist_es6_promise_auto__WEBPACK_IMPORTED_MODULE_3___default.a((resolve, reject) => {
     const xhr = new XMLHttpRequest();
     xhr.open('GET', _converse.credentials_url, true);
     xhr.setRequestHeader('Accept', "application/json, text/javascript");
@@ -42155,10 +42155,11 @@ function fetchLoginCredentials() {
         const data = JSON.parse(xhr.responseText);
         resolve({
           'jid': data.jid,
-          'password': data.password
+          'password': data.password,
+          'nickname': data.nickname
         });
       } else {
-        xhr.onerror();
+        xhr.onerror({});
       }
     };
 
@@ -42167,7 +42168,7 @@ function fetchLoginCredentials() {
 
       _converse.api.trigger('noResumeableSession', this);
 
-      reject(xhr.responseText);
+      reject(new Error(xhr.responseText));
     };
 
     xhr.send();
@@ -42983,7 +42984,7 @@ _converse.initialize = async function (settings, callback) {
     }
   };
 
-  this.attemptNonPreboundSession = function (credentials, reconnecting) {
+  this.attemptNonPreboundSession = async function (credentials, reconnecting) {
     /* Handle session resumption or initialization when prebind is not being used.
      *
      * Two potential options exist and are handled in this method:
@@ -43000,7 +43001,21 @@ _converse.initialize = async function (settings, callback) {
       this.autoLogin(credentials);
     } else if (this.auto_login) {
       if (this.credentials_url) {
-        fetchLoginCredentials().then(this.autoLogin.bind(this), this.autoLogin.bind(this));
+        let data = {};
+
+        try {
+          data = await fetchLoginCredentials();
+        } catch (e) {
+          _converse.log("Could not fetch login credentials", strophe_js__WEBPACK_IMPORTED_MODULE_0__["Strophe"].LogLevel.ERROR);
+
+          _converse.log(e, strophe_js__WEBPACK_IMPORTED_MODULE_0__["Strophe"].LogLevel.ERROR);
+        } finally {
+          if (_lodash_noconflict__WEBPACK_IMPORTED_MODULE_4___default.a.get(data, 'nickname')) {
+            _converse.nickname = data.nickname;
+          }
+
+          this.autoLogin(data);
+        }
       } else if (!this.jid) {
         throw new Error("attemptNonPreboundSession: If you use auto_login, " + "you also need to give either a jid value (and if " + "applicable a password) or you need to pass in a URL " + "from where the username and password can be fetched " + "(via credentials_url).");
       } else {