# HG changeset patch # User Thomas Junk # Date 1530806499 -7200 # Node ID 9908260d1e6a530be7d093748e5af7fc70580011 # Parent b7ac2e4f9c5cff9101c6b755e707cd3ec6cb0dae Refactor: Login expiry refactored to lib test: First unit test for session Cleaned up Code regarding expired sessions. First unit test established for session mostly for educational purposes. diff -r b7ac2e4f9c5c -r 9908260d1e6a client/src/lib/session.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/src/lib/session.js Thu Jul 05 18:01:39 2018 +0200 @@ -0,0 +1,9 @@ +function sessionStillActive(expiresFromPastSession) { + if (!expiresFromPastSession) return false; + const now = Date.now(); + const sessionEnd = expiresFromPastSession; + const stillActive = now < sessionEnd; + return stillActive; +} + +export { sessionStillActive }; diff -r b7ac2e4f9c5c -r 9908260d1e6a client/src/router.js --- a/client/src/router.js Thu Jul 05 16:52:39 2018 +0200 +++ b/client/src/router.js Thu Jul 05 18:01:39 2018 +0200 @@ -3,6 +3,7 @@ import Login from "./views/Login.vue"; import Main from "./views/Main.vue"; import store from "./store"; +import { sessionStillActive } from "./lib/session"; Vue.use(Router); @@ -22,18 +23,13 @@ }, beforeEnter: (to, from, next) => { const expiresFromPastSession = localStorage.getItem("expires"); - const pastSessionStillActive = () => { - if (!expiresFromPastSession) return false; - const now = new Date(); - const sessionEnd = new Date(expiresFromPastSession * 1000); - const stillActive = now < sessionEnd; - return stillActive; - }; - if (pastSessionStillActive()) { + if (sessionStillActive(expiresFromPastSession)) { store.commit("user/set_user", localStorage.getItem("user")); store.commit("user/set_expires", expiresFromPastSession); store.commit("user/set_roles", localStorage.getItem("roles")); - store.commit("user/authenticate", true); + store.commit("user/set_authenticate", true); + } else { + store.commit("user/clear_auth"); } next(); } @@ -47,11 +43,13 @@ router.beforeEach((to, from, next) => { const requiresAuth = to.matched.some(record => record.meta.requiresAuth); - const currentUser = localStorage.getItem("token") != null; - const userIsAuthenticated = requiresAuth && !currentUser; - if (userIsAuthenticated) { + const loggedIn = store.getters.authenticated; + const expiresFromPastSession = localStorage.getItem("expires"); + const authRequired = + requiresAuth && !(loggedIn || sessionStillActive(expiresFromPastSession)); + if (authRequired) { next("/login"); - } else if (!userIsAuthenticated) { + } else if (!authRequired) { next(); } else { next(); diff -r b7ac2e4f9c5c -r 9908260d1e6a client/src/stores/user.js --- a/client/src/stores/user.js Thu Jul 05 16:52:39 2018 +0200 +++ b/client/src/stores/user.js Thu Jul 05 18:01:39 2018 +0200 @@ -50,7 +50,7 @@ set_expires: (state, expires) => { state.expires = expires; }, - authenticate: state => { + set_authenticate: state => { state.authenticated = true; } }, diff -r b7ac2e4f9c5c -r 9908260d1e6a client/tests/unit/HelloWorld.spec.js --- a/client/tests/unit/HelloWorld.spec.js Thu Jul 05 16:52:39 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,5 +0,0 @@ -describe("HelloWorld.vue", () => { - it("renders props.msg when passed", () => { - expect(true); - }); -}); diff -r b7ac2e4f9c5c -r 9908260d1e6a client/tests/unit/session.spec.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/tests/unit/session.spec.js Thu Jul 05 18:01:39 2018 +0200 @@ -0,0 +1,15 @@ +import { sessionStillActive } from "../../src/lib/session"; + +test("No session counts as expired session", () => { + expect(sessionStillActive(null)).toBe(false); +}); + +test("Session expired", () => { + const expired = Date.now() - 6000; + expect(sessionStillActive(expired)).toBe(false); +}); + +test("Session alive", () => { + const active = Date.now() + 60000; + expect(sessionStillActive(active)).toBe(true); +});