Files
memberApp/src/vueLib/login/userStore.ts
Adrian Zürcher 690b7f4034
All checks were successful
Build Quasar SPA and Go Backend for memberApp / build-spa (push) Successful in 2m12s
Build Quasar SPA and Go Backend for memberApp / build-backend (amd64, , linux) (push) Successful in 5m8s
Build Quasar SPA and Go Backend for memberApp / build-backend (amd64, .exe, windows) (push) Successful in 5m8s
Build Quasar SPA and Go Backend for memberApp / build-backend (arm, 6, , linux) (push) Successful in 4m57s
Build Quasar SPA and Go Backend for memberApp / build-backend (arm64, , linux) (push) Successful in 5m7s
add new user and role table in app (in progress)
2025-10-14 16:41:20 +02:00

69 lines
1.5 KiB
TypeScript

import { defineStore } from 'pinia';
import { useGlobalRouter } from 'src/utils/globalRouter';
import { useGlobalQ } from 'src/utils/globalQ';
interface User {
id: number;
username: string;
role: string;
}
interface UserState {
user: User | null;
}
export const useUserStore = defineStore('user', {
state: (): UserState => ({
user: null,
}),
getters: {
isAuthenticated: (state): boolean => !!state.user,
},
actions: {
setUser(user: User) {
this.user = user;
},
getUser() {
return this.user;
},
clearUser() {
const $q = useGlobalQ();
if (!this.user) return;
if ($q) {
$q?.notify({
message: "user '" + this.user?.username + "' logged out",
color: 'orange',
position: 'bottom-right',
icon: 'warning',
timeout: 5000,
});
} else {
console.error("user '" + this.user?.username + "' logged out");
}
this.user = null;
const router = useGlobalRouter();
router?.push('/').catch((err) => {
if ($q) {
$q?.notify({
message: err,
color: 'orange',
position: 'bottom-right',
icon: 'warning',
timeout: 5000,
});
} else {
console.error("user '" + this.user?.username + "' logged out");
}
});
},
isAuthorizedAs(roles: string[]) {
return this.user !== null && roles.includes(this.user.role);
},
},
});