Skip to content

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

feat(profile): add self-serve editing of username and primary email (#38042)

Chris Stocktona1f2e5006804
diff --git a/apps/studio/components/interfaces/Account/Preferences/AccountIdentities.tsx b/apps/studio/components/interfaces/Account/Preferences/AccountIdentities.tsx
index 1f68854fcc3..81801248215 100644
--- a/apps/studio/components/interfaces/Account/Preferences/AccountIdentities.tsx
+++ b/apps/studio/components/interfaces/Account/Preferences/AccountIdentities.tsx
@@ -31,6 +31,13 @@ import {
SSOChangeEmailAddress,
} from './ChangeEmailAddress'
+const getProviderName = (provider: string) =>
+ provider === 'github'
+ ? 'GitHub'
+ : provider.startsWith('sso')
+ ? 'SSO'
+ : provider.replaceAll('_', ' ')
+
export const AccountIdentities = () => {
const router = useRouter()
@@ -54,9 +61,6 @@ export const AccountIdentities = () => {
const [_, message] = router.asPath.split('#message=')
- const getProviderName = (provider: string) =>
- provider === 'github' ? 'GitHub' : provider.startsWith('sso') ? 'SSO' : provider
-
const onConfirmUnlinkIdentity = async () => {
const identity = identities.find((i) => i.provider === selectedProviderUnlink)
if (identity) unlinkIdentity(identity)
@@ -115,7 +119,7 @@ export const AccountIdentities = () => {
</div>
<p className="text-sm text-foreground-lighter">
{!!username ? <span>{username} • </span> : null}
- {(identity as any).email}
+ {identity.email}
</p>
</div>
</div>
diff --git a/apps/studio/components/interfaces/Account/Preferences/ProfileInformation.tsx b/apps/studio/components/interfaces/Account/Preferences/ProfileInformation.tsx
index e4497165102..2c9decfe6ec 100644
--- a/apps/studio/components/interfaces/Account/Preferences/ProfileInformation.tsx
+++ b/apps/studio/components/interfaces/Account/Preferences/ProfileInformation.tsx
@@ -2,25 +2,35 @@ import { zodResolver } from '@hookform/resolvers/zod'
import { SubmitHandler, useForm } from 'react-hook-form'
import { toast } from 'sonner'
import {
+ Button,
+ Card,
+ CardContent,
+ CardFooter,
+ CardHeader,
FormControl_Shadcn_,
FormField_Shadcn_,
- FormItem_Shadcn_,
- FormLabel_Shadcn_,
- FormMessage_Shadcn_,
Form_Shadcn_,
Input_Shadcn_,
+ SelectContent_Shadcn_,
+ SelectItem_Shadcn_,
+ SelectTrigger_Shadcn_,
+ SelectValue_Shadcn_,
+ Select_Shadcn_,
} from 'ui'
import z from 'zod'
-import { FormActions } from 'components/ui/Forms/FormActions'
-import Panel from 'components/ui/Panel'
+import { useProfileIdentitiesQuery } from 'data/profile/profile-identities-query'
import { useProfileUpdateMutation } from 'data/profile/profile-update-mutation'
import { useProfile } from 'lib/profile'
+import { groupBy } from 'lodash'
import type { FormSchema } from 'types'
+import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
const FormSchema = z.object({
first_name: z.string().optional(),
last_name: z.string().optional(),
+ username: z.string().optional(),
+ primary_email: z.string().email().optional(),
})
const formId = 'profile-information-form'
@@ -28,76 +38,151 @@ const formId = 'profile-information-form'
export const ProfileInformation = () => {
const { profile } = useProfile()
+ const {
+ data: identityData,
+ isLoading: isIdentitiesLoading,
+ isSuccess: isIdentitiesSuccess,
+ } = useProfileIdentitiesQuery()
+ const identities = (identityData?.identities ?? []).filter((x) => x.identity_data?.email !== null)
+ const dedupedIdentityEmails = Object.keys(groupBy(identities, 'identity_data.email'))
+
+ const defaultValues = {
+ first_name: profile?.first_name ?? '',
+ last_name: profile?.last_name ?? '',
+ username: profile?.username ?? '',
+ primary_email: profile?.primary_email ?? '',
+ }
+
const form = useForm({
resolver: zodResolver(FormSchema),
- defaultValues: { first_name: profile?.first_name ?? '', last_name: profile?.last_name ?? '' },
+ defaultValues,
+ values: defaultValues,
})
const { mutate: updateProfile, isLoading } = useProfileUpdateMutation({
onSuccess: (data) => {
toast.success('Successfully saved profile')
- form.reset({ first_name: data.first_name, last_name: data.last_name })
+ const { first_name, last_name, username, primary_email } = data
+ form.reset({ first_name, last_name, username, primary_email })
},
onError: (error) => toast.error(`Failed to update profile: ${error.message}`),
})
const onSubmit: SubmitHandler<z.infer<typeof FormSchema>> = async (data) => {
- updateProfile({ firstName: data.first_name || '', lastName: data.last_name || '' })
+ updateProfile({
+ firstName: data.first_name || '',
+ lastName: data.last_name || '',
+ username: data.username || '',
+ primaryEmail: data.primary_email || '',
+ })
}
return (
- <>
- <Panel
- className="mb-4 md:mb-8"
- title={<h5>Profile Information</h5>}
- footer={
- <FormActions
- form={formId}
- isSubmitting={isLoading}
- hasChanges={form.formState.isDirty}
- handleReset={() => form.reset()}
- />
- }
- >
- <Form_Shadcn_ {...form}>
- <form
- id={formId}
- className="space-y-6 w-full p-4 md:p-8"
- onSubmit={form.handleSubmit(onSubmit)}
- >
+ <Form_Shadcn_ {...form}>
+ <form id={formId} className="space-y-6 w-full" onSubmit={form.handleSubmit(onSubmit)}>
+ <Card className="mb-8">
+ <CardHeader>Profile Information</CardHeader>
+ <CardContent className="flex flex-col gap-y-2">
<FormField_Shadcn_
control={form.control}
name="first_name"
render={({ field }) => (
- <FormItem_Shadcn_ className="grid gap-2 md:grid md:grid-cols-12 space-y-0">
- <FormLabel_Shadcn_ className="flex flex-col space-y-2 col-span-4 text-sm justify-center text-foreground-light">
- First name
- </FormLabel_Shadcn_>
+ <FormItemLayout label="First name" layout="horizontal">
<FormControl_Shadcn_ className="col-span-8">
- <Input_Shadcn_ {...field} className="w-full" />
+ <Input_Shadcn_ {...field} className="w-72" />
</FormControl_Shadcn_>
- <FormMessage_Shadcn_ className="col-start-5 col-span-8" />
- </FormItem_Shadcn_>
+ </FormItemLayout>
)}
/>
<FormField_Shadcn_
control={form.control}
name="last_name"
render={({ field }) => (
- <FormItem_Shadcn_ className="grid gap-2 md:grid md:grid-cols-12 space-y-0">
- <FormLabel_Shadcn_ className="flex flex-col space-y-2 col-span-4 text-sm justify-center text-foreground-light">
- Last name
- </FormLabel_Shadcn_>
+ <FormItemLayout label="Last name" layout="horizontal">
+ <FormControl_Shadcn_ className="col-span-8">
+ <Input_Shadcn_ {...field} className="w-72" />
+ </FormControl_Shadcn_>
+ </FormItemLayout>
+ )}
+ />
+ <FormField_Shadcn_
+ control={form.control}
+ name="primary_email"
+ render={({ field }) => (
+ <FormItemLayout label="Primary email" layout="horizontal">
+ <FormControl_Shadcn_ className="col-span-8">
+ <div className="flex flex-col gap-1">
+ <Select_Shadcn_
+ value={field.value}
+ onValueChange={field.onChange}
+ disabled={profile?.is_sso_user}
+ >
+ <SelectTrigger_Shadcn_ className="col-span-8 w-72">
+ <SelectValue_Shadcn_ placeholder="Select primary email" />
+ </SelectTrigger_Shadcn_>
+ <SelectContent_Shadcn_ className="col-span-8">
+ {isIdentitiesSuccess &&
+ dedupedIdentityEmails.map((email) => (
+ <SelectItem_Shadcn_ key={email} value={email}>
+ {email}
+ </SelectItem_Shadcn_>
+ ))}
+ </SelectContent_Shadcn_>
+ </Select_Shadcn_>
+ {(profile?.is_sso_user && (
+ <p className="text-xs text-foreground-light">
+ Primary email is managed by your SSO provider and cannot be changed here.
+ </p>
+ )) || (
+ <p className="text-xs text-foreground-light">
+ Primary email is used for account notifications.
+ </p>
+ )}
+ </div>
+ </FormControl_Shadcn_>
+ </FormItemLayout>
+ )}
+ />
+ <FormField_Shadcn_
+ control={form.control}
+ name="username"
+ render={({ field }) => (
+ <FormItemLayout label="Username" layout="horizontal">
<FormControl_Shadcn_ className="col-span-8">
- <Input_Shadcn_ {...field} className="w-full" />
+ <div className="flex flex-col gap-1">
+ <Input_Shadcn_ {...field} className="w-72" disabled={profile?.is_sso_user} />
+ {(profile?.is_sso_user && (
+ <p className="text-xs text-foreground-light">
+ Username is managed by your SSO provider and cannot be changed here.
+ </p>
+ )) || (
+ <p className="text-xs text-foreground-light">
+ Username appears as a display name throughout the dashboard.
+ </p>
+ )}
+ </div>
</FormControl_Shadcn_>
- <FormMessage_Shadcn_ className="col-start-5 col-span-8" />
- </FormItem_Shadcn_>
+ </FormItemLayout>
)}
/>
- </form>
- </Form_Shadcn_>
- </Panel>
- </>
+ </CardContent>
+ <CardFooter className="justify-end space-x-2">
+ {form.formState.isDirty && (
+ <Button type="default" onClick={() => form.reset()}>
+ Cancel
+ </Button>
+ )}
+ <Button
+ type="primary"
+ htmlType="submit"
+ loading={isLoading || isIdentitiesLoading}
+ disabled={!form.formState.isDirty}
+ >
+ Save
+ </Button>
+ </CardFooter>
+ </Card>
+ </form>
+ </Form_Shadcn_>
)
}
diff --git a/apps/studio/components/interfaces/UserDropdown.tsx b/apps/studio/components/interfaces/UserDropdown.tsx
index 2a23a8950a0..86dafac63bf 100644
--- a/apps/studio/components/interfaces/UserDropdown.tsx
+++ b/apps/studio/components/interfaces/UserDropdown.tsx
@@ -1,12 +1,14 @@
-import { Command, FlaskConical, Settings } from 'lucide-react'
+import { Command, FlaskConical, Loader2, Settings } from 'lucide-react'
import { useTheme } from 'next-themes'
import Link from 'next/link'
import { useRouter } from 'next/router'
import { ProfileImage } from 'components/ui/ProfileImage'
+import { useProfileIdentitiesQuery } from 'data/profile/profile-identities-query'
import { useIsFeatureEnabled } from 'hooks/misc/useIsFeatureEnabled'
import { useSignOut } from 'lib/auth'
import { IS_PLATFORM } from 'lib/constants'
+import { getGitHubProfileImgUrl } from 'lib/github'
import { useProfile } from 'lib/profile'
import { useAppStateSnapshot } from 'state/app-state'
import {
@@ -29,13 +31,22 @@ import { useFeaturePreviewModal } from './App/FeaturePreview/FeaturePreviewConte
export function UserDropdown() {
const router = useRouter()
const signOut = useSignOut()
- const { profile } = useProfile()
+ const { profile, isLoading: isLoadingProfile } = useProfile()
const { theme, setTheme } = useTheme()
const appStateSnapshot = useAppStateSnapshot()
const setCommandMenuOpen = useSetCommandMenuOpen()
const { openFeaturePreviewModal } = useFeaturePreviewModal()
const profileShowEmailEnabled = useIsFeatureEnabled('profile:show_email')
+ const { username, primary_email } = profile ?? {}
+
+ const { data, isLoading: isLoadingIdentities } = useProfileIdentitiesQuery()
+ const isGitHubProfile = profile?.auth0_id.startsWith('github')
+ const gitHubUsername = isGitHubProfile
+ ? (data?.identities ?? []).find((x) => x.provider === 'github')?.identity_data?.user_name
+ : undefined
+ const profileImageUrl = isGitHubProfile ? getGitHubProfileImgUrl(gitHubUsername) : undefined
+
return (
<DropdownMenu>
<DropdownMenuTrigger className="border flex-shrink-0 px-3" asChild>
@@ -43,11 +54,17 @@ export function UserDropdown() {
type="default"
className="[&>span]:flex px-0 py-0 rounded-full overflow-hidden h-8 w-8"
>
- <ProfileImage
- alt={profile?.username}
- src={profile?.profileImageUrl}
- className="w-8 h-8 rounded-md"
- />
+ {isLoadingProfile || isLoadingIdentities ? (
+ <div className="w-full h-full flex items-center justify-center">
+ <Loader2 className="animate-spin text-foreground-lighter" size={16} />
+ </div>
+ ) : (
+ <ProfileImage
+ alt={profile?.username}
+ src={profileImageUrl}
+ className="w-8 h-8 rounded-md"
+ />
+ )}
</Button>
</DropdownMenuTrigger>
@@ -57,18 +74,15 @@ export function UserDropdown() {
<div className="px-2 py-1 flex flex-col gap-0 text-sm">
{profile && (
<>
- <span
- title={profile.username}
- className="w-full text-left text-foreground truncate"
- >
- {profile.username}
+ <span title={username} className="w-full text-left text-foreground truncate">
+ {username}
</span>
- {profile.primary_email !== profile.username && profileShowEmailEnabled && (
+ {primary_email !== username && profileShowEmailEnabled && (
<span
- title={profile.primary_email}
+ title={primary_email}
className="w-full text-left text-foreground-light text-xs truncate"
>
- {profile.primary_email}
+ {primary_email}
</span>
)}
</>
diff --git a/apps/studio/data/profile/profile-identities-query.ts b/apps/studio/data/profile/profile-identities-query.ts
index b5eca84e2ff..e640502c83b 100644
--- a/apps/studio/data/profile/profile-identities-query.ts
+++ b/apps/studio/data/profile/profile-identities-query.ts
@@ -14,7 +14,7 @@ export async function getProfileIdentities() {
}
type ProfileIdentitiesData = {
- identities: UserIdentity[]
+ identities: (UserIdentity & { email?: string })[]
new_email?: string
email_change_sent_at?: string
}
diff --git a/apps/studio/data/profile/profile-update-mutation.ts b/apps/studio/data/profile/profile-update-mutation.ts
index 06f895cfb8f..d86aac70a8b 100644
--- a/apps/studio/data/profile/profile-update-mutation.ts
+++ b/apps/studio/data/profile/profile-update-mutation.ts
@@ -8,11 +8,23 @@ import { profileKeys } from './keys'
export type ProfileUpdateVariables = {
firstName: string
lastName: string
+ username: string
+ primaryEmail: string
}
-export async function updateProfile({ firstName, lastName }: ProfileUpdateVariables) {
+export async function updateProfile({
+ firstName,
+ lastName,
+ username,
+ primaryEmail,
+}: ProfileUpdateVariables) {
const { data, error } = await patch('/platform/profile', {
- body: { first_name: firstName, last_name: lastName },
+ body: {
+ first_name: firstName,
+ last_name: lastName,
+ username: username,
+ primary_email: primaryEmail,
+ },
})
if (error) handleError(error)
diff --git a/apps/studio/lib/profile.tsx b/apps/studio/lib/profile.tsx
index dd6edd2f722..5ecf4617b79 100644
--- a/apps/studio/lib/profile.tsx
+++ b/apps/studio/lib/profile.tsx
@@ -1,9 +1,9 @@
import * as Sentry from '@sentry/nextjs'
-import { useIsLoggedIn, useUser } from 'common'
import { useRouter } from 'next/router'
import { PropsWithChildren, createContext, useContext, useMemo } from 'react'
import { toast } from 'sonner'
+import { useIsLoggedIn, useUser } from 'common'
import { usePermissionsQuery } from 'data/permissions/permissions-query'
import { useProfileCreateMutation } from 'data/profile/profile-create-mutation'
import { useProfileQuery } from 'data/profile/profile-query'
@@ -11,7 +11,6 @@ import type { Profile } from 'data/profile/types'
import { useSendEventMutation } from 'data/telemetry/send-event-mutation'
import type { ResponseError } from 'types'
import { useSignOut } from './auth'
-import { getGitHubProfileImgUrl } from './github'
export type ProfileContextType = {
profile: Profile | undefined
@@ -96,12 +95,10 @@ export const ProfileProvider = ({ children }: PropsWithChildren<{}>) => {
const value = useMemo(() => {
const isLoading = isLoadingProfile || isCreatingProfile || isLoadingPermissions
- const isGHUser = !!profile && 'auth0_id' in profile && profile?.auth0_id.startsWith('github')
- const profileImageUrl = isGHUser ? getGitHubProfileImgUrl(profile.username) : undefined
return {
error,
- profile: !!profile ? { ...profile, profileImageUrl } : undefined,
+ profile,
isLoading,
isError,
isSuccess,
diff --git a/packages/api-types/types/platform.d.ts b/packages/api-types/types/platform.d.ts
index 19730327eeb..70b04347348 100644
--- a/packages/api-types/types/platform.d.ts
+++ b/packages/api-types/types/platform.d.ts
@@ -677,7 +677,7 @@ export interface paths {
patch?: never
trace?: never
}
- '/platform/integrations/private-link/{organization_slug}': {
+ '/platform/integrations/private-link/{slug}': {
parameters: {
query?: never
header?: never
@@ -7294,6 +7294,7 @@ export interface components {
gotrue_id: string
id: number
is_alpha_user: boolean
+ is_sso_user: boolean
last_name: string
mobile: string
primary_email: string
@@ -8924,6 +8925,9 @@ export interface components {
UpdateProfileBody: {
first_name?: string
last_name?: string
+ /** Format: email */
+ primary_email?: string
+ username?: string
}
UpdateProjectBody: {
name: string
@@ -10496,6 +10500,12 @@ export interface operations {
'application/json': components['schemas']['GetOrganizationIntegrationResponse'][]
}
}
+ 403: {
+ headers: {
+ [name: string]: unknown
+ }
+ content?: never
+ }
/** @description Failed to get integration with the given organization slug */
500: {
headers: {
@@ -10821,7 +10831,7 @@ export interface operations {
query?: never
header?: never
path: {
- organization_slug: string
+ slug: string
}
cookie?: never
}
@@ -10835,6 +10845,12 @@ export interface operations {
'application/json': components['schemas']['PrivateLinkResponse']
}
}
+ 403: {
+ headers: {
+ [name: string]: unknown
+ }
+ content?: never
+ }
/** @description Failed to retrieve organization's PrivateLink config */
500: {
headers: {
@@ -10849,7 +10865,7 @@ export interface operations {
query?: never
header?: never
path: {
- organization_slug: string
+ slug: string
}
cookie?: never
}
@@ -10867,6 +10883,12 @@ export interface operations {
'application/json': components['schemas']['PrivateLinkResponse']
}
}
+ 403: {
+ headers: {
+ [name: string]: unknown
+ }
+ content?: never
+ }
/** @description Failed to update organization's PrivateLink configuration. */
500: {
headers: {