Login compat. with backend

This commit is contained in:
Ferdinand Thiessen 2021-02-10 18:09:37 +01:00
parent 567e994b71
commit cd74612e6d
2 changed files with 21 additions and 25 deletions

View File

@ -1,10 +1,8 @@
<template>
<q-page padding class="fit row justify-center items-center content-center">
<q-page padding class="fit row justify-center items-center content-center">
<q-card class="col-xs-11 col-sm-8 col-md-6 col-lg-4 justify-center items-center content-center">
<q-toolbar class="bg-primary text-white">
<q-toolbar-title>
Login
</q-toolbar-title>
<q-toolbar-title> Login </q-toolbar-title>
</q-toolbar>
<q-card-section>
@ -91,7 +89,8 @@ export default defineComponent({
userid: userid.value,
password: password.value
})
.then(() => {
.then(async finished => {
await finished;
const x = root.$route.query['redirect'];
void root.$router.push(typeof x === 'string' ? { path: x } : mainRoute);
})

View File

@ -25,7 +25,7 @@ function loadCurrentSession() {
const state: SessionInterface = {
sessions: [],
currentSession: loadCurrentSession() || undefined,
loading: false
loading: false,
};
const mutations: MutationTree<SessionInterface> = {
@ -44,11 +44,11 @@ const mutations: MutationTree<SessionInterface> = {
state.loading = value;
},
updateSession(state, session: FG.Session) {
const index = state.sessions.findIndex(x => x.token == session.token);
const index = state.sessions.findIndex((x) => x.token == session.token);
if (index > -1) {
state.sessions[index] = session;
}
}
},
};
const actions: ActionTree<SessionInterface, StateInterface> = {
@ -57,16 +57,13 @@ const actions: ActionTree<SessionInterface, StateInterface> = {
* @param param0 Context
* @param data Credentitals
*/
login({ commit }, data: LoginData) {
login({ commit, dispatch }, data: LoginData) {
return axios
.post('/auth', data)
.then((response: AxiosResponse<LoginResponse>) => {
response.data.session.expires = new Date(response.data.session.expires);
commit('setCurrentSession', response.data.session);
commit('user/setCurrentUser', response.data.user, { root: true });
commit('user/setCurrentPermissions', response.data.permissions, {
root: true
});
.then(async (response: AxiosResponse<FG.Session>) => {
response.data.expires = new Date(response.data.expires);
commit('setCurrentSession', response.data);
await dispatch('user/getCurrentUser', undefined, { root: true });
})
.catch((error: AxiosError) => {
return Promise.reject(error.response);
@ -78,7 +75,7 @@ const actions: ActionTree<SessionInterface, StateInterface> = {
*/
logout({ dispatch, rootState }) {
if (rootState.session.currentSession) {
dispatch('deleteSession', rootState.session.currentSession.token).catch(error => {
dispatch('deleteSession', rootState.session.currentSession.token).catch((error) => {
console.log(error);
void dispatch('clearCurrent', false);
});
@ -97,7 +94,7 @@ const actions: ActionTree<SessionInterface, StateInterface> = {
if (token === rootState.session.currentSession?.token) {
void dispatch('clearCurrent', false);
} else {
dispatch('getSessions').catch(error => {
dispatch('getSessions').catch((error) => {
throw error;
});
}
@ -116,7 +113,7 @@ const actions: ActionTree<SessionInterface, StateInterface> = {
void Router.push({
name: 'login',
query: redirect ? { redirect: Router.currentRoute.fullPath } : {},
params: { logout: 'true' }
params: { logout: 'true' },
}).then(() => {
commit('clearCurrentSession');
commit('user/clearCurrentUser', null, { root: true });
@ -132,7 +129,7 @@ const actions: ActionTree<SessionInterface, StateInterface> = {
axios
.get('/auth')
.then((response: AxiosResponse<FG.Session[]>) => {
response.data.forEach(session => {
response.data.forEach((session) => {
session.expires = new Date(session.expires);
});
commit('setSessions', response.data);
@ -143,7 +140,7 @@ const actions: ActionTree<SessionInterface, StateInterface> = {
commit('setCurrentSession', currentSession);
}
})
.catch(error => {
.catch((error) => {
throw error;
})
.finally(() => {
@ -160,7 +157,7 @@ const actions: ActionTree<SessionInterface, StateInterface> = {
commit('setCurrentSession', response.data);
}
})
.catch(err => console.log(err))
.catch((err) => console.log(err))
.finally(() => {
commit('setLoading', false);
});
@ -173,7 +170,7 @@ const actions: ActionTree<SessionInterface, StateInterface> = {
return axios.post('/auth/reset', data).catch((error: AxiosError) => {
return Promise.reject(error.response);
});
}
},
};
const getters: GetterTree<SessionInterface, StateInterface> = {
@ -185,7 +182,7 @@ const getters: GetterTree<SessionInterface, StateInterface> = {
},
loading(state) {
return state.loading;
}
},
};
const sessions: Module<SessionInterface, StateInterface> = {
@ -193,7 +190,7 @@ const sessions: Module<SessionInterface, StateInterface> = {
state,
mutations,
actions,
getters
getters,
};
export default sessions;