Files
speckle-server/packages/frontend-2/middleware/requireValidWorkspace.ts
T
andrewwallacespeckle 677b8202fa feat(fe2): workspace slug / short id support (#3088)
* Add edit slug dialog

* Design changes to edit button

* Slug url implementation

* Disable slug edit if non admin

* Preloading queries

* Use preloaded workspace id

* Changes pre PR

* Removed unneeded import

* feat(workspaces): migrate slug to match the workspaceId

* WIP Comments

* Add watch. Use Fragment

* Tidy create

* Tidy ups

* Tidy up Edit Dialog

* Remove comment

* Update text - changing slug does not break embeds

* Updates from CR

* Reorder ProjectList

* Feedback from Michal

---------

Co-authored-by: Gergő Jedlicska <gergo@jedlicska.com>
2024-10-01 16:23:54 +01:00

57 lines
1.5 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 workspaceSlug = to.params.slug as string
const client = useApolloClientFromNuxt()
const { data, errors } = await client
.query({
query: workspaceAccessCheckQuery,
variables: { slug: workspaceSlug },
context: {
skipLoggingErrors: true
}
})
.catch(convertThrowIntoFetchResult)
if (data?.workspaceBySlug.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
})
)
}
})