jiti-meet/modules/UI/authentication/LoginDialog.js

235 lines
6.6 KiB
JavaScript
Raw Normal View History

/* global APP, config */
2016-01-15 14:59:35 +00:00
/**
* Build html for "password required" dialog.
* @returns {string} html string
*/
2015-12-17 15:31:11 +00:00
function getPasswordInputHtml() {
let placeholder = config.hosts.authdomain
? "user identity"
: "user@domain.net";
2015-12-17 15:31:11 +00:00
return `
<input name="username" type="text"
class="input-control__input"
placeholder=${placeholder} autofocus>
2015-12-17 15:31:11 +00:00
<input name="password" type="password"
class="input-control__input"
2016-01-15 14:59:35 +00:00
data-i18n="[placeholder]dialog.userPassword"
placeholder="user password">`;
}
2016-01-15 14:59:35 +00:00
/**
* Convert provided id to jid if it's not jid yet.
* @param {string} id user id or jid
* @returns {string} jid
*/
function toJid(id) {
if (id.indexOf("@") >= 0) {
return id;
}
let jid = id.concat('@');
if (config.hosts.authdomain) {
jid += config.hosts.authdomain;
} else {
jid += config.hosts.domain;
}
return jid;
}
2016-01-15 14:59:35 +00:00
/**
* Generate cancel button config for the dialog.
* @returns {Object}
*/
function cancelButton() {
return {
title: APP.translation.generateTranslationHTML("dialog.Cancel"),
value: false
};
2015-12-17 15:31:11 +00:00
}
2016-01-15 14:59:35 +00:00
/**
* Auth dialog for JitsiConnection which supports retries.
* If no cancelCallback provided then there will be
* no cancel button on the dialog.
*
* @class LoginDialog
* @constructor
*
* @param {function(jid, password)} successCallback
* @param {function} [cancelCallback] callback to invoke if user canceled.
*/
function LoginDialog(successCallback, cancelCallback) {
let loginButtons = [{
title: APP.translation.generateTranslationHTML("dialog.Ok"),
value: true
}];
let finishedButtons = [{
title: APP.translation.translateString('dialog.retry'),
value: 'retry'
}];
// show "cancel" button only if cancelCallback provided
if (cancelCallback) {
loginButtons.push(cancelButton());
finishedButtons.push(cancelButton());
}
2015-12-17 15:31:11 +00:00
const states = {
login: {
title: APP.translation.translateString('dialog.passwordRequired'),
2015-12-17 15:31:11 +00:00
html: getPasswordInputHtml(),
buttons: loginButtons,
focus: ':input:first',
submit: function (e, v, m, f) {
e.preventDefault();
if (v) {
2015-12-17 15:31:11 +00:00
let jid = f.username;
let password = f.password;
if (jid && password) {
connDialog.goToState('connecting');
successCallback(toJid(jid), password);
}
} else {
// User cancelled
2015-12-15 12:09:44 +00:00
cancelCallback();
}
}
},
connecting: {
title: APP.translation.translateString('dialog.connecting'),
html: '<div id="connectionStatus"></div>',
buttons: [],
defaultButton: 0
},
finished: {
title: APP.translation.translateString('dialog.error'),
html: '<div id="errorMessage"></div>',
buttons: finishedButtons,
defaultButton: 0,
submit: function (e, v) {
e.preventDefault();
2015-12-15 12:09:44 +00:00
if (v === 'retry') {
connDialog.goToState('login');
2015-12-15 12:09:44 +00:00
} else {
2015-12-17 15:31:11 +00:00
// User cancelled
2015-12-15 12:09:44 +00:00
cancelCallback();
}
}
}
};
var connDialog = APP.UI.messageHandler.openDialogWithStates(
2015-12-15 12:09:44 +00:00
states, { persistent: true, closeText: '' }, null
);
/**
* Displays error message in 'finished' state which allows either to cancel
* or retry.
* @param message the final message to be displayed.
*/
this.displayError = function (message) {
2015-12-29 14:41:24 +00:00
let finishedState = connDialog.getState('finished');
2015-12-29 14:41:24 +00:00
let errorMessageElem = finishedState.find('#errorMessage');
errorMessageElem.text(message);
connDialog.goToState('finished');
};
2016-01-15 14:59:35 +00:00
/**
* Show message as connection status.
* @param {string} message
*/
2015-12-29 14:41:24 +00:00
this.displayConnectionStatus = function (message) {
let connectingState = connDialog.getState('connecting');
let connectionStatus = connectingState.find('#connectionStatus');
connectionStatus.text(message);
};
/**
* Closes LoginDialog.
*/
this.close = function () {
connDialog.close();
};
}
2016-01-15 14:59:35 +00:00
export default {
2016-01-15 14:59:35 +00:00
/**
* Show new auth dialog for JitsiConnection.
*
* @param {function(jid, password)} successCallback
* @param {function} [cancelCallback] callback to invoke if user canceled.
*
* @returns {LoginDialog}
*/
2015-12-17 15:31:11 +00:00
showAuthDialog: function (successCallback, cancelCallback) {
2016-01-15 14:59:35 +00:00
return new LoginDialog(successCallback, cancelCallback);
2015-12-15 12:09:44 +00:00
},
2016-01-15 14:59:35 +00:00
/**
* Show notification that external auth is required (using provided url).
* @param {string} url URL to use for external auth.
* @param {function} callback callback to invoke when auth popup is closed.
* @returns auth dialog
*/
2015-12-15 12:09:44 +00:00
showExternalAuthDialog: function (url, callback) {
var dialog = APP.UI.messageHandler.openCenteredPopup(
2015-12-15 12:09:44 +00:00
url, 910, 660,
// On closed
callback
);
if (!dialog) {
APP.UI.messageHandler.openMessageDialog(null, "dialog.popupError");
2015-12-15 12:09:44 +00:00
}
return dialog;
2015-12-15 16:29:37 +00:00
},
2016-01-15 14:59:35 +00:00
/**
* Show notification that authentication is required
* to create the conference, so he should authenticate or wait for a host.
* @param {string} roomName name of the conference
* @param {function} onAuthNow callback to invoke if
* user want to authenticate.
* @returns dialog
*/
2015-12-15 16:29:37 +00:00
showAuthRequiredDialog: function (roomName, onAuthNow) {
var title = APP.translation.generateTranslationHTML(
"dialog.WaitingForHost"
);
var msg = APP.translation.generateTranslationHTML(
"dialog.WaitForHostMsg", {room: roomName}
);
var buttonTxt = APP.translation.generateTranslationHTML(
"dialog.IamHost"
);
var buttons = [{title: buttonTxt, value: "authNow"}];
return APP.UI.messageHandler.openDialog(
title,
msg,
true,
buttons,
2015-12-17 15:31:11 +00:00
function (e, submitValue) {
2015-12-15 16:29:37 +00:00
// Do not close the dialog yet
2015-12-17 15:31:11 +00:00
e.preventDefault();
2015-12-15 16:29:37 +00:00
// Open login popup
if (submitValue === 'authNow') {
onAuthNow();
}
}
);
}
};