- var username = this.find("input[name='username']").val();
- var password = this.find("input[name='pass']").val();
- var force = this.find("input[name='force']").is(":checked");
- var url = mctx.api + "control";
-
- var authFunc = function(xhr) {
- xhr.setRequestHeader("Authorization",
- "Basic " + base64.encode(username + ":" + password));
- };
-
- $.ajax({
- url : url,
- data : {action : "lock", force : (force ? true : undefined)},
- beforeSend : authFunc
- }).done(function (data) {
- mctx.key = data.key;
- if (data.status < 0) {
- alert("no - " + data.description);
- } else {
- mctx.has_control = true;
- alert("yes - " + mctx.key);
- }
- }).fail(function (jqXHR) {
- mctx.key = undefined;
- mctx.has_control = false;
- alert("no");
- });
+ 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";
+ });