Fixed some TS issues related to user and session store

This commit is contained in:
Ferdinand Thiessen 2020-10-29 00:19:39 +01:00
parent 3f756437ee
commit 7c33a71c4d
4 changed files with 42 additions and 84 deletions

View File

@ -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<Store<StateInterface>>(({ router, store }) => {
router.beforeEach((to, from, next) => {
@ -11,29 +10,23 @@ export default boot<Store<StateInterface>>(({ router, store }) => {
.then(() => {
const user: FG.User = <FG.User>store.getters['user/user'];
const session: FG.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<Store<StateInterface>>(({ 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);

View File

@ -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')
}
]

View File

@ -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<UserStateInterface, StateInterface> = {
void axios
.post('/auth', data)
.then((response: AxiosResponse<LoginResponse>) => {
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<UserStateInterface, StateInterface> = {
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<UserStateInterface, StateInterface> = {
commit('setLoginLoading', false);
});
},
loadFromLocalStorage({ commit }) {
console.log('load from store');
let data = LocalStorage.getItem('user');
@ -124,27 +121,14 @@ const actions: ActionTree<UserStateInterface, StateInterface> = {
'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');
}

View File

@ -26,7 +26,7 @@ const routes: RouteConfig[] = [
{
name: 'about',
path: 'about',
meta: { 'permissions': ['user'] },
meta: { 'permission': 'user' },
component: () => import('pages/about/About.vue')
}
]