Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: multiple profile creation

Alaister Young13459a711a36
diff --git a/studio/components/interfaces/App/CommandMenuWrapper.tsx b/studio/components/interfaces/App/CommandMenuWrapper.tsx
index c4498e11db1..c45cae8447f 100644
--- a/studio/components/interfaces/App/CommandMenuWrapper.tsx
+++ b/studio/components/interfaces/App/CommandMenuWrapper.tsx
@@ -8,13 +8,13 @@ import { PermissionAction } from '@supabase/shared-types/out/constants'
import { uuidv4 } from 'lib/helpers'
import { checkPermissions, useFlag, useStore } from 'hooks'
-import useProfile from 'hooks/misc/useProfile'
import { createSqlSnippetSkeleton } from '../SQLEditor/SQLEditor.utils'
import { useSqlEditorStateSnapshot } from 'state/sql-editor'
+import { codeBlock } from 'common-tags'
import { SqlSnippet } from 'data/content/sql-snippets-query'
import { useProjectApiQuery } from 'data/config/project-api-query'
-import { codeBlock } from 'common-tags'
import { useEntityDefinitionsQuery } from 'data/database/entity-definitions-query'
+import { useProfileQuery } from 'data/profile/profile-query'
const CommandMenuWrapper = observer(({ children }: PropsWithChildren<{}>) => {
const { ref } = useParams()
@@ -25,7 +25,7 @@ const CommandMenuWrapper = observer(({ children }: PropsWithChildren<{}>) => {
const allowCMDKDataOptIn = useFlag('dashboardCmdkDataOptIn')
const isOptedInToAI = opt_in_tags?.includes('AI_SQL_GENERATOR_OPT_IN') ?? false
- const { data: profile } = useProfile()
+ const { data: profile } = useProfileQuery()
const { data: settings } = useProjectApiQuery({ projectRef: ref })
const canCreateSQLSnippet = checkPermissions(PermissionAction.CREATE, 'user_content', {
resource: { type: 'sql', owner_id: profile?.id },
diff --git a/studio/components/interfaces/Organization/TeamSettings/MembersView.tsx b/studio/components/interfaces/Organization/TeamSettings/MembersView.tsx
index df807884036..1727cb59eaa 100644
--- a/studio/components/interfaces/Organization/TeamSettings/MembersView.tsx
+++ b/studio/components/interfaces/Organization/TeamSettings/MembersView.tsx
@@ -6,7 +6,6 @@ import { Badge, Button, Loading, Listbox, IconUser, Modal, IconAlertCircle, Icon
import { Member } from 'types'
import { useStore } from 'hooks'
-import useProfile from 'hooks/misc/useProfile'
import { useParams } from 'common/hooks'
import ShimmeringLoader from 'components/ui/ShimmeringLoader'
@@ -18,6 +17,7 @@ import { isInviteExpired, getUserDisplayName } from '../Organization.utils'
import { useOrganizationRolesQuery } from 'data/organizations/organization-roles-query'
import { useOrganizationDetailQuery } from 'data/organizations/organization-detail-query'
import { useOrganizationMemberUpdateMutation } from 'data/organizations/organization-member-update-mutation'
+import { useProfileQuery } from 'data/profile/profile-query'
interface SelectedMember extends Member {
oldRoleId: number
@@ -32,7 +32,7 @@ const MembersView = ({ searchString }: MembersViewProps) => {
const { ui } = useStore()
const { slug } = useParams()
- const { data: profile } = useProfile()
+ const { data: profile } = useProfileQuery()
const { data: detailData, isLoading: isLoadingOrgDetails } = useOrganizationDetailQuery({ slug })
const { data: rolesData, isLoading: isLoadingRoles } = useOrganizationRolesQuery({ slug })
const { mutate: updateOrganizationMember, isLoading } = useOrganizationMemberUpdateMutation({
diff --git a/studio/components/interfaces/Organization/TeamSettings/TeamSettings.tsx b/studio/components/interfaces/Organization/TeamSettings/TeamSettings.tsx
index 04cb1a953a2..cada57f4926 100644
--- a/studio/components/interfaces/Organization/TeamSettings/TeamSettings.tsx
+++ b/studio/components/interfaces/Organization/TeamSettings/TeamSettings.tsx
@@ -4,7 +4,6 @@ import { Button, Input, IconSearch } from 'ui'
import * as Tooltip from '@radix-ui/react-tooltip'
import { useStore } from 'hooks'
-import useProfile from 'hooks/misc/useProfile'
import { useParams } from 'common/hooks'
import { post } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
@@ -14,12 +13,13 @@ import { getRolesManagementPermissions, hasMultipleOwners } from './TeamSettings
import { confirmAlert } from 'components/to-be-cleaned/ModalsDeprecated/ConfirmModal'
import { useOrganizationDetailQuery } from 'data/organizations/organization-detail-query'
import { useOrganizationRolesQuery } from 'data/organizations/organization-roles-query'
+import { useProfileQuery } from 'data/profile/profile-query'
const TeamSettings = () => {
const { ui } = useStore()
const { slug } = useParams()
- const { data: profile } = useProfile()
+ const { data: profile } = useProfileQuery()
const isOwner = ui.selectedOrganization?.is_owner
const { data: detailData } = useOrganizationDetailQuery({ slug })
diff --git a/studio/components/interfaces/Reports/Reports.tsx b/studio/components/interfaces/Reports/Reports.tsx
index f4215efde7f..bfb9cb69220 100644
--- a/studio/components/interfaces/Reports/Reports.tsx
+++ b/studio/components/interfaces/Reports/Reports.tsx
@@ -17,7 +17,6 @@ import {
import { PermissionAction } from '@supabase/shared-types/out/constants'
import { checkPermissions } from 'hooks'
-import useProfile from 'hooks/misc/useProfile'
import { useParams } from 'common/hooks'
import { uuidv4 } from 'lib/helpers'
import { METRIC_CATEGORIES, METRICS, TIME_PERIODS_REPORTS } from 'lib/constants'
@@ -27,13 +26,14 @@ import DateRangePicker from 'components/to-be-cleaned/DateRangePicker'
import NoPermission from 'components/ui/NoPermission'
import GridResize from './GridResize'
import { LAYOUT_COLUMN_COUNT } from './Reports.constants'
+import { useProfileQuery } from 'data/profile/profile-query'
const DEFAULT_CHART_COLUMN_COUNT = 12
const DEFAULT_CHART_ROW_COUNT = 4
const Reports = () => {
const { id, ref } = useParams()
- const { data: profile } = useProfile()
+ const { data: profile } = useProfileQuery()
const [report, setReport] = useState<any>()
diff --git a/studio/components/interfaces/SQLEditor/SQLTemplates/SQLTemplates.tsx b/studio/components/interfaces/SQLEditor/SQLTemplates/SQLTemplates.tsx
index d1e950982a9..1aa386bdac5 100644
--- a/studio/components/interfaces/SQLEditor/SQLTemplates/SQLTemplates.tsx
+++ b/studio/components/interfaces/SQLEditor/SQLTemplates/SQLTemplates.tsx
@@ -4,13 +4,13 @@ import { PermissionAction } from '@supabase/shared-types/out/constants'
import Telemetry from 'lib/telemetry'
import { checkPermissions, useStore } from 'hooks'
-import useProfile from 'hooks/misc/useProfile'
import { SQL_TEMPLATES } from 'components/interfaces/SQLEditor/SQLEditor.constants'
import SQLCard from './SQLCard'
import { createSqlSnippetSkeleton } from '../SQLEditor.utils'
import { useTelemetryProps, useParams } from 'common'
import { useRouter } from 'next/router'
import { SqlSnippet } from 'data/content/sql-snippets-query'
+import { useProfileQuery } from 'data/profile/profile-query'
import { useSqlEditorStateSnapshot } from 'state/sql-editor'
import { uuidv4 } from 'lib/helpers'
@@ -18,7 +18,7 @@ const SQLTemplates = observer(() => {
const { ui } = useStore()
const { ref } = useParams()
const router = useRouter()
- const { data: profile } = useProfile()
+ const { data: profile } = useProfileQuery()
const [sql, quickStart] = partition(SQL_TEMPLATES, { type: 'template' })
const telemetryProps = useTelemetryProps()
diff --git a/studio/components/interfaces/Settings/Logs/LogsQueryPanel.tsx b/studio/components/interfaces/Settings/Logs/LogsQueryPanel.tsx
index 1a3b564ad34..8ae231a0dad 100644
--- a/studio/components/interfaces/Settings/Logs/LogsQueryPanel.tsx
+++ b/studio/components/interfaces/Settings/Logs/LogsQueryPanel.tsx
@@ -13,8 +13,8 @@ import DatePickers from './Logs.DatePickers'
import Link from 'next/link'
import React from 'react'
import { checkPermissions } from 'hooks'
-import useProfile from 'hooks/misc/useProfile'
import { IS_PLATFORM } from 'lib/constants'
+import { useProfileQuery } from 'data/profile/profile-query'
export interface LogsQueryPanelProps {
templates?: LogTemplate[]
@@ -45,7 +45,7 @@ const LogsQueryPanel = ({
onDateChange,
warnings,
}: LogsQueryPanelProps) => {
- const { data: profile } = useProfile()
+ const { data: profile } = useProfileQuery()
const canCreateLogQuery = checkPermissions(PermissionAction.CREATE, 'user_content', {
resource: { type: 'log_sql', owner_id: profile?.id },
subject: { id: profile?.id },
diff --git a/studio/components/interfaces/Support/SupportForm.tsx b/studio/components/interfaces/Support/SupportForm.tsx
index ee7f566166e..20e5cbdef10 100644
--- a/studio/components/interfaces/Support/SupportForm.tsx
+++ b/studio/components/interfaces/Support/SupportForm.tsx
@@ -19,7 +19,6 @@ import { CLIENT_LIBRARIES } from 'common/constants'
import { Project } from 'types'
import { useStore, useFlag } from 'hooks'
-import useProfile from 'hooks/misc/useProfile'
import { post, get } from 'lib/common/fetch'
import { detectBrowser } from 'lib/helpers'
import { API_URL, PRICING_TIER_PRODUCT_IDS } from 'lib/constants'
@@ -31,6 +30,7 @@ import InformationBox from 'components/ui/InformationBox'
import { formatMessage, uploadAttachments } from './SupportForm.utils'
import { CATEGORY_OPTIONS, SEVERITY_OPTIONS, SERVICE_OPTIONS } from './Support.constants'
import DisabledStateForFreeTier from './DisabledStateForFreeTier'
+import { useProfileQuery } from 'data/profile/profile-query'
const MAX_ATTACHMENTS = 5
@@ -76,7 +76,7 @@ const SupportForm: FC<Props> = ({ setSentCategory }) => {
}
}, [uploadedFiles])
- const { data: profile } = useProfile()
+ const { data: profile } = useProfileQuery()
const respondToEmail = profile?.primary_email ?? 'your email'
if (!isInitialized) {
diff --git a/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx b/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx
index c85e72837b6..e4874093443 100644
--- a/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx
+++ b/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx
@@ -15,7 +15,6 @@ import {
import { PermissionAction } from '@supabase/shared-types/out/constants'
import { checkPermissions, useFlag, useStore } from 'hooks'
-import useProfile from 'hooks/misc/useProfile'
import { IS_PLATFORM } from 'lib/constants'
import { uuidv4 } from 'lib/helpers'
@@ -23,6 +22,7 @@ import ProductMenuItem from 'components/ui/ProductMenu/ProductMenuItem'
import { useParams } from 'common'
import { useSnippets, useSqlEditorStateSnapshot } from 'state/sql-editor'
import { SqlSnippet, useSqlSnippetsQuery } from 'data/content/sql-snippets-query'
+import { useProfileQuery } from 'data/profile/profile-query'
import ShimmeringLoader from 'components/ui/ShimmeringLoader'
import QueryItem from './QueryItem'
import { createSqlSnippetSkeleton } from 'components/interfaces/SQLEditor/SQLEditor.utils'
@@ -33,7 +33,7 @@ const SideBarContent = observer(() => {
const { ui } = useStore()
const { ref, id } = useParams()
const router = useRouter()
- const { data: profile } = useProfile()
+ const { data: profile } = useProfileQuery()
const [filterString, setFilterString] = useState('')
const { setPages, setIsOpen } = useCommandMenu()
const showCmdkHelper = useFlag('dashboardCmdk')
diff --git a/studio/hooks/misc/useProfile.tsx b/studio/hooks/misc/useProfile.tsx
deleted file mode 100644
index a41439114b3..00000000000
--- a/studio/hooks/misc/useProfile.tsx
+++ /dev/null
@@ -1,41 +0,0 @@
-import { useRouter } from 'next/router'
-
-import { useTelemetryProps } from 'common'
-import { useProfileCreateMutation } from 'data/profile/profile-create-mutation'
-import { useProfileQuery } from 'data/profile/profile-query'
-import Telemetry from 'lib/telemetry'
-
-/**
- * Fetches the user's profile, creating one if it does not exist
- */
-function useProfile() {
- const router = useRouter()
- const telemetryProps = useTelemetryProps()
-
- const { mutate: createProfile, isLoading: isCreating } = useProfileCreateMutation({
- onSuccess() {
- Telemetry.sendEvent(
- { category: 'conversion', action: 'sign_up', label: '' },
- telemetryProps,
- router
- )
- },
- })
-
- const { isLoading, ...result } = useProfileQuery({
- onError(err) {
- // if the user does not yet exist, create a profile for them
- if (typeof err === 'object' && err !== null && 'code' in err && (err as any).code === 404) {
- createProfile()
- }
- },
- })
-
- return {
- // Continue the loading state until the profile is created
- isLoading: isLoading || isCreating,
- ...result,
- }
-}
-
-export default useProfile
diff --git a/studio/lib/auth.tsx b/studio/lib/auth.tsx
index b42ac9c84bc..b5b31e36721 100644
--- a/studio/lib/auth.tsx
+++ b/studio/lib/auth.tsx
@@ -1,6 +1,6 @@
import { useQueryClient } from '@tanstack/react-query'
import { useRouter } from 'next/router'
-import { PropsWithChildren, useCallback, useEffect } from 'react'
+import { PropsWithChildren, useCallback, useEffect, useRef } from 'react'
import {
AuthContext as AuthContextInternal,
@@ -13,6 +13,7 @@ import { useStore } from 'hooks'
import Telemetry from 'lib/telemetry'
import { GOTRUE_ERRORS, IS_PLATFORM } from './constants'
import { clearLocalStorage } from './local-storage'
+import { useProfileCreateMutation } from 'data/profile/profile-create-mutation'
export const AuthContext = AuthContextInternal
@@ -54,6 +55,40 @@ export const AuthProvider = ({ children }: PropsWithChildren<{}>) => {
return subscription.unsubscribe
}, [])
+ const notificationIdRef = useRef<string | undefined>()
+ const { mutate: createProfile } = useProfileCreateMutation({
+ onMutate() {
+ notificationIdRef.current = ui.setNotification({
+ category: 'loading',
+ message: 'Creating your profile...',
+ })
+ },
+ onSuccess() {
+ ui.setNotification({
+ category: 'success',
+ message: 'Profile created successfully!',
+ id: notificationIdRef.current,
+ })
+
+ notificationIdRef.current = undefined
+
+ Telemetry.sendEvent(
+ { category: 'conversion', action: 'sign_up', label: '' },
+ telemetryProps,
+ router
+ )
+ },
+ onError(err) {
+ ui.setNotification({
+ category: 'error',
+ message: 'Failed to create your profile. Please refresh to try again.',
+ id: notificationIdRef.current,
+ })
+
+ notificationIdRef.current = undefined
+ },
+ })
+
// Track telemetry for the current user
useProfileQuery({
onSuccess(profile) {
@@ -65,9 +100,12 @@ export const AuthProvider = ({ children }: PropsWithChildren<{}>) => {
// creation of the default org). Hence why calling org load here
app.organizations.load()
},
- // Never rerun the query
- staleTime: Infinity,
- cacheTime: Infinity,
+ onError(err) {
+ // if the user does not yet exist, create a profile for them
+ if (typeof err === 'object' && err !== null && 'code' in err && (err as any).code === 404) {
+ createProfile()
+ }
+ },
})
return <AuthProviderInternal alwaysLoggedIn={!IS_PLATFORM}>{children}</AuthProviderInternal>
diff --git a/studio/pages/account/me.tsx b/studio/pages/account/me.tsx
index cbe90639743..2ecbc5de734 100644
--- a/studio/pages/account/me.tsx
+++ b/studio/pages/account/me.tsx
@@ -6,8 +6,8 @@ import SchemaFormPanel from 'components/to-be-cleaned/forms/SchemaFormPanel'
import Panel from 'components/ui/Panel'
import { Profile as ProfileType } from 'data/profile/types'
import { useProfileUpdateMutation } from 'data/profile/profile-update-mutation'
+import { useProfileQuery } from 'data/profile/profile-query'
import { useStore } from 'hooks'
-import useProfile from 'hooks/misc/useProfile'
import { useSession } from 'lib/auth'
import Link from 'next/link'
import { NextPageWithLayout } from 'types'
@@ -41,7 +41,7 @@ const ProfileCard = observer(() => {
const { ui } = useStore()
const { mutateAsync } = useProfileUpdateMutation()
- const { data: profile } = useProfile()
+ const { data: profile } = useProfileQuery()
// TODO: ^ handle loading state
const updateUser = async (model: any) => {
diff --git a/studio/pages/join/index.tsx b/studio/pages/join/index.tsx
index ce5174adbce..a87cffe2e10 100644
--- a/studio/pages/join/index.tsx
+++ b/studio/pages/join/index.tsx
@@ -4,10 +4,10 @@ import { useRouter } from 'next/router'
import { Button, IconCheckSquare, Loading } from 'ui'
import { useStore } from 'hooks'
-import useProfile from 'hooks/misc/useProfile'
import { useSignOut } from 'lib/auth'
import { API_URL } from 'lib/constants'
import { get, post, delete_ } from 'lib/common/fetch'
+import { useProfileQuery } from 'data/profile/profile-query'
interface ITokenInfo {
organization_name?: string | undefined
@@ -24,7 +24,7 @@ const JoinOrganizationPage = () => {
const router = useRouter()
const { slug, token, name } = router.query
const { ui, app } = useStore()
- const { data: profile } = useProfile()
+ const { data: profile } = useProfileQuery()
const signOut = useSignOut()
const [isSubmitting, setIsSubmitting] = useState(false)
diff --git a/studio/pages/project/[ref]/reports/index.tsx b/studio/pages/project/[ref]/reports/index.tsx
index a533b467837..40c56009800 100644
--- a/studio/pages/project/[ref]/reports/index.tsx
+++ b/studio/pages/project/[ref]/reports/index.tsx
@@ -5,7 +5,6 @@ import { PermissionAction } from '@supabase/shared-types/out/constants'
import { NextPageWithLayout } from 'types'
import { checkPermissions, useFlag, useStore } from 'hooks'
-import useProfile from 'hooks/misc/useProfile'
import { useParams } from 'common/hooks'
import { post } from 'lib/common/fetch'
import { API_URL, PROJECT_STATUS } from 'lib/constants'
@@ -14,6 +13,7 @@ import Loading from 'components/ui/Loading'
import ProductEmptyState from 'components/to-be-cleaned/ProductEmptyState'
import { createReport } from 'components/to-be-cleaned/Reports/Reports.utils'
import { ReportsLayout } from 'components/layouts'
+import { useProfileQuery } from 'data/profile/profile-query'
export const UserReportPage: NextPageWithLayout = () => {
const [loading, setLoading] = useState(true)
@@ -21,7 +21,7 @@ export const UserReportPage: NextPageWithLayout = () => {
const router = useRouter()
const { ref } = useParams()
- const { data: profile } = useProfile()
+ const { data: profile } = useProfileQuery()
const { ui } = useStore()
const project = ui.selectedProject