Files
speckle-server/packages/frontend-2/middleware/requireValidWorkspace.ts
T
andrewwallacespeckle e3f90377b5 feat(fe2): workspace project list (#2616)
* Create and Use ProjectList

* ProjectList and WorkspaceList

* Workspace Header

* Header Actions Menu

* Add projects to Workspace

* Add middleware

* Remove unused title

* Rename WorkspaceList

* useDebouncedSearch

* Merge ProjectList into Dashboard

* Make workspaceId reactive

* Remove unneeded useSubscription

* Merge Dashboard into index

* Add fragments

* Cache updates

* gql

* GQL

* Linting updates

* Updates from CR

* Changes from CR

* Changes from PR. Middleware added

* Updates from CR

* GQL

* Updates from CR

* Updates from CR

* Updates from CR

* Add id to WorkspaceHeader_Workspace

* GQL

* Fragment naming

* Use identifier

* Comment buttons not yet ready

* Fix problem with pagination
2024-08-14 15:21:38 +01:00

57 lines
1.4 KiB
TypeScript

import { useApolloClientFromNuxt } from '~~/lib/common/composables/graphql'
import {
convertThrowIntoFetchResult,
getFirstErrorMessage
} from '~~/lib/common/helpers/graphql'
import { workspaceAccessCheckQuery } from '~~/lib/workspaces/graphql/queries'
/**
* Used to validate that the workspace ID refers to a valid workspace and redirects to 404 if not
*/
export default defineNuxtRouteMiddleware(async (to) => {
const workspaceId = to.params.id as string
const client = useApolloClientFromNuxt()
const { data, errors } = await client
.query({
query: workspaceAccessCheckQuery,
variables: { id: workspaceId },
context: {
skipLoggingErrors: true
}
})
.catch(convertThrowIntoFetchResult)
if (data?.workspace?.id) return
const isForbidden = (errors || []).find((e) => e.extensions['code'] === 'FORBIDDEN')
const isNotFound = (errors || []).find(
(e) => e.extensions['code'] === 'WORKSPACE_NOT_FOUND_ERROR'
)
if (isForbidden) {
return abortNavigation(
createError({
statusCode: 403,
message: 'You do not have access to this workspace'
})
)
}
if (isNotFound) {
return abortNavigation(
createError({ statusCode: 404, message: 'Workspace not found' })
)
}
if (errors?.length) {
const errMsg = getFirstErrorMessage(errors)
return abortNavigation(
createError({
statusCode: 500,
message: errMsg
})
)
}
})