2021-12-04 13:23:33 +01:00
|
|
|
import bootstrap from 'bootstrap.native';
|
2023-02-15 14:19:48 +01:00
|
|
|
import tplLoginPanel from './templates/loginform.js';
|
2023-03-02 12:18:26 +01:00
|
|
|
import { ANONYMOUS } from '@converse/headless/shared/constants';
|
2021-12-04 22:28:50 +01:00
|
|
|
import { CustomElement } from 'shared/components/element.js';
|
2022-12-28 21:36:40 +01:00
|
|
|
import { _converse, api, converse } from '@converse/headless/core.js';
|
|
|
|
import { initConnection } from '@converse/headless/utils/init.js';
|
2022-03-24 21:41:05 +01:00
|
|
|
import { updateSettingsWithFormData, validateJID } from './utils.js';
|
2021-12-04 13:23:33 +01:00
|
|
|
|
|
|
|
const { Strophe, u } = converse.env;
|
|
|
|
|
|
|
|
|
2021-12-04 22:28:50 +01:00
|
|
|
class LoginForm extends CustomElement {
|
2021-12-04 13:23:33 +01:00
|
|
|
|
|
|
|
initialize () {
|
2021-12-04 23:42:42 +01:00
|
|
|
this.listenTo(_converse.connfeedback, 'change', () => this.requestUpdate());
|
2022-03-24 21:41:05 +01:00
|
|
|
this.handler = () => this.requestUpdate()
|
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback () {
|
|
|
|
super.connectedCallback();
|
|
|
|
api.settings.listen.on('change', this.handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnectedCallback () {
|
|
|
|
super.disconnectedCallback();
|
|
|
|
api.settings.listen.not('change', this.handler);
|
2021-12-04 13:23:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2023-02-15 14:19:48 +01:00
|
|
|
return tplLoginPanel(this);
|
2021-12-04 22:28:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
firstUpdated () {
|
|
|
|
this.initPopovers();
|
2021-12-04 13:23:33 +01:00
|
|
|
}
|
|
|
|
|
2021-12-04 23:42:42 +01:00
|
|
|
async onLoginFormSubmitted (ev) {
|
|
|
|
ev?.preventDefault();
|
2022-03-24 21:41:05 +01:00
|
|
|
|
2023-03-02 12:18:26 +01:00
|
|
|
if (api.settings.get('authentication') === ANONYMOUS) {
|
2022-03-24 21:41:05 +01:00
|
|
|
return this.connect(_converse.jid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!validateJID(ev.target)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
updateSettingsWithFormData(ev.target);
|
|
|
|
|
|
|
|
if (!api.settings.get('bosh_service_url') && !api.settings.get('websocket_url')) {
|
2021-12-11 12:28:04 +01:00
|
|
|
// We don't have a connection URL available, so we try here to discover
|
|
|
|
// XEP-0156 connection methods now, and if not found we present the user
|
|
|
|
// with the option to enter their own connection URL
|
|
|
|
await this.discoverConnectionMethods(ev);
|
2022-03-24 21:41:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (api.settings.get('bosh_service_url') || api.settings.get('websocket_url')) {
|
|
|
|
// FIXME: The connection class will still try to discover XEP-0156 connection methods
|
|
|
|
this.connect();
|
|
|
|
} else {
|
|
|
|
api.settings.set('show_connection_url_input', true);
|
2021-12-04 23:42:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
2022-12-28 21:36:40 +01:00
|
|
|
discoverConnectionMethods (ev) {
|
2021-12-04 23:42:42 +01:00
|
|
|
if (!api.settings.get("discover_connection_methods")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const form_data = new FormData(ev.target);
|
|
|
|
const jid = form_data.get('jid');
|
|
|
|
const domain = Strophe.getDomainFromJid(jid);
|
|
|
|
if (!_converse.connection?.jid || (jid && !u.isSameDomain(_converse.connection.jid, jid))) {
|
2022-12-28 21:36:40 +01:00
|
|
|
initConnection();
|
2021-12-04 23:42:42 +01:00
|
|
|
}
|
|
|
|
return _converse.connection.discoverConnectionMethods(domain);
|
|
|
|
}
|
|
|
|
|
2021-12-04 13:23:33 +01:00
|
|
|
initPopovers () {
|
|
|
|
Array.from(this.querySelectorAll('[data-title]')).forEach(el => {
|
|
|
|
new bootstrap.Popover(el, {
|
|
|
|
'trigger': (api.settings.get('view_mode') === 'mobile' && 'click') || 'hover',
|
|
|
|
'dismissible': (api.settings.get('view_mode') === 'mobile' && true) || false,
|
|
|
|
'container': this.parentElement.parentElement.parentElement,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
2022-03-24 21:41:05 +01:00
|
|
|
connect (jid) {
|
2021-12-04 13:23:33 +01:00
|
|
|
if (['converse/login', 'converse/register'].includes(_converse.router.history.getFragment())) {
|
|
|
|
_converse.router.navigate('', { 'replace': true });
|
|
|
|
}
|
2022-03-24 21:41:05 +01:00
|
|
|
_converse.connection?.reset();
|
|
|
|
api.user.login(jid);
|
2021-12-04 13:23:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-23 12:01:41 +02:00
|
|
|
api.elements.define('converse-login-form', LoginForm);
|