Mercurial > gemma
changeset 165:4bf2173748f3
refactor: extracted the string to milisecondconversion
Extracted the conversion of session data to string conversion
to Method. SRP.
author | Thomas Junk <thomas.junk@intevation.de> |
---|---|
date | Fri, 06 Jul 2018 11:04:52 +0200 |
parents | 7c40e9f28f94 |
children | 8f49ba6cddd9 |
files | client/src/lib/session.js client/src/router.js client/tests/unit/session/session.spec.js |
diffstat | 3 files changed, 22 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- 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 };
--- 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) {
--- 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); });