Files
speckle-server/packages/frontend-2/lib/auth/composables/activeUser.ts
T
Kristaps Fabians Geikins dd7409dcb2 feat(fe2): implementing various RUM tools for trialing (#2066)
* basic raygun setup

* testing seq logging

* minor fixes

* more accurate user identification

* logrocket adjustments

* speedcurve seems to work?

* added debugbear

* minor cleanup

* chore(helm chart): adds new web app analytics ids/keys to fe2 env vars
- assumes none are secrets

* Quote all secrets to prevent interpretation as digits

---------

Co-authored-by: Iain Sproat <68657+iainsproat@users.noreply.github.com>
2024-02-22 10:51:13 +02:00

74 lines
2.0 KiB
TypeScript

import { Roles, type MaybeNullOrUndefined } from '@speckle/shared'
import { useApolloClient, useQuery } from '@vue/apollo-composable'
import { graphql } from '~~/lib/common/generated/gql'
import md5 from '~~/lib/common/helpers/md5'
export const activeUserQuery = graphql(`
query ActiveUserMainMetadata {
activeUser {
id
email
company
bio
name
role
avatar
isOnboardingFinished
createdAt
verified
notificationPreferences
}
}
`)
/**
* Lightweight composable to read user id from cache imperatively (useful for logging)
*/
export function useReadUserId() {
const client = useApolloClient().client
return () => {
return client.readQuery({ query: activeUserQuery })?.activeUser?.id
}
}
export function useResolveUserDistinctId() {
return (user: MaybeNullOrUndefined<{ email?: MaybeNullOrUndefined<string> }>) => {
if (!user) return user // null or undefined
if (!user.email) return null
return '@' + md5(user.email.toLowerCase()).toUpperCase()
}
}
/**
* Get active user.
* undefined - not yet resolved
* null - resolved that user is a guest
*/
export function useActiveUser() {
const { result, refetch, onResult } = useQuery(activeUserQuery)
const getDistinctId = useResolveUserDistinctId()
const activeUser = computed(() =>
result.value ? result.value.activeUser : undefined
)
const isLoggedIn = computed(() => !!activeUser.value?.id)
const distinctId = computed(() => {
const user = activeUser.value
return getDistinctId(user)
})
const isGuest = computed(() => activeUser.value?.role === Roles.Server.Guest)
const isAdmin = computed(() => activeUser.value?.role === Roles.Server.Admin)
return { activeUser, isLoggedIn, distinctId, refetch, onResult, isGuest, isAdmin }
}
/**
* Prevents setup function from resolving until active user is resolved
*/
export function useWaitForActiveUser() {
const client = useApolloClient().client
return async () => await client.query({ query: activeUserQuery }).catch(() => void 0)
}