Files
speckle-server/packages/frontend-2/middleware/guest.ts
T
Kristaps Fabians Geikins 582793924d fix(fe2): CEF 65 support
2023-05-09 10:37:16 +03:00

37 lines
1.1 KiB
TypeScript

import { ApolloClient } from '@apollo/client/core'
import { activeUserQuery } from '~~/lib/auth/composables/activeUser'
import { convertThrowIntoFetchResult } from '~~/lib/common/helpers/graphql'
import { homeRoute } from '~~/lib/common/helpers/route'
const exclusionList = ['authorize-app']
/**
* Apply this to a page to prevent authenticated access
*/
export default defineNuxtRouteMiddleware(async (to) => {
const nuxt = useNuxtApp()
const { $apollo } = nuxt
const client = ($apollo as { default: ApolloClient<unknown> }).default
// Skipping this on some auth sub-pages
const routeName = to.name
if (routeName && exclusionList.includes(routeName.toString())) return undefined
const { data } = await client
.query({
query: activeUserQuery
})
.catch(convertThrowIntoFetchResult)
// Redirect home, if not logged in
if (data?.activeUser?.id) {
if (process.server && nuxt.ssrContext?.event.node.req.method === 'OPTIONS') {
// quickfix hack to prevent redirect in OPTIONS
return
}
return navigateTo(homeRoute)
}
return undefined
})