From f9db04ef9678eaaf7e5d3fffe6ba62ad16176c3d Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 2 Dec 2021 13:17:42 +0100 Subject: [PATCH] fix: Catch Expired Session when Coming from Admin Interface --- frontend/src/routes/guards.js | 19 +++++++---- frontend/src/routes/guards.test.js | 55 +++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 7 deletions(-) diff --git a/frontend/src/routes/guards.js b/frontend/src/routes/guards.js index 0788cb9d8..1838fa0fc 100644 --- a/frontend/src/routes/guards.js +++ b/frontend/src/routes/guards.js @@ -15,12 +15,19 @@ const addNavigationGuards = (router, store, apollo) => { router.beforeEach(async (to, from, next) => { if (to.path === '/authenticate' && to.query.token) { store.commit('token', to.query.token) - const result = await apollo.query({ - query: verifyLogin, - fetchPolicy: 'network-only', - }) - store.dispatch('login', result.data.verifyLogin) - next({ path: '/overview' }) + await apollo + .query({ + query: verifyLogin, + fetchPolicy: 'network-only', + }) + .then((result) => { + store.dispatch('login', result.data.verifyLogin) + next({ path: '/overview' }) + }) + .catch(() => { + store.dispatch('logout') + next() + }) } else { next() } diff --git a/frontend/src/routes/guards.test.js b/frontend/src/routes/guards.test.js index f271c5427..fa9f60f56 100644 --- a/frontend/src/routes/guards.test.js +++ b/frontend/src/routes/guards.test.js @@ -2,15 +2,28 @@ import addNavigationGuards from './guards' import router from './router' const storeCommitMock = jest.fn() +const storeDispatchMock = jest.fn() +const apolloQueryMock = jest.fn().mockResolvedValue({ + data: { + verifyLogin: { + firstName: 'Peter', + }, + }, +}) const store = { commit: storeCommitMock, state: { token: null, }, + dispatch: storeDispatchMock, } -addNavigationGuards(router, store) +const apollo = { + query: apolloQueryMock, +} + +addNavigationGuards(router, store, apollo) describe('navigation guards', () => { beforeEach(() => { @@ -29,6 +42,46 @@ describe('navigation guards', () => { }) }) + describe('authenticate', () => { + const navGuard = router.beforeHooks[1] + const next = jest.fn() + + describe('with valid token', () => { + beforeEach(() => { + navGuard({ path: '/authenticate', query: { token: 'valid-token' } }, {}, next) + }) + + it('commts the token to the store', () => { + expect(storeCommitMock).toBeCalledWith('token', 'valid-token') + }) + + it('calls verifyLogin', () => { + expect(apolloQueryMock).toBeCalled() + }) + + it('commits login to the store', () => { + expect(storeDispatchMock).toBeCalledWith('login', { firstName: 'Peter' }) + }) + }) + + describe('with valid token and server error', () => { + beforeEach(() => { + apolloQueryMock.mockRejectedValue({ + message: 'Ouch!', + }) + navGuard({ path: '/authenticate', query: { token: 'valid-token' } }, {}, next) + }) + + it('dispatches logout to store', () => { + expect(storeDispatchMock).toBeCalledWith('logout') + }) + + it('calls next', () => { + expect(next).toBeCalledWith() + }) + }) + }) + describe('authorization', () => { const navGuard = router.beforeHooks[2] const next = jest.fn()