window.matrixLogin = { endpoint: location.origin + "/_matrix/client/r0/login", serverAcceptsPassword: false, serverAcceptsSso: false, }; // Titles get updated through the process to give users feedback. var TITLE_PRE_AUTH = "Log in with one of the following methods"; var TITLE_POST_AUTH = "Logging in..."; // The cookie used to store the original query parameters when using SSO. var COOKIE_KEY = "synapse_login_fallback_qs"; /* * Submit a login request. * * type: The login type as a string (e.g. "m.login.foo"). * data: An object of data specific to the login type. * extra: (Optional) An object to search for extra information to send with the * login request, e.g. device_id. * callback: (Optional) Function to call on successful login. */ var submitLogin = function(type, data, extra, callback) { console.log("Logging in with " + type); set_title(TITLE_POST_AUTH); // Add the login type. data.type = type; // Add the device information, if it was provided. if (extra.device_id) { data.device_id = extra.device_id; } if (extra.initial_device_display_name) { data.initial_device_display_name = extra.initial_device_display_name; } $.post(matrixLogin.endpoint, JSON.stringify(data), function(response) { if (callback) { callback(); } 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) { // Set the redirect to come back to this page, a login token will get added // and handled after the 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) { // Before submitting SSO, set the current query parameters into a cookie // for retrieval later. var qs = parseQsFromUrl(); setCookie(COOKIE_KEY, JSON.stringify(qs)); 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