From f83bd20fa9460d475387c400dd32074f745e17b8 Mon Sep 17 00:00:00 2001 From: dkanada Date: Sat, 6 Apr 2019 23:07:02 -0700 Subject: [PATCH 1/2] deminify login controller --- src/controllers/loginpage.js | 152 ++++++++++++++++++++++++----------- 1 file changed, 103 insertions(+), 49 deletions(-) diff --git a/src/controllers/loginpage.js b/src/controllers/loginpage.js index fc8369c29a..b3e5c632cc 100644 --- a/src/controllers/loginpage.js +++ b/src/controllers/loginpage.js @@ -2,89 +2,143 @@ define(["appSettings", "dom", "connectionManager", "loading", "cardStyle", "emby "use strict"; function authenticateUserByName(page, apiClient, username, password) { - loading.show(), apiClient.authenticateUserByName(username, password).then(function(result) { - var newUrl, user = result.User, - serverId = getParameterByName("serverid"); - newUrl = user.Policy.IsAdministrator && !serverId ? "dashboard.html" : "home.html", loading.hide(), Dashboard.onServerChanged(user.Id, result.AccessToken, apiClient), Dashboard.navigate(newUrl) + loading.show(); + apiClient.authenticateUserByName(username, password).then(function(result) { + var user = result.User; + var serverId = getParameterByName("serverid"); + var newUrl = user.Policy.IsAdministrator && !serverId ? "dashboard.html" : "home.html"; + loading.hide(); + Dashboard.onServerChanged(user.Id, result.AccessToken, apiClient); + Dashboard.navigate(newUrl); }, function(response) { - page.querySelector("#txtManualName").value = "", page.querySelector("#txtManualPassword").value = "", loading.hide(), 401 == response.status ? require(["toast"], function(toast) { - toast(Globalize.translate("MessageInvalidUser")) - }) : showServerConnectionFailure() - }) - } - - function showServerConnectionFailure() { - Dashboard.alert({ - message: Globalize.translate("MessageUnableToConnectToServer"), - title: Globalize.translate("HeaderConnectionFailure") - }) + page.querySelector("#txtManualName").value = ""; + page.querySelector("#txtManualPassword").value = ""; + loading.hide(); + if (response.status === 401) { + require(["toast"], function(toast) { + toast(Globalize.translate("MessageInvalidUser")); + }); + } else { + Dashboard.alert({ + message: Globalize.translate("MessageUnableToConnectToServer"), + title: Globalize.translate("HeaderConnectionFailure") + }); + } + }); } function showManualForm(context, showCancel, focusPassword) { - context.querySelector(".chkRememberLogin").checked = appSettings.enableAutoLogin(), context.querySelector(".manualLoginForm").classList.remove("hide"), context.querySelector(".visualLoginForm").classList.add("hide"), focusPassword ? context.querySelector("#txtManualPassword").focus() : context.querySelector("#txtManualName").focus(), showCancel ? context.querySelector(".btnCancel").classList.remove("hide") : context.querySelector(".btnCancel").classList.add("hide") + context.querySelector(".chkRememberLogin").checked = appSettings.enableAutoLogin(); + context.querySelector(".manualLoginForm").classList.remove("hide"); + context.querySelector(".visualLoginForm").classList.add("hide"); + focusPassword ? context.querySelector("#txtManualPassword").focus() : context.querySelector("#txtManualName").focus(); + showCancel ? context.querySelector(".btnCancel").classList.remove("hide") : context.querySelector(".btnCancel").classList.add("hide"); } + var metroColors = ["#6FBD45", "#4BB3DD", "#4164A5", "#E12026", "#800080", "#E1B222", "#008040", "#0094FF", "#FF00C7", "#FF870F", "#7F0037"]; + function getRandomMetroColor() { var index = Math.floor(Math.random() * (metroColors.length - 1)); - return metroColors[index] + return metroColors[index]; } function getMetroColor(str) { if (str) { - for (var character = String(str.substr(0, 1).charCodeAt()), sum = 0, i = 0; i < character.length; i++) sum += parseInt(character.charAt(i)); + var character = String(str.substr(0, 1).charCodeAt()); + var sum = 0; + for (var i = 0; i < character.length; i++) { + sum += parseInt(character.charAt(i)); + } var index = String(sum).substr(-1); - return metroColors[index] + return metroColors[index]; } - return getRandomMetroColor() + return getRandomMetroColor(); } function loadUserList(context, apiClient, users) { - for (var html = "", i = 0, length = users.length; i < length; i++) { + var html = ""; + for (var i = 0; i < users.length; i++) { var user = users[i]; - html += '" + html += ""; + html += ""; + html += '
'; + html += '
' + user.Name + "
"; + html += "
"; + html += ""; + html += ""; } - context.querySelector("#divUsers").innerHTML = html + context.querySelector("#divUsers").innerHTML = html; } - var metroColors = ["#6FBD45", "#4BB3DD", "#4164A5", "#E12026", "#800080", "#E1B222", "#008040", "#0094FF", "#FF00C7", "#FF870F", "#7F0037"]; + return function(view, params) { function getApiClient() { var serverId = params.serverid; - return serverId ? connectionManager.getOrCreateApiClient(serverId) : ApiClient + return serverId ? connectionManager.getOrCreateApiClient(serverId) : ApiClient; } function showVisualForm() { - view.querySelector(".visualLoginForm").classList.remove("hide"), view.querySelector(".manualLoginForm").classList.add("hide") + view.querySelector(".visualLoginForm").classList.remove("hide"); + view.querySelector(".manualLoginForm").classList.add("hide"); } + view.querySelector("#divUsers").addEventListener("click", function(e) { - var card = dom.parentWithClass(e.target, "card"), - cardContent = card ? card.querySelector(".cardContent") : null; + var card = dom.parentWithClass(e.target, "card"); + var cardContent = card ? card.querySelector(".cardContent") : null; if (cardContent) { - var context = view, - id = cardContent.getAttribute("data-userid"), - name = cardContent.getAttribute("data-username"), - haspw = cardContent.getAttribute("data-haspw"); - "manual" == id ? (context.querySelector("#txtManualName").value = "", showManualForm(context, !0)) : "false" == haspw ? authenticateUserByName(context, getApiClient(), name, "") : (context.querySelector("#txtManualName").value = name, context.querySelector("#txtManualPassword").value = "", showManualForm(context, !0, !0)) + var context = view; + var id = cardContent.getAttribute("data-userid"); + var name = cardContent.getAttribute("data-username"); + var haspw = cardContent.getAttribute("data-haspw"); + if (id === 'manual') { + context.querySelector("#txtManualName").value = ""; + showManualForm(context, true); + } else if (haspw == 'false') { + authenticateUserByName(context, getApiClient(), name, ""); + } else { + context.querySelector("#txtManualName").value = name; + context.querySelector("#txtManualPassword").value = ""; + showManualForm(context, true, true); + } } - }), view.querySelector(".manualLoginForm").addEventListener("submit", function(e) { + }); + + view.querySelector(".manualLoginForm").addEventListener("submit", function(e) { appSettings.enableAutoLogin(view.querySelector(".chkRememberLogin").checked); var apiClient = getApiClient(); - return authenticateUserByName(view, apiClient, view.querySelector("#txtManualName").value, view.querySelector("#txtManualPassword").value), e.preventDefault(), !1 - }), view.querySelector(".btnForgotPassword").addEventListener("click", function() { - Dashboard.navigate("forgotpassword.html") - }), view.querySelector(".btnCancel").addEventListener("click", showVisualForm), view.querySelector(".btnManual").addEventListener("click", function() { - view.querySelector("#txtManualName").value = "", showManualForm(view, !0) - }), view.addEventListener("viewshow", function(e) { + authenticateUserByName(view, apiClient, view.querySelector("#txtManualName").value, view.querySelector("#txtManualPassword").value); + e.preventDefault(); + return false; + }); + + view.querySelector(".btnForgotPassword").addEventListener("click", function() { + Dashboard.navigate("forgotpassword.html"); + }); + + view.querySelector(".btnCancel").addEventListener("click", showVisualForm); + + view.querySelector(".btnManual").addEventListener("click", function() { + view.querySelector("#txtManualName").value = ""; + showManualForm(view, true); + }); + + view.addEventListener("viewshow", function(e) { loading.show(); var apiClient = getApiClient(); apiClient.getPublicUsers().then(function(users) { @@ -105,7 +159,7 @@ define(["appSettings", "dom", "connectionManager", "loading", "cardStyle", "emby }); apiClient.getJSON(apiClient.getUrl("Branding/Configuration")).then(function(options) { - view.querySelector(".disclaimer").textContent = options.LoginDisclaimer || "" + view.querySelector(".disclaimer").textContent = options.LoginDisclaimer || ""; }); }); } From 38e3861e85be0d29e2dfdbaaed6c4120887fa7e5 Mon Sep 17 00:00:00 2001 From: dkanada Date: Sat, 6 Apr 2019 23:18:47 -0700 Subject: [PATCH 2/2] add check for multiserver on login page --- src/controllers/loginpage.js | 5 ++++- src/login.html | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/controllers/loginpage.js b/src/controllers/loginpage.js index b3e5c632cc..2f396ff5eb 100644 --- a/src/controllers/loginpage.js +++ b/src/controllers/loginpage.js @@ -1,4 +1,4 @@ -define(["appSettings", "dom", "connectionManager", "loading", "cardStyle", "emby-checkbox"], function(appSettings, dom, connectionManager, loading) { +define(["apphost", "appSettings", "dom", "connectionManager", "loading", "cardStyle", "emby-checkbox"], function(appHost, appSettings, dom, connectionManager, loading) { "use strict"; function authenticateUserByName(page, apiClient, username, password) { @@ -140,6 +140,9 @@ define(["appSettings", "dom", "connectionManager", "loading", "cardStyle", "emby view.addEventListener("viewshow", function(e) { loading.show(); + if (!appHost.supports('multiserver')) { + view.querySelector(".btnSelectServer").classList.add("hide"); + } var apiClient = getApiClient(); apiClient.getPublicUsers().then(function(users) { if (users.length) { diff --git a/src/login.html b/src/login.html index 100dc7a3b9..c70064ed95 100644 --- a/src/login.html +++ b/src/login.html @@ -47,7 +47,7 @@ ${ButtonForgotPassword} - + ${ButtonChangeServer}