From 7c33a71c4d694e1eba874ed30d0dc8eddcd99930 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Thu, 29 Oct 2020 00:19:39 +0100 Subject: [PATCH] Fixed some TS issues related to user and session store --- src/boot/login.ts | 50 ++++++----------------- src/plugins/user/routes/index.ts | 4 +- src/plugins/user/store/user.ts | 70 ++++++++++++-------------------- src/router/routes.ts | 2 +- 4 files changed, 42 insertions(+), 84 deletions(-) diff --git a/src/boot/login.ts b/src/boot/login.ts index d369769..9b341c4 100644 --- a/src/boot/login.ts +++ b/src/boot/login.ts @@ -2,7 +2,6 @@ import { boot } from 'quasar/wrappers'; import { RouteRecord } from 'vue-router'; import { Store } from 'vuex'; import { StateInterface } from 'src/store'; -import { UserStateInterface } from 'src/plugins/user/store/user'; export default boot>(({ router, store }) => { router.beforeEach((to, from, next) => { @@ -11,29 +10,23 @@ export default boot>(({ router, store }) => { .then(() => { const user: FG.User = store.getters['user/user']; const session: FG.Session = store.getters['user/session']; + + if (session.expires >= new Date()) { + store.dispatch('user/doLogout').catch(error => {console.warn(error)}); + return next({ name: 'login', query: { redirect: to.fullPath } }); + } + let permissions: string[] = []; user.roles.forEach(role => { permissions = permissions.concat(role.permissions); }); - console.log('route to', to); - console.log('route from', from); - if (to.name != 'login') { - console.log(new Date(session.expires), new Date()); - console.log(new Date(session.expires) >= new Date()); - if ( - new Date(session.expires) >= new Date() && - to.matched.every((record: RouteRecord) => { - const checkedPerimssions: - | boolean - | undefined = record.meta?.permissions?.every( - (permission: FG.Permission) => { - return permissions.includes(permission); - } - ); - return checkedPerimssions === undefined - ? true - : checkedPerimssions; + if (to.name != 'login') { + if(to.matched.every((record: RouteRecord) => { + if (!('meta' in record) || + !('permission' in record.meta)) + return true; + return permissions.includes((<{permission: string}>record.meta).permission); }) ) { next(); @@ -43,25 +36,6 @@ export default boot>(({ router, store }) => { } else { next(); } - - // if ( - // to.matched.some((record: RouteRecord) => { - // // permissions is set AND has NO matching permission - // return ( - // 'permissions' in record.meta && - // record.meta.permissions.filter((value: string) => - // permissions.includes(value) - // ).length == 0 - // ); - // }) - // ) { - // next({ - // path: '/login', - // query: { redirect: to.fullPath } - // }); - // } else { - // next(); - // } }) .catch(error => { console.exception(error); diff --git a/src/plugins/user/routes/index.ts b/src/plugins/user/routes/index.ts index 2848a49..12957ac 100644 --- a/src/plugins/user/routes/index.ts +++ b/src/plugins/user/routes/index.ts @@ -9,12 +9,12 @@ const routes: RouteConfig[] = [ path: 'user', name: 'user', component: () => import('../pages/User.vue'), - meta: { permissions: ['user'] }, + meta: { permission: 'user' }, children: [ { path: 'settings', name: 'user-settings', - meta: { permissions: ['user'] }, + meta: { permission: 'user' }, component: () => import('../pages/Settings.vue') } ] diff --git a/src/plugins/user/store/user.ts b/src/plugins/user/store/user.ts index 92ccf98..86b4e02 100644 --- a/src/plugins/user/store/user.ts +++ b/src/plugins/user/store/user.ts @@ -15,22 +15,26 @@ export interface LoginResponse { session: FG.Session; } -const state: UserStateInterface = { - user: { +const empty_session: FG.Session = { + browser: '', + expires: new Date(), + lifetime: -1, + platform: '', + token: '' +}; + +const empty_user: FG.User = { display_name: '', firstname: '', lastname: '', mail: '', roles: [], userid: '' - }, - session: { - browser: '', - expires: new Date(), - lifetime: -1, - platform: '', - token: '' - }, +}; + +const state: UserStateInterface = { + user: empty_user, + session: empty_session, loginLoading: false }; @@ -61,8 +65,8 @@ const actions: ActionTree = { void axios .post('/auth', data) .then((response: AxiosResponse) => { + response.data.session.expires = new Date(response.data.session.expires); commit('setUser', response.data.user); - console.log('saved permisisons'); commit('setSession', response.data.session); commit('showState'); LocalStorage.set('user', response.data.user); @@ -78,34 +82,26 @@ const actions: ActionTree = { Loading.hide(); }); }, - logout({ commit }, token) { + + doLogout({commit}, token: string) { Loading.show({ message: 'Du wirst ausgeloggt' }); void axios .delete(`/auth/${token}`) .then(() => { - commit('setUser', { - display_name: '', - firstname: '', - lastname: '', - mail: '', - roles: [], - userid: '' - }); - commit('setSession', { - browser: '', - expires: '', - lifetime: '', - platform: '', - token: '' - }); + commit('setUser', empty_user); + commit('setSession', empty_session); }) .finally(() => { LocalStorage.remove('user'); LocalStorage.remove('session'); - void Router.push({ name: 'login' }); Loading.hide(); }); }, + + logout({ dispatch}, token: string) { + dispatch('doLogout', token).finally(() => {void Router.push({ name: 'login' });}); + }, + updateUser({ commit, getters }, data) { commit('setLoginLoading', true); axios @@ -117,6 +113,7 @@ const actions: ActionTree = { commit('setLoginLoading', false); }); }, + loadFromLocalStorage({ commit }) { console.log('load from store'); let data = LocalStorage.getItem('user'); @@ -124,27 +121,14 @@ const actions: ActionTree = { 'setUser', data ? data - : { - display_name: '', - firstname: '', - lastname: '', - mail: '', - roles: [], - userid: '' - } + : empty_user ); data = LocalStorage.getItem('session'); commit( 'setSession', data ? data - : { - browser: '', - expires: new Date(), - lifetime: -1, - platform: '', - token: '' - } + : empty_session ); commit('showState'); } diff --git a/src/router/routes.ts b/src/router/routes.ts index 9ff5656..cda0866 100644 --- a/src/router/routes.ts +++ b/src/router/routes.ts @@ -26,7 +26,7 @@ const routes: RouteConfig[] = [ { name: 'about', path: 'about', - meta: { 'permissions': ['user'] }, + meta: { 'permission': 'user' }, component: () => import('pages/about/About.vue') } ]