[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 { defineStore } from 'pinia';
 | 
				
			||||||
import { PersistentStorage } from '../utils/persistent';
 | 
					import { PersistentStorage } from '../utils/persistent';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function loadToken() {
 | 
					function reviveSession() {
 | 
				
			||||||
  return PersistentStorage.get<string>('fg_token');
 | 
					  return PersistentStorage.get<FG.Session>('fg_session').then((s) => fixSession(s || undefined));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function clearToken() {
 | 
					function clearPersistant() {
 | 
				
			||||||
  void PersistentStorage.remove('fg_token');
 | 
					  void PersistentStorage.remove('fg_session');
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function saveToken(token?: string) {
 | 
					export function saveSession(session?: FG.Session) {
 | 
				
			||||||
  if (token === undefined) return clearToken();
 | 
					  if (session === undefined) return clearPersistant();
 | 
				
			||||||
  PersistentStorage.set('fg_token', token).catch(() =>
 | 
					  PersistentStorage.set('fg_session', session).catch(() =>
 | 
				
			||||||
    console.error('Could not save token to storage')
 | 
					    console.error('Could not save token to storage')
 | 
				
			||||||
  );
 | 
					  );
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -52,9 +52,9 @@ export const useMainStore = defineStore({
 | 
				
			||||||
      const userStore = useUserStore();
 | 
					      const userStore = useUserStore();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      try {
 | 
					      try {
 | 
				
			||||||
        const token = await loadToken();
 | 
					        this.session = await reviveSession();
 | 
				
			||||||
        if (token !== null) {
 | 
					        if (this.session !== undefined) {
 | 
				
			||||||
          this.session = await sessionStore.getSession(token);
 | 
					          this.session = await sessionStore.getSession(this.session.token);
 | 
				
			||||||
          if (this.session !== undefined) this.user = await userStore.getUser(this.session.userid);
 | 
					          if (this.session !== undefined) this.user = await userStore.getUser(this.session.userid);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      } catch (error) {
 | 
					      } catch (error) {
 | 
				
			||||||
| 
						 | 
					@ -146,7 +146,7 @@ export const useMainStore = defineStore({
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    handleLoggedOut() {
 | 
					    handleLoggedOut() {
 | 
				
			||||||
      this.$reset();
 | 
					      this.$reset();
 | 
				
			||||||
      void clearToken();
 | 
					      void clearPersistant();
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -24,6 +24,7 @@
 | 
				
			||||||
  "devDependencies": {
 | 
					  "devDependencies": {
 | 
				
			||||||
    "@capacitor/core": "^3.3.2",
 | 
					    "@capacitor/core": "^3.3.2",
 | 
				
			||||||
    "@capacitor/storage": "^1.2.3",
 | 
					    "@capacitor/storage": "^1.2.3",
 | 
				
			||||||
 | 
					    "@flaschengeist/types": "^1.0.0-alpha.9",
 | 
				
			||||||
    "@quasar/app": "^3.2.3",
 | 
					    "@quasar/app": "^3.2.3",
 | 
				
			||||||
    "@quasar/extras": "^1.12.1",
 | 
					    "@quasar/extras": "^1.12.1",
 | 
				
			||||||
    "@types/node": "^14.17.34",
 | 
					    "@types/node": "^14.17.34",
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,7 +1,13 @@
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * This boot file initalizes the store from persistent storage and load all plugins
 | 
					 * 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 { Notify, Platform } from 'quasar';
 | 
				
			||||||
import { loadPlugins } from './plugins';
 | 
					import { loadPlugins } from './plugins';
 | 
				
			||||||
import { boot } from 'quasar/wrappers';
 | 
					import { boot } from 'quasar/wrappers';
 | 
				
			||||||
| 
						 | 
					@ -44,7 +50,7 @@ export default boot(async ({ app, router }) => {
 | 
				
			||||||
  } finally {
 | 
					  } finally {
 | 
				
			||||||
    // Any changes on the session is written back to the persistent store
 | 
					    // Any changes on the session is written back to the persistent store
 | 
				
			||||||
    store.$subscribe((mutation, state) => {
 | 
					    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);
 | 
					      const status = await mainStore.login(userid.value, password.value);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if (status === true) {
 | 
					      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'];
 | 
					        const x = router.currentRoute.value.query['redirect'];
 | 
				
			||||||
        void router.push(typeof x === 'string' ? { path: x } : mainRoute);
 | 
					        void router.push(typeof x === 'string' ? { path: x } : mainRoute);
 | 
				
			||||||
      } else {
 | 
					      } else {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue