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> <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-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 class="bg-primary text-white">
<q-toolbar-title> <q-toolbar-title> Login </q-toolbar-title>
Login
</q-toolbar-title>
</q-toolbar> </q-toolbar>
<q-card-section> <q-card-section>
@ -91,7 +89,8 @@ export default defineComponent({
userid: userid.value, userid: userid.value,
password: password.value password: password.value
}) })
.then(() => { .then(async finished => {
await finished;
const x = root.$route.query['redirect']; const x = root.$route.query['redirect'];
void root.$router.push(typeof x === 'string' ? { path: x } : mainRoute); void root.$router.push(typeof x === 'string' ? { path: x } : mainRoute);
}) })

View File

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