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

17 lines
647 B
JavaScript
Raw Permalink Normal View History

2023-12-19 17:57:31 +00:00
import {fetchAllWorkspaces} from "~/stores/workspaces.js";
import {opnFetch} from "~/composables/useOpnApi.js";
2023-12-16 18:21:03 +00:00
export default defineNuxtRouteMiddleware(async(to, from) => {
const authStore = useAuthStore()
authStore.initStore(useCookie('token').value, useCookie('admin_token').value)
if (authStore.token && !authStore.user) {
2023-12-19 17:57:31 +00:00
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)
2023-12-16 18:21:03 +00:00
}
})