blob: fe8de4c420aa748a827ed42a99b3f7782ab591b6 [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'
Helge9c0a19c2024-10-08 13:14:44 +020014 * Expects a path or defaults to '/'
Nils Diewald0e6992a2015-04-14 20:13:52 +000015 */
Helge9c0a19c2024-10-08 13:14:44 +020016 create : function (name = 'korap', path ="/") {
Akron7650eaa2020-10-19 17:10:30 +020017 const obj = Object.create(this);
Nils Diewald0e6992a2015-04-14 20:13:52 +000018 obj._name = name.toLowerCase();
Helge9c0a19c2024-10-08 13:14:44 +020019 obj._path = path;
Nils Diewald0e6992a2015-04-14 20:13:52 +000020 obj._hash = {};
21 obj._parse();
22 return obj;
23 },
Nils Diewald58141332015-04-07 16:18:45 +000024
Akron7650eaa2020-10-19 17:10:30 +020025
Nils Diewald0e6992a2015-04-14 20:13:52 +000026 /**
27 * Get a value based on a key.
28 * The value can be complex, as the value is stored as JSON.
29 */
30 get : function (key) {
31 return this._hash[key.toLowerCase()];
32 },
Nils Diewald58141332015-04-07 16:18:45 +000033
Akron7650eaa2020-10-19 17:10:30 +020034
Nils Diewald0e6992a2015-04-14 20:13:52 +000035 /**
36 * Set a value based on a key.
37 * The value can be complex, as the value is stored as JSON.
38 */
39 set : function (key, value) {
40 this._hash[key] = value;
41 this._store();
42 },
Helge9c0a19c2024-10-08 13:14:44 +020043
Akron7650eaa2020-10-19 17:10:30 +020044
Nils Diewald0e6992a2015-04-14 20:13:52 +000045 /**
46 * Clears the session by removing the cookie
47 */
48 clear : function () {
Akron7650eaa2020-10-19 17:10:30 +020049 this._hash = {};
Nils Diewald0e6992a2015-04-14 20:13:52 +000050 document.cookie = this._name + '=; expires=-1';
51 },
Nils Diewald58141332015-04-07 16:18:45 +000052
Akron7650eaa2020-10-19 17:10:30 +020053
Nils Diewald0e6992a2015-04-14 20:13:52 +000054 /* Store cookie */
55 _store : function () {
Akron7650eaa2020-10-19 17:10:30 +020056 document.cookie = this.toString();
57 },
58
59
60 /**
61 * Stringify session cookie.
62 */
63 toString : function () {
Nils Diewald0e6992a2015-04-14 20:13:52 +000064 /*
65 var date = new Date();
66 date.setYear(date.getFullYear() + 1);
67 */
Helge9c0a19c2024-10-08 13:14:44 +020068 return this._name + '=' + encodeURIComponent(JSON.stringify(this._hash)) + ';SameSite=Lax' + ';path='+this._path;
Nils Diewald0e6992a2015-04-14 20:13:52 +000069 },
Nils Diewald58141332015-04-07 16:18:45 +000070
Akron7650eaa2020-10-19 17:10:30 +020071
72 /**
73 * Parse cookie
74 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000075 _parse : function () {
Akronb50964a2020-10-12 11:44:37 +020076 document.cookie.split(';').forEach(
Akron7650eaa2020-10-19 17:10:30 +020077 function (i) {
78 const pair = i.split('=');
79 const name = pair[0].trim().toLowerCase();
Akronb50964a2020-10-12 11:44:37 +020080 if (name === this._name) {
81 if (pair.length === 1 || pair[1].length === 0)
82 return;
83 this._hash = JSON.parse(decodeURIComponent(pair[1]));
Akron0b489ad2018-02-02 16:49:32 +010084 return;
Akronb50964a2020-10-12 11:44:37 +020085 };
Akron7650eaa2020-10-19 17:10:30 +020086 },
87 this);
Nils Diewald58141332015-04-07 16:18:45 +000088 }
Nils Diewald0e6992a2015-04-14 20:13:52 +000089});