Files
speckle-server/packages/frontend-2/lib/auth/composables/activeUser.ts
T
andrewwallacespeckle 91cb011ded feat(fe2): New user onboarding flow (#3932)
* CodeInput. verify-email page

* middleware

* Loading toast

* Countdown only for registration

* Improve middleware

* Fix middleware breaking auth flow

* Remove old notifications

* Remove old onboarding. New segmentation

* Remove skip button

* Block verify email when verified

* useUserEmails composable. Cancel addition

* Move user emails queries

* Fix fragments etc

* redirect updates

* HeaderWithEmptyPage

* Check env before enforcing

* Join workspace

* Updates

* Fix console warnings on login

* Fix register console warnings

* Working cache updates

* Verify secondary email

* Force onboarding off

* EMAIL WIP

* useIsJustRegistered state

* Improve isRequired

* Uneeded change

* Improved slots

* Updates from CR

* CR comments

* Only show message if forced

* Update onboarding middleware

* Update loading bar

* ref > computed to fix onboarding

* Resend tooltip. Better errors

* Add other to form.

* Email changes

* Updates to emails

* Remove force email FF

* Remove FF's

* Hide header on embed

* Update graphql.ts

* Re-add FF

* Update graphql.ts

* GQL Fragments

* Fix build
2025-02-14 10:20:14 +00:00

97 lines
2.3 KiB
TypeScript

import {
Roles,
type MaybeNullOrUndefined,
resolveMixpanelUserId
} from '@speckle/shared'
import { useApolloClient, useQuery } from '@vue/apollo-composable'
import { graphql } from '~~/lib/common/generated/gql'
export const activeUserQuery = graphql(`
query ActiveUserMainMetadata {
activeUser {
id
email
emails {
id
verified
}
company
bio
name
role
avatar
isOnboardingFinished
createdAt
verified
notificationPreferences
versions(limit: 0) {
totalCount
}
}
}
`)
/**
* 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 resolveMixpanelUserId(user.email)
}
}
/**
* 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 userId = computed(() => activeUser.value?.id)
const isGuest = computed(() => activeUser.value?.role === Roles.Server.Guest)
const isAdmin = computed(() => activeUser.value?.role === Roles.Server.Admin)
const projectVersionCount = computed(() => activeUser.value?.versions.totalCount)
return {
activeUser,
userId,
isLoggedIn,
distinctId,
refetch,
onResult,
isGuest,
isAdmin,
projectVersionCount
}
}
/**
* 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)
}