2020-10-02 07:13:14 +00:00
|
|
|
import { store } from 'quasar/wrappers';
|
2021-01-30 03:19:30 +00:00
|
|
|
import { createStore } from 'vuex';
|
2021-01-31 19:40:18 +00:00
|
|
|
|
|
|
|
export interface StateInterface {
|
2021-01-31 21:21:49 +00:00
|
|
|
[key: string]: unknown;
|
2021-01-31 19:40:18 +00:00
|
|
|
}
|
2020-10-02 07:13:14 +00:00
|
|
|
|
2021-01-30 03:19:30 +00:00
|
|
|
export default store(function (/* { ssrContext } */) {
|
|
|
|
const Store = createStore({
|
2020-10-16 07:38:14 +00:00
|
|
|
modules: {},
|
2020-10-02 07:13:14 +00:00
|
|
|
|
|
|
|
// enable strict mode (adds overhead!)
|
2021-01-30 03:19:30 +00:00
|
|
|
// for dev mode and --debug builds only
|
|
|
|
strict: !!process.env.DEBUGGING,
|
2020-10-02 07:13:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return Store;
|
|
|
|
});
|
2021-02-02 21:32:23 +00:00
|
|
|
|
|
|
|
import { defineStore } from 'pinia';
|
|
|
|
import { api } from 'src/boot/axios';
|
|
|
|
import { AxiosResponse } from 'axios';
|
2021-02-03 02:46:06 +00:00
|
|
|
import { LocalStorage, SessionStorage } from 'quasar';
|
2021-02-02 21:32:23 +00:00
|
|
|
import { useUserStore, useSessionStore } from 'src/plugins/user/store';
|
|
|
|
|
|
|
|
function loadCurrentSession() {
|
|
|
|
const session = LocalStorage.getItem<FG.Session>('session');
|
|
|
|
if (session) session.expires = new Date(session.expires);
|
|
|
|
return session || undefined;
|
|
|
|
}
|
|
|
|
|
2021-02-03 02:46:06 +00:00
|
|
|
function loadUser() {
|
|
|
|
const user = SessionStorage.getItem<FG.User>('user');
|
|
|
|
if (user && user.birthday) user.birthday = new Date(user.birthday);
|
|
|
|
return user || undefined;
|
|
|
|
}
|
|
|
|
|
2021-02-02 21:32:23 +00:00
|
|
|
export const useMainStore = defineStore({
|
|
|
|
id: 'main',
|
|
|
|
|
|
|
|
state: () => ({
|
|
|
|
session: loadCurrentSession(),
|
2021-02-03 02:46:06 +00:00
|
|
|
user: loadUser(),
|
2021-03-29 05:35:23 +00:00
|
|
|
notifications: [] as Array<FG.Notification>,
|
2021-02-02 21:32:23 +00:00
|
|
|
}),
|
|
|
|
|
|
|
|
getters: {
|
|
|
|
loggedIn() {
|
|
|
|
return this.session !== undefined;
|
|
|
|
},
|
|
|
|
currentUser() {
|
|
|
|
if (this.user === undefined) throw 'Not logged in, this should not be called';
|
|
|
|
return this.user;
|
|
|
|
},
|
|
|
|
permissions() {
|
|
|
|
return this.user?.permissions || [];
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
/** Ininitalize store from saved session
|
|
|
|
* Updates session and loads current user
|
|
|
|
*/
|
|
|
|
async init() {
|
|
|
|
if (this.session) {
|
|
|
|
const sessionStore = useSessionStore();
|
|
|
|
const session = await sessionStore.getSession(this.session.token);
|
|
|
|
if (session) {
|
|
|
|
this.session = this.session;
|
|
|
|
const userStore = useUserStore();
|
|
|
|
const user = await userStore.getUser(this.session.userid);
|
2021-02-03 02:46:06 +00:00
|
|
|
if (user) {
|
|
|
|
this.user = user;
|
|
|
|
SessionStorage.set('user', user);
|
|
|
|
}
|
2021-02-02 21:32:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async login(userid: string, password: string) {
|
|
|
|
try {
|
|
|
|
const { data } = await api.post<FG.Session>('/auth', { userid, password });
|
|
|
|
this.session = data;
|
|
|
|
this.session.expires = new Date(this.session.expires);
|
|
|
|
LocalStorage.set('session', this.session);
|
|
|
|
return true;
|
|
|
|
} catch ({ response }) {
|
|
|
|
return (<AxiosResponse | undefined>response)?.status || false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-02-04 23:22:11 +00:00
|
|
|
async logout() {
|
|
|
|
if (!this.session || !this.session.token) return false;
|
2021-02-02 21:32:23 +00:00
|
|
|
|
2021-02-04 23:22:11 +00:00
|
|
|
LocalStorage.clear();
|
|
|
|
|
|
|
|
try {
|
2021-03-20 16:04:09 +00:00
|
|
|
const token = this.session.token;
|
2021-02-03 02:46:06 +00:00
|
|
|
this.$patch({
|
|
|
|
session: undefined,
|
|
|
|
user: undefined,
|
|
|
|
});
|
2021-03-20 16:04:09 +00:00
|
|
|
await api.delete(`/auth/${token}`);
|
|
|
|
} catch (error) {
|
|
|
|
return false;
|
|
|
|
} finally {
|
2021-02-03 02:46:06 +00:00
|
|
|
SessionStorage.clear();
|
2021-02-04 23:22:11 +00:00
|
|
|
}
|
2021-02-02 21:32:23 +00:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
async requestReset(userid: string) {
|
|
|
|
return await api
|
|
|
|
.post('/auth/reset', { userid })
|
|
|
|
.then(() => true)
|
|
|
|
.catch(() => false);
|
|
|
|
},
|
|
|
|
|
|
|
|
async resetPassword(token: string, password: string) {
|
|
|
|
return await api
|
|
|
|
.post('/auth/reset', { token, password })
|
|
|
|
.then(() => true)
|
|
|
|
.catch(({ response }) =>
|
|
|
|
response && 'status' in response ? (<AxiosResponse>response).status : false
|
|
|
|
);
|
|
|
|
},
|
2021-03-29 05:35:23 +00:00
|
|
|
|
|
|
|
async loadNotifications() {
|
|
|
|
const params =
|
|
|
|
this.notifications.length > 0
|
|
|
|
? { from: this.notifications[this.notifications.length - 1].time }
|
|
|
|
: {};
|
|
|
|
const { data } = await api.get<FG.Notification[]>('/notifications', { params: params });
|
|
|
|
data.forEach((n) => {
|
|
|
|
n.time = new Date(n.time);
|
|
|
|
if (window.Notification.permission === 'granted')
|
|
|
|
new window.Notification(n.text, {
|
|
|
|
timestamp: n.time.getTime(),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this.notifications.push(...data);
|
|
|
|
},
|
|
|
|
|
|
|
|
async removeNotification(id: number) {
|
|
|
|
const idx = this.notifications.findIndex((n) => n.id === id);
|
|
|
|
if (idx >= 0)
|
|
|
|
try {
|
|
|
|
this.notifications.splice(idx, 1);
|
|
|
|
await api.delete(`/notifications/${id}`);
|
|
|
|
} catch (error) {
|
|
|
|
if (this.notifications.length > idx)
|
|
|
|
this.notifications.splice(idx, this.notifications.length - idx - 1);
|
|
|
|
}
|
|
|
|
},
|
2021-02-02 21:32:23 +00:00
|
|
|
},
|
|
|
|
});
|