Skip to content

Commit

Permalink
getSessionID() should compare integer representations of Date objects (
Browse files Browse the repository at this point in the history
…#257)

* getSessionID() should compare integer value representations of Date objects.

Before the method was comparing a string representation (i.e., ttl)
to a number representation (i.e., Date.now()).

* fiveMinutesFromNow is now twoHoursFromNow
  • Loading branch information
simensta authored and shaunanoordin committed Mar 19, 2018
1 parent 6b3aee3 commit f7de22c
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/lib/get-session-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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}))
}
Expand All @@ -42,4 +42,10 @@ const fiveMinutesFromNow = () => {
return d;
};

const twoHoursFromNow = () => {
const d = new Date();
d.setHours(d.getHours() + 2);
return d;
};

export { generateSessionID, getSessionID };

0 comments on commit f7de22c

Please sign in to comment.