From 1b152b52f5a86d893d6832e8c82871579a3af14f Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Fri, 26 Nov 2021 20:31:49 +0100 Subject: [PATCH] [core] Save session token in PersistentStorage. --- api/src/stores/main.ts | 56 ++++++++++++++++++++---------------------- src/boot/store.ts | 7 +++++- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/api/src/stores/main.ts b/api/src/stores/main.ts index 839f2b2..876deac 100644 --- a/api/src/stores/main.ts +++ b/api/src/stores/main.ts @@ -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('session'); - if (session) session.expires = new Date(session.expires); - return session || undefined; +function loadToken() { + return PersistentStorage.get('fg_token'); } -function loadUser() { - const user = SessionStorage.getItem('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, shortcuts: [] as Array, }), @@ -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(); }, }, }); diff --git a/src/boot/store.ts b/src/boot/store.ts index fbc02a8..d54acfc 100644 --- a/src/boot/store.ts +++ b/src/boot/store.ts @@ -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); + }); + }); });