diff --git a/browser_tests/tests/cloudSsoSignIn.spec.ts b/browser_tests/tests/cloudSsoSignIn.spec.ts index e2d5a9ec76b..c431672d9a8 100644 --- a/browser_tests/tests/cloudSsoSignIn.spec.ts +++ b/browser_tests/tests/cloudSsoSignIn.spec.ts @@ -300,7 +300,7 @@ test.describe('Cloud login SSO entry', { tag: ['@cloud', '@ui'] }, () => { await expect(page.getByText(SSO_COPY.errors.orgDisabled)).toBeVisible() }) - test('a customer record refused with sso_required opens the SSO dialog, not a failure toast', async ({ + test('a customer record refused with sso_required shows the SSO notice on the page, not a failure toast', async ({ page, cloudAuth }) => { @@ -315,15 +315,19 @@ test.describe('Cloud login SSO entry', { tag: ['@cloud', '@ui'] }, () => { await signInWithEmail(page) await expect( - page - .getByRole('dialog') - .getByRole('heading', { name: SSO_COPY.required.title }) + page.getByRole('alert').filter({ + hasText: SSO_COPY.required.bodyWithEmail.replace( + '{email}', + CLOUD_SELF_EMAIL + ) + }) ).toBeVisible() + await expect(page.getByRole('dialog')).toHaveCount(0) await expect(page.getByText(/Failed to create customer/)).toHaveCount(0) await expect(page).toHaveURL(/\/cloud\/login/) }) - test('a Firebase sign-in ingest refuses with sso_required opens the SSO dialog', async ({ + test('a Firebase sign-in ingest refuses with sso_required shows the SSO notice on the page', async ({ page, cloudAuth }) => { @@ -337,15 +341,18 @@ test.describe('Cloud login SSO entry', { tag: ['@cloud', '@ui'] }, () => { await signInWithEmail(page) - const dialog = page.getByRole('dialog') await expect( - dialog.getByRole('heading', { name: SSO_COPY.required.title }) + page.getByRole('alert').filter({ + hasText: SSO_COPY.required.bodyWithEmail.replace( + '{email}', + CLOUD_SELF_EMAIL + ) + }) ).toBeVisible() await expect( - dialog.getByText( - SSO_COPY.required.bodyWithEmail.replace('{email}', CLOUD_SELF_EMAIL) - ) - ).toBeVisible() + page.getByRole('button', { name: SSO_COPY.continueWithSso }) + ).toHaveCount(1) + await expect(page.getByRole('dialog')).toHaveCount(0) await expect(page).toHaveURL(/\/cloud\/login/) }) }) diff --git a/src/platform/auth/sso/SsoRequiredDialogContent.vue b/src/platform/auth/sso/SsoRequiredDialogContent.vue index 67530c76600..8f8e076e4cf 100644 --- a/src/platform/auth/sso/SsoRequiredDialogContent.vue +++ b/src/platform/auth/sso/SsoRequiredDialogContent.vue @@ -44,18 +44,11 @@ diff --git a/src/platform/auth/sso/ssoRequired.ts b/src/platform/auth/sso/ssoRequired.ts index 5996cc97a0e..accd4a3d7b2 100644 --- a/src/platform/auth/sso/ssoRequired.ts +++ b/src/platform/auth/sso/ssoRequired.ts @@ -9,6 +9,7 @@ import { SELF_STYLED_PANEL_CONTENT_CLASS } from '@/components/ui/dialog/dialog.v import { useFeatureFlags } from '@/composables/useFeatureFlags' import { t } from '@/i18n' import { SSO_REQUIRED_DIALOG_KEY } from '@/platform/auth/sso/ssoRequiredDialogKey' +import { presentInline } from '@/platform/auth/sso/ssoRequiredInline' import { reportError } from '@/platform/telemetry/reportError' import { useToastStore } from '@/platform/updates/common/toastStore' import { useDialogStore } from '@/stores/dialogStore' @@ -37,6 +38,7 @@ export function presentSsoRequired(context: SsoRequiredContext = {}): boolean { organizationId: context.organizationId }) } + if (presentInline(known)) return true void import('@/platform/auth/sso/SsoRequiredDialogContent.vue') .then(({ default: component }) => { if (dialogStore.isDialogOpen(SSO_REQUIRED_DIALOG_KEY)) { diff --git a/src/platform/auth/sso/ssoRequiredInline.test.ts b/src/platform/auth/sso/ssoRequiredInline.test.ts new file mode 100644 index 00000000000..70fc7df0de4 --- /dev/null +++ b/src/platform/auth/sso/ssoRequiredInline.test.ts @@ -0,0 +1,63 @@ +import { render, screen } from '@testing-library/vue' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { defineComponent, h } from 'vue' + +import { useFeatureFlags } from '@/composables/useFeatureFlags' +import { presentSsoRequired } from '@/platform/auth/sso/ssoRequired' +import { SSO_REQUIRED_DIALOG_KEY } from '@/platform/auth/sso/ssoRequiredDialogKey' +import { useSsoRequiredInlineHost } from '@/platform/auth/sso/ssoRequiredInline' +import { useDialogStore } from '@/stores/dialogStore' + +vi.mock(import('@/composables/useFeatureFlags')) + +const AuthPage = defineComponent({ + setup() { + const { notice } = useSsoRequiredInlineHost() + return () => h('pre', JSON.stringify(notice.value ?? null)) + } +}) + +async function dialogShown() { + await vi.dynamicImportSettled() + return useDialogStore().dialogStack.some( + (dialog) => dialog.key === SSO_REQUIRED_DIALOG_KEY + ) +} + +describe('the SSO-required notice on an auth page', () => { + afterEach(() => { + useDialogStore().closeDialog({ key: SSO_REQUIRED_DIALOG_KEY }) + }) + + it('shows on the page instead of the shared dialog, keeping what each caller knew', async () => { + vi.mocked(useFeatureFlags().flags).ssoEnabled = true + render(AuthPage) + + expect(presentSsoRequired({ email: 'ada@acme.com' })).toBe(true) + expect(presentSsoRequired({ organizationId: 'org_meta' })).toBe(true) + + expect( + await screen.findByText( + JSON.stringify({ email: 'ada@acme.com', organizationId: 'org_meta' }) + ) + ).toBeInTheDocument() + expect(await dialogShown()).toBe(false) + }) + + it('falls back to the dialog once the page is gone', async () => { + vi.mocked(useFeatureFlags().flags).ssoEnabled = true + const page = render(AuthPage) + page.unmount() + + expect(presentSsoRequired({ email: 'ada@acme.com' })).toBe(true) + expect(await dialogShown()).toBe(true) + }) + + it('stays hidden with the flag off', async () => { + vi.mocked(useFeatureFlags().flags).ssoEnabled = false + render(AuthPage) + + expect(presentSsoRequired({ email: 'ada@acme.com' })).toBe(false) + expect(screen.getByText('null')).toBeInTheDocument() + }) +}) diff --git a/src/platform/auth/sso/ssoRequiredInline.ts b/src/platform/auth/sso/ssoRequiredInline.ts new file mode 100644 index 00000000000..8cef361d763 --- /dev/null +++ b/src/platform/auth/sso/ssoRequiredInline.ts @@ -0,0 +1,26 @@ +import { onUnmounted, readonly, shallowRef } from 'vue' + +import type { SsoRequiredContext } from '@/platform/auth/sso/ssoRequired' + +let hosts = 0 +const notice = shallowRef() + +/** + * Shows the refusal on the auth page that hosts it, in that page's brand + * design system, instead of the app-styled shared dialog. False without one. + */ +export function presentInline(context: SsoRequiredContext): boolean { + if (hosts === 0) return false + notice.value = { ...notice.value, ...context } + return true +} + +/** Makes the calling auth page the place an SSO refusal is shown. */ +export function useSsoRequiredInlineHost() { + hosts++ + onUnmounted(() => { + hosts-- + notice.value = undefined + }) + return { notice: readonly(notice) } +} diff --git a/src/platform/auth/sso/useContinueWithSso.ts b/src/platform/auth/sso/useContinueWithSso.ts new file mode 100644 index 00000000000..3bbb8ddeb49 --- /dev/null +++ b/src/platform/auth/sso/useContinueWithSso.ts @@ -0,0 +1,56 @@ +import { computed, ref } from 'vue' +import { useRouter } from 'vue-router' + +import { ssoStartUrl } from '@comfyorg/account-core/sso' + +import { useErrorHandling } from '@/composables/useErrorHandling' +import type { SsoRequiredContext } from '@/platform/auth/sso/ssoRequired' +import { toSsoReturnPath } from '@/platform/auth/sso/ssoReturnPath' +import { SSO_ENTRY_OPEN_QUERY } from '@/platform/cloud/onboarding/sso/ssoEntryQuery' +import { useAuthStore } from '@/stores/authStore' + +/** Signs out of Firebase and leaves for the refusing organization's SSO. */ +export function useContinueWithSso(context: () => SsoRequiredContext) { + const router = useRouter() + const authStore = useAuthStore() + const { toastErrorHandler } = useErrorHandling() + const knownEmail = computed(() => context().email ?? authStore.userEmail) + const leaving = ref(false) + + function destination(): string { + const { returnTo, organizationId } = context() + const back = { + returnTo: toSsoReturnPath(returnTo ?? router.currentRoute.value.fullPath), + origin: window.location.origin + } + if (organizationId) { + return ssoStartUrl({ + organizationId, + email: knownEmail.value ?? undefined, + ...back + }) + } + if (!knownEmail.value) { + return router.resolve({ + name: 'cloud-login', + query: SSO_ENTRY_OPEN_QUERY + }).href + } + return ssoStartUrl({ email: knownEmail.value, ...back }) + } + + async function continueWithSso() { + leaving.value = true + const target = destination() + try { + if (authStore.currentUser) await authStore.logout() + } catch (error) { + leaving.value = false + toastErrorHandler(error) + return + } + window.location.assign(target) + } + + return { knownEmail, leaving, continueWithSso } +} diff --git a/src/platform/cloud/onboarding/CloudLoginView.test.ts b/src/platform/cloud/onboarding/CloudLoginView.test.ts index 0ef4f797d18..04fd4c807ca 100644 --- a/src/platform/cloud/onboarding/CloudLoginView.test.ts +++ b/src/platform/cloud/onboarding/CloudLoginView.test.ts @@ -569,7 +569,7 @@ describe('CloudLoginView Firebase sign-in refused for SSO', () => { it.for([ { - name: 'signs the account out and shows the SSO screen', + name: 'signs the account out and shows the SSO notice on the page', url: '/cloud/login', returnTo: '/cloud/user-check' }, @@ -604,9 +604,14 @@ describe('CloudLoginView Firebase sign-in refused for SSO', () => { vi.mocked(presentSsoRequired).mock.results[0].value, 'shown before the sign-out drops the remote config that carries sso_enabled' ).toBe(true) - await waitFor(() => - expect(useDialogStore().isDialogOpen(SSO_REQUIRED_DIALOG_KEY)).toBe(true) - ) + expect( + await screen.findByText('auth.sso.required.bodyWithEmail') + ).toBeInTheDocument() + expect( + screen.getAllByRole('button', { name: 'auth.sso.continueWithSso' }), + 'the notice replaces the SSO entry, so there is one way on' + ).toHaveLength(1) + expect(useDialogStore().isDialogOpen(SSO_REQUIRED_DIALOG_KEY)).toBe(false) expect(useAuthStore().logout).toHaveBeenCalledOnce() expect(redirectAfterAuth).not.toHaveBeenCalled() }) diff --git a/src/platform/cloud/onboarding/CloudLoginView.vue b/src/platform/cloud/onboarding/CloudLoginView.vue index c0f80830602..add692d0a11 100644 --- a/src/platform/cloud/onboarding/CloudLoginView.vue +++ b/src/platform/cloud/onboarding/CloudLoginView.vue @@ -27,6 +27,12 @@ {{ t(ssoErrorKey) }} + +