Files
speckle-server/packages/frontend-2/middleware/canViewProjectTokens.ts
T
Kristaps Fabians Geikins 843606775c feat(fe2): parallel middlewares (#5314)
* parallel middlewares foundation + hydration mismatch

* moved to fully parallel middlewares

* a bit less hacky

* some more cleanup

* improved nuxt 4 error formatting

* make parallel middlewares toggleable
2025-08-27 12:38:04 +03:00

42 lines
1.2 KiB
TypeScript

import { graphql } from '~/lib/common/generated/gql'
import { useApolloClientFromNuxt } from '~~/lib/common/composables/graphql'
import { convertThrowIntoFetchResult } from '~~/lib/common/helpers/graphql'
import { projectRoute } from '~~/lib/common/helpers/route'
const canViewProjectTokensQuery = graphql(`
query CanViewProjectTokens($projectId: String!) {
project(id: $projectId) {
id
permissions {
canReadEmbedTokens {
...FullPermissionCheckResult
}
}
}
}
`)
/**
* Apply this to a page to prevent unauthenticated access to tokens and ensure the user is the owner
*/
export default defineParallelizedNuxtRouteMiddleware(async (to) => {
const client = useApolloClientFromNuxt()
const projectId = to.params.id as string
const { data } = await client
.query({
query: canViewProjectTokensQuery,
variables: { projectId }
})
.catch(convertThrowIntoFetchResult)
if (!data?.project) {
return navigateTo(projectRoute(projectId))
}
const canReadTokens = data.project.permissions.canReadEmbedTokens.authorized
if (!canReadTokens) {
return navigateTo(projectRoute(projectId))
}
})