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' |
Helge | 9c0a19c | 2024-10-08 13:14:44 +0200 | [diff] [blame^] | 14 | * Expects a path or defaults to '/' |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 15 | */ |
Helge | 9c0a19c | 2024-10-08 13:14:44 +0200 | [diff] [blame^] | 16 | create : function (name = 'korap', path ="/") { |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 17 | const obj = Object.create(this); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 18 | obj._name = name.toLowerCase(); |
Helge | 9c0a19c | 2024-10-08 13:14:44 +0200 | [diff] [blame^] | 19 | obj._path = path; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 20 | obj._hash = {}; |
| 21 | obj._parse(); |
| 22 | return obj; |
| 23 | }, |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 24 | |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 25 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 26 | /** |
| 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 Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 33 | |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 34 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 35 | /** |
| 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 | }, |
Helge | 9c0a19c | 2024-10-08 13:14:44 +0200 | [diff] [blame^] | 43 | |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 44 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 45 | /** |
| 46 | * Clears the session by removing the cookie |
| 47 | */ |
| 48 | clear : function () { |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 49 | this._hash = {}; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 50 | document.cookie = this._name + '=; expires=-1'; |
| 51 | }, |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 52 | |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 53 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 54 | /* Store cookie */ |
| 55 | _store : function () { |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 56 | document.cookie = this.toString(); |
| 57 | }, |
| 58 | |
| 59 | |
| 60 | /** |
| 61 | * Stringify session cookie. |
| 62 | */ |
| 63 | toString : function () { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 64 | /* |
| 65 | var date = new Date(); |
| 66 | date.setYear(date.getFullYear() + 1); |
| 67 | */ |
Helge | 9c0a19c | 2024-10-08 13:14:44 +0200 | [diff] [blame^] | 68 | return this._name + '=' + encodeURIComponent(JSON.stringify(this._hash)) + ';SameSite=Lax' + ';path='+this._path; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 69 | }, |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 70 | |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Parse cookie |
| 74 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 75 | _parse : function () { |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 76 | document.cookie.split(';').forEach( |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 77 | function (i) { |
| 78 | const pair = i.split('='); |
| 79 | const name = pair[0].trim().toLowerCase(); |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 80 | if (name === this._name) { |
| 81 | if (pair.length === 1 || pair[1].length === 0) |
| 82 | return; |
| 83 | this._hash = JSON.parse(decodeURIComponent(pair[1])); |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 84 | return; |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 85 | }; |
Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 86 | }, |
| 87 | this); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 88 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 89 | }); |