diff --git a/api/src/stores/main.ts b/api/src/stores/main.ts index 1635840..835b56a 100644 --- a/api/src/stores/main.ts +++ b/api/src/stores/main.ts @@ -5,17 +5,17 @@ import { api } from '../internal'; import { defineStore } from 'pinia'; import { PersistentStorage } from '../utils/persistent'; -function loadToken() { - return PersistentStorage.get('fg_token'); +function reviveSession() { + return PersistentStorage.get('fg_session').then((s) => fixSession(s || undefined)); } -function clearToken() { - void PersistentStorage.remove('fg_token'); +function clearPersistant() { + void PersistentStorage.remove('fg_session'); } -export function saveToken(token?: string) { - if (token === undefined) return clearToken(); - PersistentStorage.set('fg_token', token).catch(() => +export function saveSession(session?: FG.Session) { + if (session === undefined) return clearPersistant(); + PersistentStorage.set('fg_session', session).catch(() => console.error('Could not save token to storage') ); } @@ -52,9 +52,9 @@ export const useMainStore = defineStore({ const userStore = useUserStore(); try { - const token = await loadToken(); - if (token !== null) { - this.session = await sessionStore.getSession(token); + this.session = await reviveSession(); + if (this.session !== undefined) { + this.session = await sessionStore.getSession(this.session.token); if (this.session !== undefined) this.user = await userStore.getUser(this.session.userid); } } catch (error) { @@ -146,7 +146,7 @@ export const useMainStore = defineStore({ handleLoggedOut() { this.$reset(); - void clearToken(); + void clearPersistant(); }, }, }); diff --git a/package.json b/package.json index c7fc570..6c9568c 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "devDependencies": { "@capacitor/core": "^3.3.2", "@capacitor/storage": "^1.2.3", + "@flaschengeist/types": "^1.0.0-alpha.9", "@quasar/app": "^3.2.3", "@quasar/extras": "^1.12.1", "@types/node": "^14.17.34", diff --git a/src/boot/init.ts b/src/boot/init.ts index 361b2bb..e3ed67e 100644 --- a/src/boot/init.ts +++ b/src/boot/init.ts @@ -1,7 +1,13 @@ /** * This boot file initalizes the store from persistent storage and load all plugins */ -import { PersistentStorage, api, saveToken, useMainStore, isAxiosError } from '@flaschengeist/api'; +import { + PersistentStorage, + api, + isAxiosError, + saveSession, + useMainStore, +} from '@flaschengeist/api'; import { Notify, Platform } from 'quasar'; import { loadPlugins } from './plugins'; import { boot } from 'quasar/wrappers'; @@ -44,7 +50,7 @@ export default boot(async ({ app, router }) => { } finally { // Any changes on the session is written back to the persistent store store.$subscribe((mutation, state) => { - saveToken(state.session?.token); + saveSession(state.session); }); } diff --git a/src/pages/Login.vue b/src/pages/Login.vue index 2a80ab2..9517df2 100644 --- a/src/pages/Login.vue +++ b/src/pages/Login.vue @@ -115,7 +115,7 @@ export default defineComponent({ const status = await mainStore.login(userid.value, password.value); if (status === true) { - mainStore.user = (await userStore.getUser(userid.value, true)) || undefined; + mainStore.user = await userStore.getUser(userid.value, true); const x = router.currentRoute.value.query['redirect']; void router.push(typeof x === 'string' ? { path: x } : mainRoute); } else {