Files
supabase/apps/studio/data/profile/profile-identities-query.ts
Ivan Vasilov 8b657165b5 chore: Migrate to use custom type for ReactQuery queries and mutations (#40073)
* Add custom types for queries, mutations and infinite queries.

* Migrate all queries to use the new type.

* Migrate all infinite queries to useCustomInfiniteQueryOptions.

* Migrate all mutations to use useCustomMutationOptions.

* Add type to all imports in `types` folder.
2025-11-03 13:18:13 +01:00

39 lines
1.4 KiB
TypeScript

import type { UserIdentity } from '@supabase/supabase-js'
import { useQuery } from '@tanstack/react-query'
import { auth } from 'lib/gotrue'
import { profileKeys } from './keys'
import { UseCustomQueryOptions } from 'types'
export async function getProfileIdentities() {
// getSession() uses a cached user object, which is almost never stale as the
// session refresh logic keeps it fresh. If there are claims of data not being
// fresh, it's because it was modified on another device / browser and the
// session hasn't been refreshed yet.
const { error, data } = await auth.getSession()
if (error) throw error
if (!data.session) throw new Error('Session not found with getSession()')
const { identities = [], new_email, email_change_sent_at } = data.session.user
return { identities, new_email, email_change_sent_at }
}
type ProfileIdentitiesData = {
identities: (UserIdentity & { email?: string })[]
new_email?: string
email_change_sent_at?: string
}
type ProfileIdentitiesError = any
export const useProfileIdentitiesQuery = <TData = ProfileIdentitiesData>({
enabled = true,
...options
}: UseCustomQueryOptions<ProfileIdentitiesData, ProfileIdentitiesError, TData> = {}) => {
return useQuery<ProfileIdentitiesData, ProfileIdentitiesError, TData>({
queryKey: profileKeys.identities(),
queryFn: () => getProfileIdentities(),
...options,
})
}