aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/lib/cookie.js
blob: 6046d62d8201586402277cd27a9705dc246baa59 (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
class Cookie {
    static set(name, value, expiry_days) {
        var chunks = [name + '=' + value, 'SameSite=strict'];
        if (expiry_days !== undefined) {
            let date = new Date();
            date.setTime(date.getTime() + expiry_days * 24 * 60 * 60 * 1000);

            chunks.push(`expires=${date.toUTCString()}`);
        }
        chunks.push(
            'path=/' + STUDIP.URLHelper.getURL('a', {}, true)
                    .slice(0, -1)
                    .split('/')
                    .slice(3)
                    .map(encodeURIComponent)
                    .join('/')
        );

        document.cookie = chunks.join(';');
    }

    static get(name) {
        let chunks = document.cookie.split(';');
        var data = {};
        chunks.forEach(chunk => {
            let chunks = chunk.split('=');
            data[chunks[0].trim()] = chunks.slice(1).join('=');
        });

        return data[name];
    }
}

export default Cookie;