aboutsummaryrefslogtreecommitdiff
path: root/src/js/Botleagues.js
blob: 35c8c0df6b0f499be8df7cb8ce7dbbc2d944ff49 (plain) (blame)
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
function Botleagues(){}

Botleagues.url = 'https://api.local.botleagues.camilstaps.nl';

$.cookie.json = true;
Botleagues.cookie_login_email = 'botleagues_login_email';
Botleagues.cookie_login_user_id = 'botleagues_login_user_id';
Botleagues.cookie_login_token = 'botleagues_login_token';
Botleagues.cookie_login_valid_till = 'botleagues_login_valid_till';

Botleagues.request = function(user_options) {
    var callback = user_options.callback;

    var options = {
        endpoint: null,
        method: 'GET',
        dataType: 'json',
        complete: function(data) {
            callback(data.responseJSON);
        }
    };

    var url = Botleagues.url + '/' + user_options['endpoint'];
    delete user_options['callback'];
    delete user_options['endpoint'];

    for (var name in user_options) {
        options[name] = user_options[name];
    }

    jQuery.ajax(url, options);
};

Botleagues.redirect = function(user_options) {
    window.location = Botleagues.url + '/' + user_options['endpoint'];
};

Botleagues.login = function(email, password) {
    Botleagues.request({
        endpoint: 'user_token',
        method: 'POST',
        callback: BotleaguesCallback.login,
        headers: {
            'Authorization': 'Basic ' + btoa(email + ':' + password)
        }
    });

    $.cookie(Botleagues.cookie_login_email, email);
};

Botleagues.logout = function(){
    Botleagues.clearLoginCookies();
    window.location = '/';
};

Botleagues.clearLoginCookies = function() {
    $.removeCookie(Botleagues.cookie_login_email);
    $.removeCookie(Botleagues.cookie_login_user_id);
    $.removeCookie(Botleagues.cookie_login_token);
    $.removeCookie(Botleagues.cookie_login_valid_till);
};