LCOV - code coverage report
Current view: top level - src/composables/auth - useCurrentUser.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 61.9 % 63 39
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { whenever } from '@vueuse/core'
       2              : import { computed, watch } from 'vue'
       3              : 
       4              : import { provideCurrentUser } from '@/platform/auth/currentUserAccess'
       5              : import { desktopHostUser } from '@/platform/auth/desktopHost/desktopHostSession'
       6              : import { useApiKeyAuthStore } from '@/stores/apiKeyAuthStore'
       7              : import { useCommandStore } from '@/platform/keybindings/commandStore'
       8              : import { useAuthStore } from '@/stores/authStore'
       9              : import type { AuthUserInfo } from '@comfyorg/api-client/authTypes'
      10              : 
      11         2356 : export const useCurrentUser = () => {
      12        36142 :   const authStore = useAuthStore()
      13        36142 :   const commandStore = useCommandStore()
      14        36142 :   const apiKeyStore = useApiKeyAuthStore()
      15              : 
      16        36142 :   const sessionUser = computed(() => authStore.sessionUser)
      17        36142 :   const firebaseForSession = computed(() => {
      18           20 :     const user = authStore.currentUser
      19           20 :     return !sessionUser.value || user?.uid === sessionUser.value.id
      20              :       ? user
      21              :       : null
      22              :   })
      23              :   // A signed-in identity takes precedence on every auth rail (see
      24              :   // authStore.getUserAuthHeader), so a stored key behind one is not an
      25              :   // API-key session.
      26        36142 :   const isApiKeyLogin = computed(
      27              :     () =>
      28              :       apiKeyStore.isAuthenticated &&
      29              :       authStore.currentUser === null &&
      30              :       !sessionUser.value &&
      31              :       !desktopHostUser.value
      32              :   )
      33        36142 :   const isLoggedIn = computed(
      34              :     () => isApiKeyLogin.value || authStore.isAuthenticated
      35              :   )
      36        36142 :   const isAuthInitialized = computed(() => authStore.isInitialized)
      37              : 
      38        36142 :   const resolvedUserInfo = computed<AuthUserInfo | null>(() => {
      39        12075 :     if (isApiKeyLogin.value && apiKeyStore.currentUser) {
      40            0 :       return { id: apiKeyStore.currentUser.id }
      41              :     }
      42              : 
      43        12075 :     if (authStore.userId) return { id: authStore.userId }
      44              : 
      45        11919 :     return null
      46              :   })
      47              : 
      48        36142 :   const onUserResolved = (callback: (user: AuthUserInfo) => void) =>
      49              :     whenever(resolvedUserInfo, callback, { immediate: true })
      50              : 
      51        36142 :   const onTokenRefreshed = (callback: () => void) =>
      52              :     whenever(() => authStore.tokenRefreshTrigger, callback)
      53              : 
      54        36142 :   const onUserLogout = (callback: () => void) =>
      55              :     watch(resolvedUserInfo, (user, prevUser) => {
      56            2 :       if (prevUser && !user) callback()
      57              :     })
      58              : 
      59        36142 :   const userDisplayName = computed(() => {
      60            8 :     if (desktopHostUser.value) return desktopHostUser.value.name
      61            3 :     if (isApiKeyLogin.value) {
      62            0 :       return apiKeyStore.currentUser?.name
      63              :     }
      64            8 :     return sessionUser.value?.name ?? firebaseForSession.value?.displayName
      65              :   })
      66              : 
      67        36142 :   const userEmail = computed(() => {
      68           11 :     if (isApiKeyLogin.value) {
      69            0 :       return apiKeyStore.currentUser?.email
      70              :     }
      71           11 :     return authStore.userEmail
      72              :   })
      73              : 
      74        36142 :   const providerId = computed(
      75              :     () =>
      76              :       sessionUser.value?.signInProvider ??
      77              :       firebaseForSession.value?.providerData[0]?.providerId
      78              :   )
      79              : 
      80        36142 :   const providerName = computed(() => {
      81            0 :     if (isApiKeyLogin.value) {
      82            0 :       return 'Comfy API Key'
      83              :     }
      84            0 :     if (providerId.value?.includes('google')) {
      85            0 :       return 'Google'
      86              :     }
      87            0 :     if (providerId.value?.includes('github')) {
      88            0 :       return 'GitHub'
      89              :     }
      90            0 :     return providerId.value
      91              :   })
      92              : 
      93        36142 :   const providerIcon = computed(() => {
      94            0 :     if (isApiKeyLogin.value) {
      95            0 :       return 'icon-[lucide--key]'
      96              :     }
      97            0 :     if (providerId.value?.includes('google')) {
      98            0 :       return 'pi pi-google'
      99              :     }
     100            0 :     if (providerId.value?.includes('github')) {
     101            0 :       return 'icon-[lucide--github]'
     102              :     }
     103            0 :     return 'icon-[lucide--user]'
     104              :   })
     105              : 
     106        36142 :   const isEmailProvider = computed(() => {
     107            0 :     if (isApiKeyLogin.value || authStore.signedInWithSso) {
     108            0 :       return false
     109              :     }
     110            0 :     const firebaseUser = firebaseForSession.value
     111            0 :     return firebaseUser
     112              :       ? firebaseUser.providerData[0]?.providerId === 'password'
     113              :       : sessionUser.value?.signInProvider === 'password'
     114              :   })
     115              : 
     116        36142 :   const needsFirebaseSignIn = computed(
     117              :     () => !!sessionUser.value && !firebaseForSession.value
     118              :   )
     119              : 
     120        36142 :   const userPhotoUrl = computed(() => {
     121           17 :     if (isApiKeyLogin.value) return null
     122           17 :     return firebaseForSession.value?.photoURL
     123              :   })
     124              : 
     125        36142 :   const handleSignOut = async () => {
     126            0 :     if (isApiKeyLogin.value) {
     127            0 :       await apiKeyStore.clearStoredApiKey()
     128              :     } else {
     129            0 :       await commandStore.execute('Comfy.User.SignOut')
     130              :     }
     131              :   }
     132              : 
     133        36142 :   const handleSignIn = async () => {
     134            2 :     await commandStore.execute('Comfy.User.OpenSignInDialog')
     135              :   }
     136              : 
     137        36142 :   return {
     138              :     loading: authStore.loading,
     139              :     isAuthInitialized,
     140              :     isLoggedIn,
     141              :     isApiKeyLogin,
     142              :     isEmailProvider,
     143              :     needsFirebaseSignIn,
     144              :     userDisplayName,
     145              :     userEmail,
     146              :     userPhotoUrl,
     147              :     providerName,
     148              :     providerIcon,
     149              :     resolvedUserInfo,
     150              :     handleSignOut,
     151              :     handleSignIn,
     152              :     onUserResolved,
     153              :     onTokenRefreshed,
     154              :     onUserLogout
     155              :   }
     156              : }
     157              : 
     158         2356 : provideCurrentUser(useCurrentUser)
        

Generated by: LCOV version 2.0-1