Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Simple cookie based session library that stores |
| 3 | * values in JSON encoded cookies. |
| 4 | * |
| 5 | * @author Nils Diewald |
| 6 | */ |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 7 | |
| 8 | "use strict"; |
| 9 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 10 | define({ |
| 11 | /** |
| 12 | * Create a new session. |
| 13 | * Expects a name or defaults to 'korap' |
| 14 | */ |
Akron | 3b253d3 | 2018-07-15 10:16:06 +0200 | [diff] [blame] | 15 | create : function (name = 'korap') { |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 16 | const obj = Object.create(this); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 17 | obj._name = name.toLowerCase(); |
| 18 | obj._hash = {}; |
| 19 | obj._parse(); |
| 20 | return obj; |
| 21 | }, |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 22 | |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 23 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 24 | /** |
| 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 Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 31 | |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 32 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 33 | /** |
| 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 Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 41 | |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 42 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 43 | /** |
| 44 | * Clears the session by removing the cookie |
| 45 | */ |
| 46 | clear : function () { |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 47 | this._hash = {}; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 48 | document.cookie = this._name + '=; expires=-1'; |
| 49 | }, |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 50 | |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 51 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 52 | /* Store cookie */ |
| 53 | _store : function () { |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 54 | document.cookie = this.toString(); |
| 55 | }, |
| 56 | |
| 57 | |
| 58 | /** |
| 59 | * Stringify session cookie. |
| 60 | */ |
| 61 | toString : function () { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 62 | /* |
| 63 | var date = new Date(); |
| 64 | date.setYear(date.getFullYear() + 1); |
| 65 | */ |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 66 | return this._name + '=' + encodeURIComponent(JSON.stringify(this._hash)) + ';'; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 67 | }, |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 68 | |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Parse cookie |
| 72 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 73 | _parse : function () { |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 74 | document.cookie.split(';').forEach( |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 75 | function (i) { |
| 76 | const pair = i.split('='); |
| 77 | const name = pair[0].trim().toLowerCase(); |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 78 | if (name === this._name) { |
| 79 | if (pair.length === 1 || pair[1].length === 0) |
| 80 | return; |
| 81 | this._hash = JSON.parse(decodeURIComponent(pair[1])); |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 82 | return; |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 83 | }; |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 84 | }, |
| 85 | this); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 86 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 87 | }); |