Commit e4eafe9e authored by JC Brand's avatar JC Brand

Simplify `attemptNonPreboundSession`

- Split between `login` auth and the rest (`external`, `anonymous`, `prebind`)
- It doesn't make sense to pass along credentials when not using `login` auth so avoid that flow.
- It's not necessary to pass the `reconnecting` flag to `attemptNonPreboundSession`
parent 30ad84d3
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
// 'prosody@conference.prosody.im', // 'prosody@conference.prosody.im',
// 'jdev@conference.jabber.org' // 'jdev@conference.jabber.org'
// ], // ],
// bosh_service_url: 'http://chat.example.org:5280/http-bind/',
websocket_url: 'wss://conversejs.org/xmpp-websocket', websocket_url: 'wss://conversejs.org/xmpp-websocket',
bosh_service_url: 'https://conversejs.org/http-bind/', // Please use this connection manager only for testing purposes bosh_service_url: 'https://conversejs.org/http-bind/', // Please use this connection manager only for testing purposes
view_mode: 'fullscreen', view_mode: 'fullscreen',
......
...@@ -642,6 +642,14 @@ async function getLoginCredentials () { ...@@ -642,6 +642,14 @@ async function getLoginCredentials () {
return credentials; return credentials;
} }
async function getLoginCredentialsFromBrowser () {
const creds = await navigator.credentials.get({'password': true});
if (creds && creds.type == 'password' && u.isValidJID(creds.id)) {
await setUserJID(creds.id);
return {'jid': creds.id, 'password': creds.password};
}
}
function unregisterGlobalEventHandlers () { function unregisterGlobalEventHandlers () {
document.removeEventListener("visibilitychange", _converse.saveWindowState); document.removeEventListener("visibilitychange", _converse.saveWindowState);
...@@ -1213,34 +1221,21 @@ _converse.initialize = async function (settings, callback) { ...@@ -1213,34 +1221,21 @@ _converse.initialize = async function (settings, callback) {
} }
}); });
this.attemptNonPreboundSession = async function (credentials) {
this.attemptNonPreboundSession = async function (credentials, reconnecting) { if (_converse.authentication === _converse.LOGIN) {
if (credentials) { if (credentials) {
this.autoLogin(credentials); this.autoLogin(credentials);
} else if (this.auto_login) { } else if (_converse.jid && (_converse.password || _converse.connection.pass)) {
if (this.credentials_url && _converse.authentication === _converse.LOGIN) {
const data = await getLoginCredentials();
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 {
// Could be ANONYMOUS or EXTERNAL or manual passing in of JID and password
this.autoLogin();
}
} else if (reconnecting) {
this.autoLogin(); this.autoLogin();
} else if (_converse.credentials_url) {
this.autoLogin(await getLoginCredentials());
} else if (!_converse.isTestEnv() && window.PasswordCredential) { } else if (!_converse.isTestEnv() && window.PasswordCredential) {
const creds = await navigator.credentials.get({'password': true}); _converse.autoLogin(await getLoginCredentialsFromBrowser());
if (creds && creds.type == 'password' && u.isValidJID(creds.id)) { } else {
await setUserJID(creds.id); throw new Error("attemptNonPreboundSession: Could not find any credentials to log you in with!");
this.autoLogin({'jid': creds.id, 'password': creds.password});
} }
} else {
this.autoLogin(); // Could be ANONYMOUS or EXTERNAL
} }
}; };
...@@ -1464,7 +1459,9 @@ _converse.api = { ...@@ -1464,7 +1459,9 @@ _converse.api = {
*/ */
async login (jid, password, reconnecting) { async login (jid, password, reconnecting) {
if (_converse.api.connection.isType('bosh')) { if (_converse.api.connection.isType('bosh')) {
if (reconnecting && _converse.prebind_url) { if (reconnecting &&
_converse.authentication === _converse.PREBIND &&
_converse.prebind_url) {
return _converse.startNewBOSHSession(); return _converse.startNewBOSHSession();
} else if (await _converse.restoreBOSHSession()) { } else if (await _converse.restoreBOSHSession()) {
return; return;
...@@ -1476,7 +1473,7 @@ _converse.api = { ...@@ -1476,7 +1473,7 @@ _converse.api = {
} }
password = password || _converse.password; password = password || _converse.password;
const credentials = (jid && password) ? { jid, password } : null; const credentials = (jid && password) ? { jid, password } : null;
_converse.attemptNonPreboundSession(credentials, reconnecting); _converse.attemptNonPreboundSession(credentials);
}, },
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment