Skip to content

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

fix(studio): send new signups to org creation directly (#41041)

Danny Whiteee931e49a1f6
diff --git a/apps/studio/components/interfaces/SignIn/SignUpForm.tsx b/apps/studio/components/interfaces/SignIn/SignUpForm.tsx
index f4640f9ad34..aa32fb110b0 100644
--- a/apps/studio/components/interfaces/SignIn/SignUpForm.tsx
+++ b/apps/studio/components/interfaces/SignIn/SignUpForm.tsx
@@ -24,7 +24,7 @@ import z from 'zod'
import PasswordConditionsHelper from './PasswordConditionsHelper'
import { useSignUpMutation } from '@/data/misc/signup-mutation'
import { BASE_PATH } from '@/lib/constants'
-import { buildPathWithParams } from '@/lib/gotrue'
+import { buildSignUpReturnPath } from '@/lib/gotrue'
import { classifyApiError, classifyValidationError } from '@/lib/telemetry/funnel-errors'
import { useTrackFunnelError } from '@/lib/telemetry/use-track-funnel-error'
@@ -110,10 +110,9 @@ export const SignUpForm = () => {
}
redirectTo = `${redirectUrlBase}/authorize?${authorizeParams.toString()}`
} else {
- // Use getRedirectToPath to handle redirect_to parameter and other query params
+ // Build the post-verification return path from returnTo and other query params.
const { returnTo } = router.query
- const basePath = returnTo || '/sign-in'
- const fullPath = buildPathWithParams(basePath as string)
+ const fullPath = buildSignUpReturnPath(returnTo)
const fullRedirectUrl = `${redirectUrlBase}${fullPath}`
redirectTo = fullRedirectUrl
}
diff --git a/apps/studio/lib/gotrue.test.ts b/apps/studio/lib/gotrue.test.ts
index 01b0a3ea82a..dc4dca400d0 100644
--- a/apps/studio/lib/gotrue.test.ts
+++ b/apps/studio/lib/gotrue.test.ts
@@ -1,6 +1,108 @@
import { describe, expect, it } from 'vitest'
-import { DEFAULT_FALLBACK_PATH, validateReturnTo } from './gotrue'
+import {
+ buildSignUpReturnPath,
+ DEFAULT_FALLBACK_PATH,
+ DEFAULT_SIGNUP_RETURN_PATH,
+ getSignUpReturnTo,
+ validateReturnTo,
+} from './gotrue'
+
+describe('getSignUpReturnTo', () => {
+ it('defaults to /new when returnTo is missing', () => {
+ expect(getSignUpReturnTo(undefined)).toBe(DEFAULT_SIGNUP_RETURN_PATH)
+ expect(getSignUpReturnTo('')).toBe(DEFAULT_SIGNUP_RETURN_PATH)
+ })
+
+ it('rewrites /org and /organizations to /new', () => {
+ expect(getSignUpReturnTo('/org')).toBe(DEFAULT_SIGNUP_RETURN_PATH)
+ expect(getSignUpReturnTo('/organizations')).toBe(DEFAULT_SIGNUP_RETURN_PATH)
+ })
+
+ it('rewrites org list paths while preserving query params', () => {
+ expect(getSignUpReturnTo('/org?foo=bar')).toBe('/new?foo=bar')
+ expect(getSignUpReturnTo('/organizations?foo=bar')).toBe('/new?foo=bar')
+ })
+
+ it('preserves explicit destinations including invite paths', () => {
+ expect(getSignUpReturnTo('/join?token=abc')).toBe('/join?token=abc')
+ expect(getSignUpReturnTo('/project/ref')).toBe('/project/ref')
+ })
+
+ it('uses the first value when returnTo is an array', () => {
+ expect(getSignUpReturnTo(['/join?token=abc', '/other'])).toBe('/join?token=abc')
+ expect(getSignUpReturnTo(['/org'])).toBe(DEFAULT_SIGNUP_RETURN_PATH)
+ expect(getSignUpReturnTo(['/organizations'])).toBe(DEFAULT_SIGNUP_RETURN_PATH)
+ })
+})
+
+describe('buildSignUpReturnPath', () => {
+ it('defaults to /new when returnTo is missing', () => {
+ // @ts-ignore
+ delete window.location
+ // @ts-ignore
+ window.location = { search: '' }
+
+ expect(buildSignUpReturnPath(undefined)).toBe(DEFAULT_SIGNUP_RETURN_PATH)
+ })
+
+ it('rewrites /org and /organizations to /new', () => {
+ // @ts-ignore
+ delete window.location
+ // @ts-ignore
+ window.location = { search: '' }
+
+ expect(buildSignUpReturnPath('/org')).toBe(DEFAULT_SIGNUP_RETURN_PATH)
+ expect(buildSignUpReturnPath('/organizations')).toBe(DEFAULT_SIGNUP_RETURN_PATH)
+ })
+
+ it('rewrites org list paths while preserving query params', () => {
+ // @ts-ignore
+ delete window.location
+ // @ts-ignore
+ window.location = { search: '' }
+
+ expect(buildSignUpReturnPath('/org?foo=bar')).toBe('/new?foo=bar')
+ expect(buildSignUpReturnPath('/organizations?foo=bar')).toBe('/new?foo=bar')
+ })
+
+ it('preserves invite returnTo paths', () => {
+ // @ts-ignore
+ delete window.location
+ // @ts-ignore
+ window.location = { search: '' }
+
+ expect(buildSignUpReturnPath('/join?token=abc')).toBe('/join?token=abc')
+ })
+
+ it('falls back to /new for invalid returnTo values', () => {
+ // @ts-ignore
+ delete window.location
+ // @ts-ignore
+ window.location = { search: '' }
+
+ expect(buildSignUpReturnPath('https://evil.com')).toBe(DEFAULT_SIGNUP_RETURN_PATH)
+ expect(buildSignUpReturnPath('//evil.com')).toBe(DEFAULT_SIGNUP_RETURN_PATH)
+ })
+
+ it('strips returnTo from the current location search params', () => {
+ // @ts-ignore
+ delete window.location
+ // @ts-ignore
+ window.location = { search: '?returnTo=/org&foo=bar' }
+
+ expect(buildSignUpReturnPath('/new')).toBe('/new?foo=bar')
+ })
+
+ it('uses the first value when returnTo is an array', () => {
+ // @ts-ignore
+ delete window.location
+ // @ts-ignore
+ window.location = { search: '' }
+
+ expect(buildSignUpReturnPath(['/join?token=abc', '/other'])).toBe('/join?token=abc')
+ })
+})
describe('validateReturnTo', () => {
const fallback = DEFAULT_FALLBACK_PATH
diff --git a/apps/studio/lib/gotrue.ts b/apps/studio/lib/gotrue.ts
index a80ab433d6e..b7277c16c6f 100644
--- a/apps/studio/lib/gotrue.ts
+++ b/apps/studio/lib/gotrue.ts
@@ -5,6 +5,49 @@ import { gotrueClient } from 'common/gotrue'
export const auth = gotrueClient
export const DEFAULT_FALLBACK_PATH = '/organizations'
+export const DEFAULT_SIGNUP_RETURN_PATH = '/new'
+
+/**
+ * When unauthenticated users hit protected dashboard routes, withAuth sets
+ * returnTo to the current path. Marketing entry via /dashboard often ends up
+ * as returnTo=/org (or /organizations). New users who switch to sign-up should
+ * start org creation instead.
+ */
+export function getSignUpReturnTo(returnTo: string | string[] | undefined): string {
+ const value = Array.isArray(returnTo) ? returnTo[0] : returnTo
+
+ if (!value) {
+ return DEFAULT_SIGNUP_RETURN_PATH
+ }
+
+ const [pathname, query] = value.split('?', 2)
+ if (pathname === DEFAULT_FALLBACK_PATH || pathname === '/org') {
+ return query ? `${DEFAULT_SIGNUP_RETURN_PATH}?${query}` : DEFAULT_SIGNUP_RETURN_PATH
+ }
+
+ return value
+}
+
+/** Post-signup redirect path, normalising returnTo and excluding it from merged query params. */
+export function buildSignUpReturnPath(returnTo: string | string[] | undefined): string {
+ const basePath = validateReturnTo(getSignUpReturnTo(returnTo), DEFAULT_SIGNUP_RETURN_PATH)
+ const [pathOnly, pathQuery] = basePath.split('?', 2)
+ const pathnameSearchParams = new URLSearchParams(pathQuery || '')
+
+ if (typeof location === 'undefined') {
+ const queryString = pathnameSearchParams.toString()
+ return queryString ? `${pathOnly}?${queryString}` : pathOnly
+ }
+
+ const mergedParams = new URLSearchParams(location.search)
+ mergedParams.delete('returnTo')
+ for (const [key, val] of pathnameSearchParams.entries()) {
+ mergedParams.set(key, val)
+ }
+
+ const queryString = mergedParams.toString()
+ return queryString ? `${pathOnly}?${queryString}` : pathOnly
+}
export const validateReturnTo = (
returnTo: string,
diff --git a/apps/studio/pages/organizations.tsx b/apps/studio/pages/organizations.tsx
index 8ea61cabe20..89c7fddbda0 100644
--- a/apps/studio/pages/organizations.tsx
+++ b/apps/studio/pages/organizations.tsx
@@ -1,8 +1,7 @@
import { Plus, Search } from 'lucide-react'
import Head from 'next/head'
import Link from 'next/link'
-import { useRouter } from 'next/router'
-import { useEffect, useState } from 'react'
+import { useState } from 'react'
import { Button, Skeleton } from 'ui'
import { Input } from 'ui-patterns/DataInputs/Input'
@@ -22,7 +21,6 @@ import { buildStudioPageTitle } from '@/lib/page-title'
import type { NextPageWithLayout } from '@/types'
const OrganizationsPage: NextPageWithLayout = () => {
- const router = useRouter()
const { appTitle } = useCustomContent(['app:title'])
const [search, setSearch] = useState('')
const pageTitle = buildStudioPageTitle({
@@ -46,13 +44,6 @@ const OrganizationsPage: NextPageWithLayout = () => {
(x) => x.name.toLowerCase().includes(search) || x.slug.toLowerCase().includes(search)
)
- useEffect(() => {
- // If there are no organizations, force the user to create one
- if (isSuccess && organizations.length <= 0) {
- router.push('/new')
- }
- }, [isSuccess, organizations])
-
return (
<>
<Head>
@@ -109,7 +100,7 @@ const OrganizationsPage: NextPageWithLayout = () => {
OrganizationsPage.getLayout = (page) => (
<AppLayout>
<DefaultLayout hideMobileMenu headerTitle="Organizations">
- <PageLayout title="Your Organizations" className="max-w-[1200px] lg:px-6 mx-auto">
+ <PageLayout title="Your organizations" className="max-w-[1200px] lg:px-6 mx-auto">
{page}
</PageLayout>
</DefaultLayout>
diff --git a/apps/studio/routes/_app/organizations.tsx b/apps/studio/routes/_app/organizations.tsx
index 117da507ee7..41b2ec86baa 100644
--- a/apps/studio/routes/_app/organizations.tsx
+++ b/apps/studio/routes/_app/organizations.tsx
@@ -14,7 +14,7 @@ export const Route = createFileRoute('/_app/organizations')({
function Organizations() {
// Next page default export is already wrapped in withAuth.
return (
- <PageLayout title="Your Organizations" className="max-w-[1200px] lg:px-6 mx-auto">
+ <PageLayout title="Your organizations" className="max-w-[1200px] lg:px-6 mx-auto">
<OrganizationsPage dehydratedState={undefined} />
</PageLayout>
)