[core] Save session token in PersistentStorage.

This commit is contained in:
Ferdinand Thiessen 2021-11-26 20:31:49 +01:00
parent 6769e18ffa
commit 1b152b52f5
2 changed files with 33 additions and 30 deletions

View File

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

View File

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