Skip to content

Commit

Permalink
Remove ttl logic from session id
Browse files Browse the repository at this point in the history
  • Loading branch information
goplayoutside3 committed Dec 9, 2024
1 parent 2342b1f commit f939961
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions app/lib/session.coffee
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
storage = window.sessionStorage ? window.localStorage
storage = window.sessionStorage
stored = JSON.parse storage?.getItem('session_id')

generateSessionID = () ->
hash = require('hash.js')
sha2 = hash.sha256()
id = sha2.update("#{Math.random() * 10000 }#{Date.now()}#{Math.random() * 1000}").digest('hex')
ttl = fiveMinutesFromNow()
stored = {id, ttl}
stored = {id}
try
storage.setItem('session_id', JSON.stringify(stored))
stored

getSessionID = () ->
{id, ttl} = JSON.parse(storage.getItem('session_id')) ? generateSessionID()
if ttl < Date.now()
{id} = generateSessionID()
else
ttl = fiveMinutesFromNow()
try
storage.setItem('session_id', JSON.stringify({id, ttl}))
{id} = JSON.parse(storage.getItem('session_id')) ? generateSessionID()
id

fiveMinutesFromNow = () ->
d = new Date()
d.setMinutes(d.getMinutes() + 5)
d

module.exports = {generateSessionID, getSessionID}

0 comments on commit f939961

Please sign in to comment.