[core] Save session token in PersistentStorage.
This commit is contained in:
parent
6769e18ffa
commit
1b152b52f5
|
@ -1,28 +1,31 @@
|
||||||
import { LocalStorage, SessionStorage } from 'quasar';
|
|
||||||
import { FG_Plugin } from '@flaschengeist/types';
|
import { FG_Plugin } from '@flaschengeist/types';
|
||||||
import { fixSession, useSessionStore, useUserStore } from '.';
|
import { fixSession, useSessionStore, useUserStore } from '.';
|
||||||
import { AxiosResponse } from 'axios';
|
import { AxiosResponse } from 'axios';
|
||||||
import { api } from '../internal';
|
import { api } from '../internal';
|
||||||
import { defineStore } from 'pinia';
|
import { defineStore } from 'pinia';
|
||||||
|
import { PersistentStorage } from '../utils/persistent';
|
||||||
|
|
||||||
function loadCurrentSession() {
|
function loadToken() {
|
||||||
const session = LocalStorage.getItem<FG.Session>('session');
|
return PersistentStorage.get<string>('fg_token');
|
||||||
if (session) session.expires = new Date(session.expires);
|
|
||||||
return session || undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadUser() {
|
function clearToken() {
|
||||||
const user = SessionStorage.getItem<FG.User>('user');
|
PersistentStorage.remove('fg_token');
|
||||||
if (user && user.birthday) user.birthday = new Date(user.birthday);
|
}
|
||||||
return user || undefined;
|
|
||||||
|
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({
|
export const useMainStore = defineStore({
|
||||||
id: 'main',
|
id: 'main',
|
||||||
|
|
||||||
state: () => ({
|
state: () => ({
|
||||||
session: loadCurrentSession(),
|
session: undefined as FG.Session | undefined,
|
||||||
user: loadUser(),
|
user: undefined as FG.User | undefined,
|
||||||
notifications: [] as Array<FG_Plugin.Notification>,
|
notifications: [] as Array<FG_Plugin.Notification>,
|
||||||
shortcuts: [] as Array<FG_Plugin.MenuLink>,
|
shortcuts: [] as Array<FG_Plugin.MenuLink>,
|
||||||
}),
|
}),
|
||||||
|
@ -45,18 +48,17 @@ export const useMainStore = defineStore({
|
||||||
* Updates session and loads current user
|
* Updates session and loads current user
|
||||||
*/
|
*/
|
||||||
async init() {
|
async init() {
|
||||||
if (this.session) {
|
|
||||||
const sessionStore = useSessionStore();
|
const sessionStore = useSessionStore();
|
||||||
const session = await sessionStore.getSession(this.session.token);
|
|
||||||
if (session) {
|
|
||||||
this.session = session;
|
|
||||||
const userStore = useUserStore();
|
const userStore = useUserStore();
|
||||||
const user = await userStore.getUser(this.session.userid);
|
|
||||||
if (user) {
|
try {
|
||||||
this.user = user;
|
const token = await loadToken();
|
||||||
SessionStorage.set('user', user);
|
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() {
|
async setShortcuts() {
|
||||||
await api.put(`users/${this.currentUser.userid}/shortcuts`, this.shortcuts);
|
await api.put(`users/${this.currentUser.userid}/shortcuts`, this.shortcuts);
|
||||||
},
|
},
|
||||||
handleLoggedOut() {
|
|
||||||
LocalStorage.clear();
|
|
||||||
|
|
||||||
this.$patch({
|
handleLoggedOut() {
|
||||||
session: undefined,
|
this.$reset();
|
||||||
user: undefined,
|
void clearToken();
|
||||||
});
|
|
||||||
SessionStorage.clear();
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,9 +1,14 @@
|
||||||
import { useMainStore, pinia } from '@flaschengeist/api';
|
import { useMainStore, pinia } from '@flaschengeist/api';
|
||||||
|
import { saveToken } from 'app/api';
|
||||||
import { boot } from 'quasar/wrappers';
|
import { boot } from 'quasar/wrappers';
|
||||||
|
|
||||||
export default boot(({ app }) => {
|
export default boot(({ app }) => {
|
||||||
app.use(pinia);
|
app.use(pinia);
|
||||||
|
|
||||||
const store = useMainStore();
|
const store = useMainStore();
|
||||||
void store.init();
|
store.init().finally(() => {
|
||||||
|
store.$subscribe((mutation, state) => {
|
||||||
|
saveToken(state.session?.token);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue