summaryrefslogtreecommitdiff
path: root/js/auth.js
blob: 30eefb8ba1c24b3c43c2aa2aedb100849b58c156 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    var auth_username;
    var auth_token;

    function auth_loginSuccess(data) {
       if (data.status == 'OK') {
         auth_username = data.username;
         sessionStorage.setItem("auth_username", auth_username);

         auth_token = data.token;
         sessionStorage.setItem("auth_token", auth_token);

         apps_select('shows');

         $('#username-field').html(auth_username);
         $('#loginbox').slideUp();
         $('#mainwindow').fadeIn();
       } else {
         alertbox.error("Fehler beim Login", data.errorstring);
         auth_cleanup();
       }
    }

    function auth_loginError(req, status, error) {
       message = req.status + ': ' + error;
       if(req.status == 401) {
         message = "Benutzer und/oder Passwort sind falsch!";
       }
       alertbox.error("Fehler beim Login", message);
       $("#password").val('');
    }

    function auth_login()
    {
      $.ajax("/authtoken.json",
              { cache: false,
                username: $("#username").val(),
                password: $("#password").val(),
                dataType: "json",
                error: auth_loginError,
                success: auth_loginSuccess
              });
    }

    function auth_logout()
    {
      auth_cleanup();
      apps_cleanup();

      $(".alert").alert('close');
      $("#username").val('');
      $("#password").val('');
      $("#mainwindow").fadeOut();
      $('#username-field').html('');
      $('#loginbox').slideDown();
    }

    function auth_init() {
      auth_username = sessionStorage.getItem("auth_username");
      auth_token = sessionStorage.getItem("auth_token");

      if(auth_token && auth_username) {
        $("#loginbox").hide();
        $('#username-field').html(auth_username);
      } else {
        $("#mainwindow").hide();
      }
      $("#loginform").submit(function(event) { auth_login(); event.preventDefault(); });
    }

    function auth_cleanup() {
      sessionStorage.removeItem("auth_username");
      delete auth_username;
      $("#username").val('').focus();

      sessionStorage.removeItem("auth_token");
      delete auth_token;
      $("#password").val('');
    }