Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Classifier: Remove the faulty session id code #7233

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature is supposed to be a session id, which refreshes just like a browser session. If researchers are using session ids to analyze anonymous users, then don't use localStorage as a backup.

See https://github.com/zooniverse/front-end-monorepo/pull/6499/files#r1861103668

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()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition was comparing a Date() string to a Date.now() number and therefore never met. It was copied directly from PFE's codebase, so I'll be doing the same cleanup over there.

{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}
Loading