diff --git a/src/App.vue b/src/App.vue index 0afe36b..c7c1cb3 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,7 +1,7 @@ @@ -11,6 +11,7 @@ import { storeToRefs } from 'pinia'; import Menu from './components/Menu.vue'; import { usePageStore } from './stores/page'; import { detect } from 'detect-browser'; +import { useUserStore } from './stores/user'; const browser = detect(); @@ -20,4 +21,5 @@ if (browser) { ).dataset.browser = `${browser.name} ${browser.version} ${browser.os}`; } const { page } = storeToRefs(usePageStore()); +const { isLogged } = storeToRefs(useUserStore()); diff --git a/src/router/router.js b/src/router/router.js index 22dae89..984f883 100644 --- a/src/router/router.js +++ b/src/router/router.js @@ -17,12 +17,12 @@ router.beforeEach(async (to, from, next) => { if (to.path === '/login') next(); - if (!userStore.user) next('/login'); - const api = useApiStore(pinia); try { const res = await api.fetchData(to.path); + if (!res.user) next('/login'); + pageStore.page = res.page; userStore.user = res.user; next(); diff --git a/src/stores/user.js b/src/stores/user.js index 7fe5956..17aa2fe 100644 --- a/src/stores/user.js +++ b/src/stores/user.js @@ -1,10 +1,14 @@ -import { defineStore, storeToRefs } from "pinia"; -import { ref, computed } from "vue"; -import { useProjectsStore } from "./projects"; +import { defineStore, storeToRefs } from 'pinia'; +import { ref, computed } from 'vue'; +import { useProjectsStore } from './projects'; -export const useUserStore = defineStore("user", () => { +export const useUserStore = defineStore('user', () => { const user = ref(null); + const isLogged = computed(() => { + return user.value?.hasOwnProperty('role'); + }); + const { projects } = storeToRefs(useProjectsStore()); const notifications = computed(() => { @@ -22,7 +26,7 @@ export const useUserStore = defineStore("user", () => { }); function readNotification(notificationId, projectId) { - console.log("Read notification", notificationId, projectId); + console.log('Read notification', notificationId, projectId); projects.value = projects.value.map((project) => ({ ...project, notifications: @@ -57,6 +61,7 @@ export const useUserStore = defineStore("user", () => { return { user, + isLogged, notifications, readNotification, readAllNotifications,