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-09 02:40:51 +00:00
|
|
|
import { LocalStorage } from 'quasar';
|
2020-11-14 13:41:46 +00:00
|
|
|
import { Notify } 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-11-06 09:52:51 +00:00
|
|
|
backendOffline: boolean;
|
2020-10-16 06:45:40 +00:00
|
|
|
}
|
2020-10-18 23:45:06 +00:00
|
|
|
|
2020-11-09 02:40:51 +00:00
|
|
|
/**
|
|
|
|
* Load current session from LocalStorage
|
|
|
|
* Used when we were already authenticated using this browser
|
|
|
|
*/
|
|
|
|
function loadCurrentSession() {
|
2020-11-06 00:17:04 +00:00
|
|
|
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-09 02:40:51 +00:00
|
|
|
currentSession: loadCurrentSession() || undefined,
|
2020-11-06 09:52:51 +00:00
|
|
|
loading: false,
|
|
|
|
backendOffline: false
|
2020-10-16 06:45:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
2020-11-06 09:52:51 +00:00
|
|
|
},
|
|
|
|
setOffline(state, value: boolean) {
|
|
|
|
state.backendOffline = value;
|
2020-10-16 06:45:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const actions: ActionTree<SessionInterface, StateInterface> = {
|
2020-11-09 02:40:51 +00:00
|
|
|
/** Used to authenticate the user
|
|
|
|
* Setting current Session, User and Permissions.
|
|
|
|
* @param param0 Context
|
|
|
|
* @param data Credentitals
|
|
|
|
*/
|
2020-11-04 22:53:10 +00:00
|
|
|
login({ commit }, data: LoginData) {
|
2020-11-09 02:40:51 +00:00
|
|
|
return axios
|
2020-11-04 22:53:10 +00:00
|
|
|
.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-11-14 13:41:46 +00:00
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
Notify.create({
|
|
|
|
type: 'negative',
|
|
|
|
message: 'Benutzername oder Passwort sind falsch.',
|
|
|
|
timeout: 10000,
|
|
|
|
progress: true,
|
|
|
|
actions: [{ icon: 'mdi-close', color: 'white' }]
|
|
|
|
});
|
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
|
2020-11-09 02:40:51 +00:00
|
|
|
* Alias of deleteSession with current session as target
|
2020-11-04 22:53:10 +00:00
|
|
|
*/
|
|
|
|
logout({ dispatch, rootState }) {
|
2020-11-09 02:40:51 +00:00
|
|
|
if (rootState.session.currentSession) {
|
|
|
|
dispatch('deleteSession', rootState.session.currentSession.token).catch(
|
|
|
|
error => {
|
|
|
|
console.log(error);
|
|
|
|
void dispatch('clearCurrent', false);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
void dispatch('clearCurrent', false);
|
|
|
|
}
|
2020-11-04 22:53:10 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Delete a given session
|
|
|
|
*/
|
2020-11-09 02:40:51 +00:00
|
|
|
deleteSession({ commit, dispatch, rootState }, token: string) {
|
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-09 02:40:51 +00:00
|
|
|
void dispatch('clearCurrent', false);
|
2020-10-30 12:27:33 +00:00
|
|
|
} else {
|
2020-11-09 02:40:51 +00:00
|
|
|
dispatch('getSessions').catch(error => {
|
|
|
|
throw error;
|
|
|
|
});
|
2020-11-04 22:53:10 +00:00
|
|
|
}
|
|
|
|
})
|
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-09 02:40:51 +00:00
|
|
|
/**
|
|
|
|
* Clear current session and logged in user
|
|
|
|
*/
|
|
|
|
clearCurrent({ commit }, redirect = true) {
|
|
|
|
void Router.push({
|
|
|
|
name: 'login',
|
|
|
|
query: redirect ? { redirect: Router.currentRoute.fullPath } : {},
|
|
|
|
params: { logout: 'true' }
|
|
|
|
}).then(() => {
|
|
|
|
commit('clearCurrentSession');
|
|
|
|
commit('user/clearCurrentUser', null, { root: true });
|
2020-11-09 02:59:17 +00:00
|
|
|
// ensure also volatile store gets cleared by refreshing the site
|
|
|
|
Router.go(0);
|
2020-11-09 02:40:51 +00:00
|
|
|
});
|
2020-11-06 00:17:04 +00:00
|
|
|
},
|
2020-11-04 22:53:10 +00:00
|
|
|
/**
|
|
|
|
* Get all sessions from current User
|
|
|
|
*/
|
2020-11-09 02:40:51 +00:00
|
|
|
getSessions({ commit, state }) {
|
2020-11-04 22:53:10 +00:00
|
|
|
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) {
|
2020-11-09 02:40:51 +00:00
|
|
|
commit('setCurrentSession', currentSession);
|
2020-10-30 12:27:33 +00:00
|
|
|
}
|
2020-10-16 11:07:31 +00:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2020-11-09 02:40:51 +00:00
|
|
|
throw 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;
|