diff --git a/api/src/stores/main.ts b/api/src/stores/main.ts index 3a38682..839f2b2 100644 --- a/api/src/stores/main.ts +++ b/api/src/stores/main.ts @@ -1,6 +1,6 @@ import { LocalStorage, SessionStorage } from 'quasar'; import { FG_Plugin } from '@flaschengeist/types'; -import { useSessionStore, useUserStore } from '.'; +import { fixSession, useSessionStore, useUserStore } from '.'; import { AxiosResponse } from 'axios'; import { api } from '../internal'; import { defineStore } from 'pinia'; @@ -63,9 +63,7 @@ export const useMainStore = defineStore({ async login(userid: string, password: string) { try { const { data } = await api.post('/auth', { userid, password }); - this.session = data; - this.session.expires = new Date(this.session.expires); - LocalStorage.set('session', this.session); + this.session = fixSession(data); return true; } catch ({ response }) { return (response)?.status || false; diff --git a/api/src/stores/session.ts b/api/src/stores/session.ts index d3301d5..c722cb2 100644 --- a/api/src/stores/session.ts +++ b/api/src/stores/session.ts @@ -3,6 +3,10 @@ import { defineStore } from 'pinia'; import { api } from '../internal'; import { isAxiosError, useMainStore } from '.'; +export function fixSession(s?: FG.Session) { + return !s ? s : Object.assign(s, { expires: new Date(s.expires) }); +} + export const useSessionStore = defineStore({ id: 'sessions', @@ -15,15 +19,13 @@ export const useSessionStore = defineStore({ return await api .get(`/auth/${token}`) .then(({ data }: AxiosResponse) => data) - .catch(() => null); + .catch(() => undefined); }, async getSessions() { try { const { data } = await api.get('/auth'); - data.forEach((session) => { - session.expires = new Date(session.expires); - }); + data.forEach(fixSession); const mainStore = useMainStore(); const currentSession = data.find((session) => { @@ -55,7 +57,7 @@ export const useSessionStore = defineStore({ async updateSession(lifetime: number, token: string) { try { const { data } = await api.put(`auth/${token}`, { value: lifetime }); - data.expires = new Date(data.expires); + fixSession(data); const mainStore = useMainStore(); if (mainStore.session?.token == data.token) mainStore.session = data; diff --git a/api/src/stores/user.ts b/api/src/stores/user.ts index a5987b9..6981c1d 100644 --- a/api/src/stores/user.ts +++ b/api/src/stores/user.ts @@ -2,6 +2,10 @@ import { defineStore } from 'pinia'; import { api } from '../internal'; import { isAxiosError, useMainStore } from '.'; +export function fixUser(u?: FG.User) { + return !u ? u : Object.assign(u, { birthday: u.birthday ? new Date(u.birthday) : undefined }); +} + export const useUserStore = defineStore({ id: 'users', @@ -24,7 +28,7 @@ export const useUserStore = defineStore({ if (force || this._dirty_users || idx === -1) { try { const { data } = await api.get(`/users/${userid}`); - if (data.birthday) data.birthday = new Date(data.birthday); + fixUser(data); if (idx === -1) this.users.push(data); else this.users[idx] = data; return data; @@ -40,9 +44,7 @@ export const useUserStore = defineStore({ async getUsers(force = false) { if (force || this._dirty_users) { const { data } = await api.get('/users'); - data.forEach((user) => { - if (user.birthday) user.birthday = new Date(user.birthday); - }); + data.forEach(fixUser); this.users = data; this._dirty_users = false; } @@ -74,7 +76,7 @@ export const useUserStore = defineStore({ }, async deleteAvatar(user: FG.User) { - await api.delete(`/users/${user.userid}/avatar`) + await api.delete(`/users/${user.userid}/avatar`); }, async getPermissions(force = false) {