blob: 92bbadf8163e00020ff0e3301eb7599bbff319b3 [file] [log] [blame]
Nils Diewald58141332015-04-07 16:18:45 +00001/**
2 * Simple cookie based session library that stores
3 * values in JSON encoded cookies.
4 *
5 * @author Nils Diewald
6 */
Akron7650eaa2020-10-19 17:10:30 +02007
8"use strict";
9
Nils Diewald0e6992a2015-04-14 20:13:52 +000010define({
11 /**
12 * Create a new session.
13 * Expects a name or defaults to 'korap'
14 */
Akron3b253d32018-07-15 10:16:06 +020015 create : function (name = 'korap') {
Akron7650eaa2020-10-19 17:10:30 +020016 const obj = Object.create(this);
Nils Diewald0e6992a2015-04-14 20:13:52 +000017 obj._name = name.toLowerCase();
18 obj._hash = {};
19 obj._parse();
20 return obj;
21 },
Nils Diewald58141332015-04-07 16:18:45 +000022
Akron7650eaa2020-10-19 17:10:30 +020023
Nils Diewald0e6992a2015-04-14 20:13:52 +000024 /**
25 * Get a value based on a key.
26 * The value can be complex, as the value is stored as JSON.
27 */
28 get : function (key) {
29 return this._hash[key.toLowerCase()];
30 },
Nils Diewald58141332015-04-07 16:18:45 +000031
Akron7650eaa2020-10-19 17:10:30 +020032
Nils Diewald0e6992a2015-04-14 20:13:52 +000033 /**
34 * Set a value based on a key.
35 * The value can be complex, as the value is stored as JSON.
36 */
37 set : function (key, value) {
38 this._hash[key] = value;
39 this._store();
40 },
Nils Diewald58141332015-04-07 16:18:45 +000041
Akron7650eaa2020-10-19 17:10:30 +020042
Nils Diewald0e6992a2015-04-14 20:13:52 +000043 /**
44 * Clears the session by removing the cookie
45 */
46 clear : function () {
Akron7650eaa2020-10-19 17:10:30 +020047 this._hash = {};
Nils Diewald0e6992a2015-04-14 20:13:52 +000048 document.cookie = this._name + '=; expires=-1';
49 },
Nils Diewald58141332015-04-07 16:18:45 +000050
Akron7650eaa2020-10-19 17:10:30 +020051
Nils Diewald0e6992a2015-04-14 20:13:52 +000052 /* Store cookie */
53 _store : function () {
Akron7650eaa2020-10-19 17:10:30 +020054 document.cookie = this.toString();
55 },
56
57
58 /**
59 * Stringify session cookie.
60 */
61 toString : function () {
Nils Diewald0e6992a2015-04-14 20:13:52 +000062 /*
63 var date = new Date();
64 date.setYear(date.getFullYear() + 1);
65 */
Akron7650eaa2020-10-19 17:10:30 +020066 return this._name + '=' + encodeURIComponent(JSON.stringify(this._hash)) + ';';
Nils Diewald0e6992a2015-04-14 20:13:52 +000067 },
Nils Diewald58141332015-04-07 16:18:45 +000068
Akron7650eaa2020-10-19 17:10:30 +020069
70 /**
71 * Parse cookie
72 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000073 _parse : function () {
Akronb50964a2020-10-12 11:44:37 +020074 document.cookie.split(';').forEach(
Akron7650eaa2020-10-19 17:10:30 +020075 function (i) {
76 const pair = i.split('=');
77 const name = pair[0].trim().toLowerCase();
Akronb50964a2020-10-12 11:44:37 +020078 if (name === this._name) {
79 if (pair.length === 1 || pair[1].length === 0)
80 return;
81 this._hash = JSON.parse(decodeURIComponent(pair[1]));
Akron0b489ad2018-02-02 16:49:32 +010082 return;
Akronb50964a2020-10-12 11:44:37 +020083 };
Akron7650eaa2020-10-19 17:10:30 +020084 },
85 this);
Nils Diewald58141332015-04-07 16:18:45 +000086 }
Nils Diewald0e6992a2015-04-14 20:13:52 +000087});