release v2.0.0 #4

Merged
crimsen merged 481 commits from develop into master 2024-01-18 15:15:08 +00:00
2 changed files with 33 additions and 30 deletions
Showing only changes of commit 1b152b52f5 - Show all commits

View File

@ -1,28 +1,31 @@
import { LocalStorage, SessionStorage } from 'quasar';
import { FG_Plugin } from '@flaschengeist/types'; import { FG_Plugin } from '@flaschengeist/types';
import { fixSession, useSessionStore, useUserStore } from '.'; import { fixSession, useSessionStore, useUserStore } from '.';
import { AxiosResponse } from 'axios'; import { AxiosResponse } from 'axios';
import { api } from '../internal'; import { api } from '../internal';
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { PersistentStorage } from '../utils/persistent';
function loadCurrentSession() { function loadToken() {
const session = LocalStorage.getItem<FG.Session>('session'); return PersistentStorage.get<string>('fg_token');
if (session) session.expires = new Date(session.expires);
return session || undefined;
} }
function loadUser() { function clearToken() {
const user = SessionStorage.getItem<FG.User>('user'); PersistentStorage.remove('fg_token');
if (user && user.birthday) user.birthday = new Date(user.birthday); }
return user || undefined;
export function saveToken(token?: string) {
if (token === undefined) return clearToken();
PersistentStorage.set('fg_token', token).catch(() =>
console.error('Could not save token to storage')
);
} }
export const useMainStore = defineStore({ export const useMainStore = defineStore({
id: 'main', id: 'main',
state: () => ({ state: () => ({
session: loadCurrentSession(), session: undefined as FG.Session | undefined,
user: loadUser(), user: undefined as FG.User | undefined,
notifications: [] as Array<FG_Plugin.Notification>, notifications: [] as Array<FG_Plugin.Notification>,
shortcuts: [] as Array<FG_Plugin.MenuLink>, shortcuts: [] as Array<FG_Plugin.MenuLink>,
}), }),
@ -45,18 +48,17 @@ export const useMainStore = defineStore({
* Updates session and loads current user * Updates session and loads current user
*/ */
async init() { async init() {
if (this.session) { const sessionStore = useSessionStore();
const sessionStore = useSessionStore(); const userStore = useUserStore();
const session = await sessionStore.getSession(this.session.token);
if (session) { try {
this.session = session; const token = await loadToken();
const userStore = useUserStore(); if (token !== null) {
const user = await userStore.getUser(this.session.userid); this.session = await sessionStore.getSession(token);
if (user) { if (this.session !== undefined) this.user = await userStore.getUser(this.session.userid);
this.user = user;
SessionStorage.set('user', user);
}
} }
} catch (error) {
console.warn('Could not load token from storage', error);
} }
}, },
@ -141,14 +143,10 @@ export const useMainStore = defineStore({
async setShortcuts() { async setShortcuts() {
await api.put(`users/${this.currentUser.userid}/shortcuts`, this.shortcuts); await api.put(`users/${this.currentUser.userid}/shortcuts`, this.shortcuts);
}, },
handleLoggedOut() {
LocalStorage.clear();
this.$patch({ handleLoggedOut() {
session: undefined, this.$reset();
user: undefined, void clearToken();
});
SessionStorage.clear();
}, },
}, },
}); });

View File

@ -1,9 +1,14 @@
import { useMainStore, pinia } from '@flaschengeist/api'; import { useMainStore, pinia } from '@flaschengeist/api';
import { saveToken } from 'app/api';
import { boot } from 'quasar/wrappers'; import { boot } from 'quasar/wrappers';
export default boot(({ app }) => { export default boot(({ app }) => {
app.use(pinia); app.use(pinia);
const store = useMainStore(); const store = useMainStore();
void store.init(); store.init().finally(() => {
store.$subscribe((mutation, state) => {
saveToken(state.session?.token);
});
});
}); });