fix: Catch Expired Session when Coming from Admin Interface

This commit is contained in:
Moriz Wahl 2021-12-02 13:17:42 +01:00
parent 1605cea902
commit f9db04ef96
2 changed files with 67 additions and 7 deletions

View File

@ -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()
}

View File

@ -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()