aboutsummaryrefslogtreecommitdiff
path: root/src/js/Botleagues.coffee
blob: 285248f1a0a748668c5c5cdeb7d2e1b4d2899c5a (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
$.cookie.json = true

@Botleagues =

    url: 'https://api.local.botleagues.camilstaps.nl'

    cookie_login_email: 'botleagues_login_email'
    cookie_login_token: 'botleagues_login_token'
    cookie_login_valid_till: 'botleagues_login_valid_till'

    request: (user_options) ->
        callback = user_options.callback

        options =
            method: 'GET'
            dataType: 'json'
            complete: (data) ->
                callback data.responseJSON

        url = Botleagues.url + '/' + user_options.endpoint
        delete user_options.callback
        delete user_options.endpoint

        for key, option of user_options
            options[key] = option

        jQuery.ajax url, options

        return

    login: (email, password) ->
        Botleagues.request
            endpoint: 'user/' + email + '/token'
            method: 'POST'
            callback: BotleaguesCallback.login
            headers:
                'Authorization': 'Basic ' + btoa(email + ':' + password)

        $.cookie Botleagues.cookie_login_email, email

        return

    logout: ->
        Botleagues.clearLoginCookies()
        window.location = '/'
        return

    clearLoginCookies: ->
        $.removeCookie Botleagues.cookie_login_email
        $.removeCookie Botleagues.cookie_login_token
        $.removeCookie Botleagues.cookie_login_valid_till
        return