# HG changeset patch # User Thomas Junk # Date 1530867892 -7200 # Node ID 4bf2173748f3ccc94081ada89ed4dd7a37f57be4 # Parent 7c40e9f28f941ddcd48bc397fe741a2670360876 refactor: extracted the string to milisecondconversion Extracted the conversion of session data to string conversion to Method. SRP. diff -r 7c40e9f28f94 -r 4bf2173748f3 client/src/lib/session.js --- a/client/src/lib/session.js Fri Jul 06 10:51:19 2018 +0200 +++ b/client/src/lib/session.js Fri Jul 06 11:04:52 2018 +0200 @@ -1,12 +1,12 @@ function sessionStillActive(expiresFromPastSession) { if (!expiresFromPastSession) return false; const now = Date.now(); - const sessionEnd = - typeof expiresFromPastSession === "string" - ? expiresFromPastSession * 1000 - : expiresFromPastSession; - const stillActive = now < sessionEnd; + const stillActive = now < expiresFromPastSession; return stillActive; } -export { sessionStillActive }; +function toMillisFromString(timestring) { + return timestring * 1000; +} + +export { sessionStillActive, toMillisFromString }; diff -r 7c40e9f28f94 -r 4bf2173748f3 client/src/router.js --- a/client/src/router.js Fri Jul 06 10:51:19 2018 +0200 +++ b/client/src/router.js Fri Jul 06 11:04:52 2018 +0200 @@ -3,7 +3,7 @@ import Login from "./views/Login.vue"; import Main from "./views/Main.vue"; import store from "./store"; -import { sessionStillActive } from "./lib/session"; +import { sessionStillActive, toMillisFromString } from "./lib/session"; Vue.use(Router); @@ -22,7 +22,9 @@ requiresAuth: true }, beforeEnter: (to, from, next) => { - const expiresFromPastSession = localStorage.getItem("expires"); + const expiresFromPastSession = toMillisFromString( + localStorage.getItem("expires") + ); if (sessionStillActive(expiresFromPastSession)) { store.commit("user/set_user", localStorage.getItem("user")); store.commit("user/set_expires", expiresFromPastSession); @@ -44,7 +46,9 @@ router.beforeEach((to, from, next) => { const requiresAuth = to.matched.some(record => record.meta.requiresAuth); const loggedIn = store.getters.authenticated; - const expiresFromPastSession = localStorage.getItem("expires"); + const expiresFromPastSession = toMillisFromString( + localStorage.getItem("expires") + ); const authRequired = requiresAuth && !(loggedIn || sessionStillActive(expiresFromPastSession)); if (authRequired) { diff -r 7c40e9f28f94 -r 4bf2173748f3 client/tests/unit/session/session.spec.js --- a/client/tests/unit/session/session.spec.js Fri Jul 06 10:51:19 2018 +0200 +++ b/client/tests/unit/session/session.spec.js Fri Jul 06 11:04:52 2018 +0200 @@ -1,11 +1,14 @@ -import { sessionStillActive } from "../../../src/lib/session"; +import { + sessionStillActive, + toMillisFromString +} from "../../../src/lib/session"; test("No session counts as expired session", () => { expect(sessionStillActive(null)).toBe(false); }); test("Session expired", () => { - const expired = Date.now() - 6000; + const expired = Date.now() - 60000; expect(sessionStillActive(expired)).toBe(false); }); @@ -15,11 +18,11 @@ }); test("Session alive with string", () => { - const active = "" + Date.now() + 60000; - expect(sessionStillActive(active)).toBe(true); + const active = "" + Date.now() / 1000 + 60000; + expect(sessionStillActive(toMillisFromString(active))).toBe(true); }); test("Session expired with string", () => { - const expired = "" + Date.now() - 60000; - expect(sessionStillActive(expired)).toBe(false); + const expired = "" + Date.now() / 1000 - 60000; + expect(sessionStillActive(toMillisFromString(expired))).toBe(false); });