opnform/client/middleware/01.check-auth.global.js

17 lines
647 B
JavaScript

import {fetchAllWorkspaces} from "~/stores/workspaces.js";
import {opnFetch} from "~/composables/useOpnApi.js";
export default defineNuxtRouteMiddleware(async(to, from) => {
const authStore = useAuthStore()
authStore.initStore(useCookie('token').value, useCookie('admin_token').value)
if (authStore.token && !authStore.user) {
const workspaceStore = useWorkspacesStore()
// Load user data and workspaces
const [userDataResponse, workspacesResponse] = await Promise.all([useOpnApi('user'), fetchAllWorkspaces()]);
authStore.setUser(userDataResponse.data.value)
workspaceStore.save(workspacesResponse.data.value)
}
})