diff --git a/CHANGES.md b/CHANGES.md index 66ed74be5..c4ec19103 100644 --- a/CHANGES.md +++ b/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 diff --git a/dist/converse.js b/dist/converse.js index 8922ed344..c7a8b65b1 100644 --- a/dist/converse.js +++ b/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 { diff --git a/docs/source/configuration.rst b/docs/source/configuration.rst index b9d44b8bd..6fbb7a565 100644 --- a/docs/source/configuration.rst +++ b/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 -------- diff --git a/src/headless/converse-core.js b/src/headless/converse-core.js index 1e68f6733..03cd2a154 100644 --- a/src/headless/converse-core.js +++ b/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, "+ diff --git a/src/headless/dist/converse-headless.js b/src/headless/dist/converse-headless.js index 14018eee5..84695ad2a 100644 --- a/src/headless/dist/converse-headless.js +++ b/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 {