2020-10-16 06:45:40 +00:00
|
|
|
import { Module, MutationTree, ActionTree, GetterTree } from 'vuex';
|
2020-11-04 22:53:10 +00:00
|
|
|
import { LoginData, LoginResponse } from 'src/plugins/user/models';
|
2020-10-16 06:45:40 +00:00
|
|
|
import { StateInterface } from 'src/store';
|
2020-10-18 23:45:06 +00:00
|
|
|
import { axios } from 'src/boot/axios';
|
2020-11-06 00:17:04 +00:00
|
|
|
import { AxiosError, AxiosResponse } from 'axios';
|
2020-10-30 12:27:33 +00:00
|
|
|
import { Router } from 'src/router';
|
2020-11-04 22:53:10 +00:00
|
|
|
import { LocalStorage, Loading } from 'quasar';
|
2020-10-18 23:45:06 +00:00
|
|
|
|
2020-10-16 06:45:40 +00:00
|
|
|
export interface SessionInterface {
|
2020-11-04 22:53:10 +00:00
|
|
|
currentSession?: FG.Session;
|
2020-10-27 10:51:53 +00:00
|
|
|
sessions: FG.Session[];
|
2020-10-16 06:45:40 +00:00
|
|
|
loading: boolean;
|
|
|
|
}
|
2020-10-18 23:45:06 +00:00
|
|
|
|
2020-11-06 00:17:04 +00:00
|
|
|
function loadFromLocal() {
|
|
|
|
const session = LocalStorage.getItem<FG.Session>('currentSession');
|
|
|
|
if (session) session.expires = new Date(session.expires);
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
2020-10-16 06:45:40 +00:00
|
|
|
const state: SessionInterface = {
|
|
|
|
sessions: [],
|
2020-11-06 00:17:04 +00:00
|
|
|
currentSession: loadFromLocal() || undefined,
|
2020-10-16 06:45:40 +00:00
|
|
|
loading: false
|
|
|
|
};
|
|
|
|
|
|
|
|
const mutations: MutationTree<SessionInterface> = {
|
2020-11-04 22:53:10 +00:00
|
|
|
setCurrentSession(state, session: FG.Session) {
|
|
|
|
LocalStorage.set('currentSession', session);
|
|
|
|
state.currentSession = session;
|
|
|
|
},
|
|
|
|
clearCurrentSession(state) {
|
|
|
|
LocalStorage.remove('currentSession');
|
|
|
|
state.currentSession = undefined;
|
|
|
|
},
|
2020-10-27 10:51:53 +00:00
|
|
|
setSessions(state, sessions: FG.Session[]) {
|
2020-10-16 06:45:40 +00:00
|
|
|
state.sessions = sessions;
|
|
|
|
},
|
2020-10-27 10:51:53 +00:00
|
|
|
setLoading(state, value: boolean) {
|
2020-10-16 06:45:40 +00:00
|
|
|
state.loading = value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const actions: ActionTree<SessionInterface, StateInterface> = {
|
2020-11-04 22:53:10 +00:00
|
|
|
login({ commit }, data: LoginData) {
|
|
|
|
Loading.show({
|
|
|
|
message: 'Du wirst angemeldet'
|
|
|
|
});
|
|
|
|
void 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 });
|
2020-11-06 00:17:04 +00:00
|
|
|
commit('user/setCurrentPermissions', response.data.permissions, {
|
|
|
|
root: true
|
|
|
|
});
|
2020-10-16 06:45:40 +00:00
|
|
|
})
|
2020-11-06 00:17:04 +00:00
|
|
|
.catch(error => console.warn(error))
|
2020-10-16 06:45:40 +00:00
|
|
|
.finally(() => {
|
2020-11-06 00:17:04 +00:00
|
|
|
void Router.push({ name: 'user-main' });
|
2020-11-04 22:53:10 +00:00
|
|
|
Loading.hide();
|
2020-10-16 06:45:40 +00:00
|
|
|
});
|
2020-10-16 11:07:31 +00:00
|
|
|
},
|
2020-11-04 22:53:10 +00:00
|
|
|
/**
|
|
|
|
* Logout from current session
|
|
|
|
*/
|
|
|
|
logout({ dispatch, rootState }) {
|
|
|
|
Loading.show({ message: 'Session wird abgemeldet' });
|
|
|
|
dispatch('deleteSession', rootState.session.currentSession?.token).finally(
|
|
|
|
() => {
|
|
|
|
Loading.hide();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Delete a given session
|
|
|
|
*/
|
2020-11-06 00:17:04 +00:00
|
|
|
deleteSession({ commit, dispatch, rootState }, token: string | null) {
|
2020-11-04 22:53:10 +00:00
|
|
|
if (token === null) return;
|
|
|
|
|
2020-10-16 11:07:31 +00:00
|
|
|
commit('setLoading', true);
|
|
|
|
axios
|
2020-10-16 20:37:37 +00:00
|
|
|
.delete(`/auth/${token}`)
|
2020-10-16 11:07:31 +00:00
|
|
|
.then(() => {
|
2020-11-04 22:53:10 +00:00
|
|
|
if (token === rootState.session.currentSession?.token) {
|
2020-11-06 00:17:04 +00:00
|
|
|
void dispatch('clearup');
|
2020-10-30 12:27:33 +00:00
|
|
|
} else {
|
2020-11-04 22:53:10 +00:00
|
|
|
commit('getSessions');
|
|
|
|
}
|
|
|
|
})
|
2020-11-06 00:17:04 +00:00
|
|
|
.catch((error: AxiosError) => {
|
|
|
|
if (!error.response || error.response.status != 401) throw error;
|
|
|
|
})
|
2020-11-04 22:53:10 +00:00
|
|
|
.finally(() => {
|
|
|
|
commit('setLoading', false);
|
|
|
|
});
|
|
|
|
},
|
2020-11-06 00:17:04 +00:00
|
|
|
clearup({ commit }) {
|
|
|
|
commit('clearCurrentSession');
|
|
|
|
commit('user/clearCurrentUser', null, { root: true });
|
|
|
|
void Router.push({ name: 'login' });
|
|
|
|
},
|
2020-11-04 22:53:10 +00:00
|
|
|
/**
|
|
|
|
* Get all sessions from current User
|
|
|
|
*/
|
|
|
|
getSessions({ commit, state, dispatch }) {
|
|
|
|
commit('setLoading', true);
|
|
|
|
axios
|
|
|
|
.get('/auth')
|
|
|
|
.then((response: AxiosResponse<FG.Session[]>) => {
|
|
|
|
response.data.forEach(session => {
|
|
|
|
session.expires = new Date(session.expires);
|
|
|
|
});
|
|
|
|
commit('setSessions', response.data);
|
|
|
|
const currentSession = response.data.find((session: FG.Session) => {
|
|
|
|
return session.token === state.currentSession?.token;
|
|
|
|
});
|
|
|
|
if (currentSession) {
|
|
|
|
void dispatch('setCurrentSession', currentSession);
|
2020-10-30 12:27:33 +00:00
|
|
|
}
|
2020-10-16 11:07:31 +00:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2020-10-18 23:45:06 +00:00
|
|
|
console.exception(error);
|
2020-10-16 11:07:31 +00:00
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
commit('setLoading', false);
|
|
|
|
});
|
2020-10-16 06:45:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getters: GetterTree<SessionInterface, StateInterface> = {
|
2020-11-04 22:53:10 +00:00
|
|
|
currentSession(state) {
|
|
|
|
return state.currentSession;
|
|
|
|
},
|
2020-10-27 10:51:53 +00:00
|
|
|
sessions(state) {
|
2020-10-16 06:45:40 +00:00
|
|
|
return state.sessions;
|
2020-10-16 11:54:01 +00:00
|
|
|
},
|
2020-10-27 10:51:53 +00:00
|
|
|
loading(state) {
|
2020-10-16 11:54:01 +00:00
|
|
|
return state.loading;
|
2020-10-16 06:45:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const sessions: Module<SessionInterface, StateInterface> = {
|
|
|
|
namespaced: true,
|
|
|
|
state,
|
|
|
|
mutations,
|
|
|
|
actions,
|
|
|
|
getters
|
|
|
|
};
|
|
|
|
|
|
|
|
export default sessions;
|