window.matrixLogin = { endpoint: location.origin + "/_matrix/client/r0/login", serverAcceptsPassword: false, serverAcceptsSso: false, }; var title_pre_auth = "Log in with one of the following methods"; var title_post_auth = "Logging in..."; var submitPassword = function(user, pwd) { console.log("Logging in with password..."); set_title(title_post_auth); var data = { type: "m.login.password", user: user, password: pwd, }; $.post(matrixLogin.endpoint, JSON.stringify(data), function(response) { matrixLogin.onLogin(response); }).fail(errorFunc); }; var submitToken = function(loginToken) { console.log("Logging in with login token..."); set_title(title_post_auth); var data = { type: "m.login.token", token: loginToken }; $.post(matrixLogin.endpoint, JSON.stringify(data), function(response) { matrixLogin.onLogin(response); }).fail(errorFunc); }; var errorFunc = function(err) { // We want to show the error to the user rather than redirecting immediately to the // SSO portal (if SSO is the only login option), so we inhibit the redirect. show_login(true); if (err.responseJSON && err.responseJSON.error) { setFeedbackString(err.responseJSON.error + " (" + err.responseJSON.errcode + ")"); } else { setFeedbackString("Request failed: " + err.status); } }; var setFeedbackString = function(text) { $("#feedback").text(text); }; var show_login = function(inhibit_redirect) { var this_page = window.location.origin + window.location.pathname; $("#sso_redirect_url").val(this_page); // If inhibit_redirect is false, and SSO is the only supported login method, we can // redirect straight to the SSO page if (matrixLogin.serverAcceptsSso) { if (!inhibit_redirect && !matrixLogin.serverAcceptsPassword) { $("#sso_form").submit(); return; } // Otherwise, show the SSO form $("#sso_flow").show(); } if (matrixLogin.serverAcceptsPassword) { $("#password_flow").show(); } if (!matrixLogin.serverAcceptsPassword && !matrixLogin.serverAcceptsSso) { $("#no_login_types").show(); } set_title(title_pre_auth); $("#loading").hide(); }; var show_spinner = function() { $("#password_flow").hide(); $("#sso_flow").hide(); $("#no_login_types").hide(); $("#loading").show(); }; var set_title = function(title) { $("#title").text(title); }; var fetch_info = function(cb) { $.get(matrixLogin.endpoint, function(response) { var serverAcceptsPassword = false; for (var i=0; i