- var username = this.find("input[name='username']").val();
- var password = this.find("input[name='pass']").val();
- var out = this.find("#result");
- var redirect = function () {
- window.location.href = mctx.location;
- };
-
- out.removeAttr("class");
- out.text("Logging in...");
-
- $.ajax({
- url : mctx.api + "bind",
- data : {user: username, pass : password}
- }).done(function (data) {
- //todo: error check
- mctx.has_control = true;
- out.attr("class", "pass");
- out.text("Login ok!");
- setTimeout(redirect, 1000);
- }).fail(function (jqXHR) {
- mctx.has_control = false;
- out.attr("class", "fail");
- out.text("Login request failed - connection issues.")
- });
+ var username = this.find("input[name='username']").val();
+ var password = this.find("input[name='pass']").val();
+ var out = this.find("#result");
+ var redirect = function () {
+ window.location.href = mctx.location;
+ };
+
+ out.removeAttr("class");
+ out.text("Logging in...");
+
+ return $.ajax({
+ url : mctx.api + "bind",
+ data : {user: username, pass : password}
+ }).done(function (data) {
+ if (data.status < 0) {
+ mctx.has_control = false;
+ out.attr("class", "fail");
+ out.text("Login failed: " + data.description);
+ } else {
+ //todo: error check
+ mctx.has_control = true;
+ out.attr("class", "pass");
+ out.text("Login ok!");
+ setTimeout(redirect, 800);
+ }
+ }).fail(function (jqXHR) {
+ mctx.has_control = false;
+ out.attr("class", "fail");
+ out.text("Login request failed - connection issues.")
+ });
+};
+
+/**
+* Performs a logout request. The nameless cookie is
+* always cleared and the browser redirected to the login page,
+* independent of whether or not logout succeeded.
+* @returns The AJAX object of the logout request.
+*/
+$.fn.logout = function () {
+ return $.ajax({
+ url : mctx.api + "unbind"
+ }).always(function () {
+ //Note: this only clears the nameless cookie
+ document.cookie = "";
+ window.location = mctx.location + "login.html";
+ });