Skip to content

Commit

Permalink
add newSession() method
Browse files Browse the repository at this point in the history
  • Loading branch information
eatyourgreens committed Nov 28, 2024
1 parent a2e4e9c commit 96718df
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/lib-classifier/src/store/utils/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,30 @@ import hash from 'hash.js'
const storage = window.sessionStorage

const sessionUtils = {
fiveMinutesFromNow () {
fiveMinutesFromNow() {
const d = new Date()
d.setMinutes(d.getMinutes() + 5)
return d
},

generateSessionID () {
generateSessionID() {
const sha2 = hash.sha256()
const id = sha2.update(`${Math.random() * 10000}${Date.now()}${Math.random() * 1000}`).digest('hex')
return id
},

getSessionID () {
newSession() {
return {
id: this.generateSessionID(),
ttl: this.fiveMinutesFromNow()
}
},

getSessionID() {
const storedSession = storage.getItem('session_id')
const stored = storedSession
? JSON.parse(storedSession)
: { id: this.generateSessionID(), ttl: new Date() }
: this.newSession()
stored.ttl = new Date(stored.ttl)

if (stored.ttl < Date.now()) {
Expand Down

0 comments on commit 96718df

Please sign in to comment.