diff --git a/src/lib/get-session-id.js b/src/lib/get-session-id.js index a614d9c1..a26fbea5 100644 --- a/src/lib/get-session-id.js +++ b/src/lib/get-session-id.js @@ -4,7 +4,7 @@ const generateSessionID = () => { const hash = require('hash.js'); const sha2 = hash.sha256(); const id = sha2.update(`${Math.random() * 10000 }${Date.now()}${Math.random() * 1000}`).digest('hex'); - const ttl = fiveMinutesFromNow(); + const ttl = twoHoursFromNow(); const stored = {id, ttl}; try { sessionStorage.setItem('session_id', JSON.stringify(stored)); @@ -22,10 +22,10 @@ const getSessionID = () => { ({id, ttl} = JSON.parse(sessionStorage.getItem('session_id'))); } - if (ttl < Date.now()) { + if (new Date(ttl).getTime() < Date.now()) { id = generateSessionID(); } else { - ttl = fiveMinutesFromNow(); + ttl = twoHoursFromNow(); try { sessionStorage.setItem('session_id', JSON.stringify({id, ttl})) } @@ -42,4 +42,10 @@ const fiveMinutesFromNow = () => { return d; }; +const twoHoursFromNow = () => { + const d = new Date(); + d.setHours(d.getHours() + 2); + return d; +}; + export { generateSessionID, getSessionID };