Files
memberApp/src/vueLib/login/useLogin.ts
Adrian Zürcher aec741f094
All checks were successful
Build Quasar SPA and Go Backend for memberApp / build-spa (push) Successful in 2m34s
Build Quasar SPA and Go Backend for memberApp / build-backend (amd64, .exe, windows) (push) Successful in 5m39s
Build Quasar SPA and Go Backend for memberApp / build-backend (amd64, , linux) (push) Successful in 5m46s
Build Quasar SPA and Go Backend for memberApp / build-backend (arm, 6, , linux) (push) Successful in 5m32s
Build Quasar SPA and Go Backend for memberApp / build-backend (arm64, , linux) (push) Successful in 5m35s
add new event and attendance table with automatic now timestamp
2025-11-04 10:59:56 +01:00

93 lines
2.9 KiB
TypeScript

import { appApi } from 'src/boot/axios';
import { useUserStore } from './userStore';
import { useNotify } from '../general/useNotify';
import type { Settings } from '../models/settings';
import { Logo } from '../models/logo';
const refreshTime = 10000;
let intervalId: ReturnType<typeof setInterval> | null = null;
export function useLogin() {
const userStore = useUserStore();
const { NotifyResponse } = useNotify();
async function login(user: string, password: string) {
try {
await appApi.post('/login', { user, password }).then((resp) => {
const sets = resp.data.settings as Settings;
Logo.value = sets.icon;
document.documentElement.style.setProperty('--q-primary', sets.primaryColor);
document.documentElement.style.setProperty('--q-primary-text', sets.primaryColorText);
document.documentElement.style.setProperty('--q-secondary', sets.secondaryColor);
document.documentElement.style.setProperty('--q-secondary-text', sets.secondaryColorText);
localStorage.setItem('icon', sets.icon);
localStorage.setItem('databaseName', sets.databaseName);
localStorage.setItem('primaryColor', sets.primaryColor);
localStorage.setItem('primaryColorText', sets.primaryColorText);
localStorage.setItem('secondaryColor', sets.secondaryColor);
localStorage.setItem('secondaryColorText', sets.secondaryColorText);
});
const resp = await appApi.get('/login/me');
await userStore
.setUser({ id: resp.data.id, username: resp.data.user, role: resp.data.role })
.catch((err) => NotifyResponse(err, 'error'));
startRefreshInterval();
return true;
} catch (err) {
console.error('Login error:', err);
throw err;
}
}
async function logout() {
await appApi.get('/logout').catch((err) => {
NotifyResponse(err, 'error');
});
userStore.clearUser();
sessionStorage.clear();
stopRefreshInterval();
}
async function refresh() {
await appApi
.post('login/refresh', {}, { withCredentials: true })
.then(() => {
appApi
.get('/login/me')
.then((resp) => {
userStore
.setUser({ id: resp.data.id, username: resp.data.user, role: resp.data.role })
.catch((err) => NotifyResponse(err, 'error'));
if (!intervalId) {
startRefreshInterval();
}
return true;
})
.catch(() => {});
})
.catch(() => {
userStore.clearUser();
});
stopRefreshInterval();
return false;
}
function startRefreshInterval() {
intervalId = setInterval(() => {
refresh().catch((err) => NotifyResponse(err, 'error'));
}, refreshTime);
}
function stopRefreshInterval() {
if (intervalId) {
clearInterval(intervalId);
intervalId = null;
}
}
return { login, logout, refresh };
}