[api] Save whole session in PersistantStore.
This commit is contained in:
		
							parent
							
								
									6732921ff7
								
							
						
					
					
						commit
						d9267bcc0a
					
				| 
						 | 
				
			
			@ -5,17 +5,17 @@ import { api } from '../internal';
 | 
			
		|||
import { defineStore } from 'pinia';
 | 
			
		||||
import { PersistentStorage } from '../utils/persistent';
 | 
			
		||||
 | 
			
		||||
function loadToken() {
 | 
			
		||||
  return PersistentStorage.get<string>('fg_token');
 | 
			
		||||
function reviveSession() {
 | 
			
		||||
  return PersistentStorage.get<FG.Session>('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();
 | 
			
		||||
    },
 | 
			
		||||
  },
 | 
			
		||||
});
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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",
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue