Lädt UserStore aus LocalStorage

Sollte die seite neu geladen werden, wird aus dem LocalStorage der UserStorage geladen. Somit bleibt man angemeldet.
Axios angepasst, sodass nur noch die url eingegeben werden muss.
This commit is contained in:
Tim Gröger 2020-10-16 22:37:37 +02:00
parent 704f6fd3fe
commit 0cdfe7f11c
5 changed files with 49 additions and 25 deletions

View File

@ -15,11 +15,12 @@ export default boot(({ Vue, store }) => {
axios.defaults.baseURL = config.baseURL; axios.defaults.baseURL = config.baseURL;
axios.interceptors.request.use(config => { axios.interceptors.request.use(config => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
const token: Token = (<UserStateInterface>store.state.user).token; const token: Token = store.getters['user/token'];
if (token) { if (token) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
config.headers['Authorization'] = 'Token ' + token.token; config.headers['Token'] = token.token;
//config.headers['Authorization'] = 'Token ' + token.token;
} }
return config; return config;
}); });

View File

@ -4,11 +4,15 @@ import { RouteRecord } from 'vue-router';
export default boot(({ router, store }) => { export default boot(({ router, store }) => {
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
store.dispatch('user/loadFromLocalStorage');
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/ban-ts-comment
// @ts-ignore // @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const user: UserStateInterface = (<UserStateInterface>store.state).user; //const user: UserStateInterface = (<UserStateInterface>store.state).user;
console.log('login_boot', user); //const user: UserStateInterface = store.getters['user/user'];
const permissions: [] = store.getters['user/permissions'];
console.log('login_boot', permissions);
if ( if (
to.matched.some((record: RouteRecord) => { to.matched.some((record: RouteRecord) => {
// permissions is set AND has NO matching permission // permissions is set AND has NO matching permission
@ -19,7 +23,7 @@ export default boot(({ router, store }) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access // eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
( (
record.meta.permissions.filter((value: string) => record.meta.permissions.filter((value: string) =>
user.permissions.includes(value) permissions.includes(value)
).length > 0 ).length > 0
) )
) )

View File

@ -1,6 +1,9 @@
<template> <template>
<div> <div>
<q-page padding class="fit row justify-center content-center items-center"> <q-page
padding
class="fit row justify-center content-center items-center q-gutter-sm"
>
<div class="text-h5 row"> <div class="text-h5 row">
Deine Sessions: Deine Sessions:
</div> </div>
@ -8,7 +11,6 @@
class="fit row justify-center content-center items-center q-gutter-sm" class="fit row justify-center content-center items-center q-gutter-sm"
> >
<circular-progress v-if="sessionsLoading" /> <circular-progress v-if="sessionsLoading" />
<div>tada:: {{ sessionsLoading }}</div>
<q-card <q-card
class="col-12" class="col-12"
height="" height=""

View File

@ -36,9 +36,7 @@ const actions: ActionTree<SessionInterface, StateInterface> = {
console.log(rootGetters); console.log(rootGetters);
commit('setLoading', true); commit('setLoading', true);
axios axios
.get('http://localhost:5000/auth', { .get('/auth')
headers: { Token: rootGetters['user/token'].token }
})
.then(response => { .then(response => {
console.log(response.data); console.log(response.data);
commit('setSessions', response.data); commit('setSessions', response.data);
@ -52,10 +50,9 @@ const actions: ActionTree<SessionInterface, StateInterface> = {
}, },
deleteSession({ commit, dispatch, rootGetters }, token) { deleteSession({ commit, dispatch, rootGetters }, token) {
commit('setLoading', true); commit('setLoading', true);
console.log('axios', axios);
axios axios
.delete(`http://localhost:5000/auth/${token}`, { .delete(`/auth/${token}`)
headers: { Token: rootGetters['user/token'].token }
})
.then(() => { .then(() => {
void dispatch('getSessions'); void dispatch('getSessions');
}) })

View File

@ -20,6 +20,7 @@ export interface User {
lastname: string; lastname: string;
mail: string; mail: string;
roles: string[]; roles: string[];
userid: string;
} }
export interface UserStateInterface extends LoginResponse { export interface UserStateInterface extends LoginResponse {
@ -41,9 +42,9 @@ const state: UserStateInterface = {
firstname: '', firstname: '',
lastname: '', lastname: '',
mail: '', mail: '',
roles: [] roles: [],
userid: ''
}, },
userid: '',
loginLoading: false loginLoading: false
}; };
@ -57,9 +58,6 @@ const mutations: MutationTree<UserStateInterface> = {
setUser(state, data: User) { setUser(state, data: User) {
state.user = data; state.user = data;
}, },
setUserId(state, data: string) {
state.userid = data;
},
setLoginLoading(state, data: boolean) { setLoginLoading(state, data: boolean) {
state.loginLoading = data; state.loginLoading = data;
}, },
@ -74,18 +72,16 @@ const actions: ActionTree<UserStateInterface, StateInterface> = {
commit('setLoginLoading', true); commit('setLoginLoading', true);
Loading.show({ message: 'Du wirst eingeloggt' }); Loading.show({ message: 'Du wirst eingeloggt' });
void axios void axios
.post('http://localhost:5000/auth', data) .post('/auth', data)
.then((response: AxiosResponse<LoginResponse>) => { .then((response: AxiosResponse<LoginResponse>) => {
commit('setPermissions', response.data.permissions); commit('setPermissions', response.data.permissions);
console.log('saved permisisons'); console.log('saved permisisons');
commit('setToken', response.data.token); commit('setToken', response.data.token);
commit('setUser', response.data.user); commit('setUser', response.data.user);
commit('setUserId', response.data.userid);
commit('showState'); commit('showState');
LocalStorage.set('permissions', response.data.permissions); LocalStorage.set('permissions', response.data.permissions);
LocalStorage.set('token', response.data.token); LocalStorage.set('token', response.data.token);
LocalStorage.set('user', response.data.user); LocalStorage.set('user', response.data.user);
LocalStorage.set('userid', response.data.userid);
void Router.push({ name: 'user' }); void Router.push({ name: 'user' });
}) })
@ -96,6 +92,33 @@ const actions: ActionTree<UserStateInterface, StateInterface> = {
commit('setLoginLoading', false); commit('setLoginLoading', false);
Loading.hide(); Loading.hide();
}); });
},
loadFromLocalStorage({ commit }) {
console.log('load from store');
let data = LocalStorage.getItem('permissions');
commit('setPermissions', data ? data : []);
data = LocalStorage.getItem('token');
commit(
'setToken',
data
? data
: { browser: '', expires: '', lifetime: -1, platform: '', token: '' }
);
data = LocalStorage.getItem('user');
commit(
'setUser',
data
? data
: {
display_name: '',
firstname: '',
lastname: '',
mail: '',
roles: [],
userid: ''
}
);
commit('showState');
} }
}; };
@ -109,9 +132,6 @@ const getters: GetterTree<UserStateInterface, StateInterface> = {
user({ user }) { user({ user }) {
return user; return user;
}, },
userid({ userid }) {
return userid;
},
loginLoading({ loginLoading }) { loginLoading({ loginLoading }) {
return loginLoading; return loginLoading;
} }