58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
import { useApolloClientFromNuxt } from '~~/lib/common/composables/graphql'
|
|
import {
|
|
convertThrowIntoFetchResult,
|
|
getFirstErrorMessage
|
|
} from '~~/lib/common/helpers/graphql'
|
|
import { projectAccessCheckQuery } from '~~/lib/projects/graphql/queries'
|
|
|
|
/**
|
|
* Used in project page to validate that project ID refers to a valid project and redirects to 404 if not
|
|
*/
|
|
export default defineNuxtRouteMiddleware(async (to) => {
|
|
const projectId = to.params.id as string
|
|
|
|
const client = useApolloClientFromNuxt()
|
|
|
|
const { data, errors } = await client
|
|
.query({
|
|
query: projectAccessCheckQuery,
|
|
variables: { id: projectId },
|
|
context: {
|
|
skipLoggingErrors: true
|
|
}
|
|
})
|
|
.catch(convertThrowIntoFetchResult)
|
|
|
|
// If project succesfully resolved, move on
|
|
if (data?.project?.id) return
|
|
|
|
const isForbidden = (errors || []).find((e) => e.extensions['code'] === 'FORBIDDEN')
|
|
const isNotFound = (errors || []).find(
|
|
(e) => e.extensions['code'] === 'STREAM_NOT_FOUND'
|
|
)
|
|
if (isForbidden) {
|
|
return abortNavigation(
|
|
createError({
|
|
statusCode: 403,
|
|
message: 'You do not have access to this project'
|
|
})
|
|
)
|
|
}
|
|
|
|
if (isNotFound) {
|
|
return abortNavigation(
|
|
createError({ statusCode: 404, message: 'Project not found' })
|
|
)
|
|
}
|
|
|
|
if (errors?.length) {
|
|
const errMsg = getFirstErrorMessage(errors)
|
|
return abortNavigation(
|
|
createError({
|
|
statusCode: 500,
|
|
message: errMsg
|
|
})
|
|
)
|
|
}
|
|
})
|